What should I do if I want to print value of ' i ' when I clicked 'like[i]' button? Like when I click on 'Button 2' then it should give me value '2'

huangapple go评论67阅读模式
英文:

What should I do if I want to print value of ' i ' when I clicked 'like[i]' button? Like when I click on 'Button 2' then it should give me value '2'

问题

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class button_array extends JFrame {
    int i;
    JLabel[] ulb = new JLabel[4];
    JButton[] like = new JButton[4];
    JPanel panel = new JPanel();

    public button_array() {
        new JFrame();
        i = 3;
        while (i > 0) {
            ulb[i] = new JLabel("user " + i);
            like[i] = new JButton("Button " + i);

            panel.add(ulb[i]);
            panel.add(like[i]);
            panel.add(Box.createVerticalStrut(25));

            like[i].addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println(i);
                }
            });

            i--;
        }

        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        add(panel);
        setSize(400, 400);
        setVisible(true);
    }

    public static void main(String[] args) {
        new button_array();
    }
}
  1. How do I get value id 'i' in action performed section. It gives value of 'i' as 0 for every button I clicked.
  2. What should I do if I want to print value of 'i' when I clicked 'like[i]' button. Like when I click on 'Button 2' then it should give me value '2'
英文:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class button_array extends JFrame{
	int i;
	JLabel[] ulb = new JLabel[4];
	JButton[] like = new JButton[4];
	JPanel panel = new JPanel();
	public button_array() {

		new JFrame();
		i =3;
		while(i>0) {
			ulb[i] = new JLabel("user "+i);
			like[i] = new JButton("Button "+i);
			
			
			panel.add(ulb[i]);
			panel.add(like[i]);
			panel.add(Box.createVerticalStrut(25));
			
			like[i].addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					// TODO Auto-generated method stub
					System.out.println(i);
				}
			});
			
			i--;
		}
	
	panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
	add(panel);
	setSize(400,400);
	setVisible(true);
	}
	public static void main(String[] args) {
		new button_array();
	}
}
  1. How do I get value id 'i' in action performed section. It gives value of 'i' as 0 for every button I clicked.
  2. What should I do if I want to print value of ' i ' when i clicked 'like[i]' button. Like when I click on 'Button 2' then it should give me value '2'

here's a link of screenshot

答案1

得分: 1

这段代码:

    System.out.println(i);

将无法工作,因为当你点击时,它将获取此刻的i的值,这个值在循环中因为i--等于0。

所以,如果你想要打印出like[]JButtonindex,你可以在重写的actionPerformed方法中这样做:

@Override
public void actionPerformed(ActionEvent e) {
    JButton o = (JButton)e.getSource();
                    
    for (int j = 0; j < like.length; j++) {
        if (like[j] != null && (JButton)like[j] == o) {
            System.out.println(j);
        }
    }
}

或者简单地打印出JButton.getText()的数字字符:

@Override
public void actionPerformed(ActionEvent e) {
    JButton o = (JButton)e.getSource();
    String str1 = o.getText();

    String str2 = str1.substring(6, str1.length());
    System.out.println(str2);
}
英文:

This code:

    System.out.println(i);

will not work because when you click, it will take the value of i at that moment, which is equal to 0, because of i-- in the loop.

So, if you want to print the index of JButton in like[], you could do like this in the override actionPerformed:

@Override
public void actionPerformed(ActionEvent e) {
    JButton o = (JButton)e.getSource();
                    
    for (int j = 0; j &lt; like.length; j++)
    {                        
        if (like[j]!= null &amp;&amp; (JButton)like[j] == o)
        {
            System.out.println(j);
        }
    }
}

or simply print the number character of JButton.getText():

@Override
public void actionPerformed(ActionEvent e) {
    JButton o = (JButton)e.getSource();
    String str1 = o.getText();

    String str2 = str1.substring(6, str1.length());
    System.out.println(j);
}

huangapple
  • 本文由 发表于 2020年9月29日 20:14:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/64119395.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定