英文:
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();
}
}
- How do I get value id 'i' in action performed section. It gives value of 'i' as 0 for every button I clicked.
- 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();
}
}
- How do I get value id 'i' in action performed section. It gives value of 'i' as 0 for every button I clicked.
- 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'
答案1
得分: 1
这段代码:
System.out.println(i);
将无法工作,因为当你点击时,它将获取此刻的i
的值,这个值在循环中因为i--
等于0。
所以,如果你想要打印出like[]
中JButton
的index
,你可以在重写的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 < like.length; j++)
{
if (like[j]!= null && (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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论