我有一个JLabel
和两个Jbutton
,名为booking和next。当点击下一个按钮时,将显示另一个图像。
next.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (i >= 8) {
i = 1;
} else {
i++;
}
i2 = new ImageIcon(this.getClass().getClassLoader().getResource("image/" + i + ".jpg"));
img.setIcon(i2);
}
});
假设图像2显示在JLabel
img中。点击预订按钮,它将转到预订gui。如何知道选择了哪个图像,以便它可以分别进入课堂?
答案 0 :(得分:1)
尝试以下
//private member used throughout the class
private int i;
i = 1;
next.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (i >= 8) {
i = 1;
} else {
i++;
}
i2 = new ImageIcon(this.getClass().getClassLoader().getResource("image/" + i + ".jpg"));
img.setIcon(i2);
}
});
}
//call test() to get the value
private void test()
{
//Your 'i' variable will have been initialised by the listener method above
switch( i )
{
case 1:
//add your code here
break;
case 2:
break;
case 3:
break;
default:
break;
}
}