我写了下面的代码,怎么按下按钮不起作用??是哪出问题了么???
JButton button1 = new JButton;
JButton button2 = new JButton;button1.addActionListener(new HandleControlButton());
button2.addActionListener(new HandleControlButton());
…………
class HandleControlButton implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(e.getSource()==button1){
int[] numbers = getNumbers(); // Get maxCount random numbers
for(int i = 0; i < numberCount; i++)
luckyNumbers[i].setValue(numbers[i]); // Set the button values
}
if(e.getSource()==button2){
Color color = new Color(
flipColor.getRGB()^luckyNumbers[0].getBackground().getRGB());
for(int i = 0; i < numberCount; i++)
luckyNumbers[i].setBackground(color); // Set the button colors
}
}
}
JButton button1 = new JButton;
JButton button2 = new JButton;button1.addActionListener(new HandleControlButton());
button2.addActionListener(new HandleControlButton());
…………
class HandleControlButton implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(e.getSource()==button1){
int[] numbers = getNumbers(); // Get maxCount random numbers
for(int i = 0; i < numberCount; i++)
luckyNumbers[i].setValue(numbers[i]); // Set the button values
}
if(e.getSource()==button2){
Color color = new Color(
flipColor.getRGB()^luckyNumbers[0].getBackground().getRGB());
for(int i = 0; i < numberCount; i++)
luckyNumbers[i].setBackground(color); // Set the button colors
}
}
}
JButton button2 = new JButton();
应该这样定义的吧
public void init() {
b.setLabel("aaa");
b.addActionListener(new ButtonActionListener());
this.add(b);
}
class ButtonActionListener implements ActionListener{ public void actionPerformed(ActionEvent e) {
if(e.getSource() == b){
System.out.println(b.getLabel());
}
}
}--------------------------
JButton button1 = new JButton; 该成new JButton();
JButton button2 = new JButton; new JButton();楼上说的对,你编译都通不过,怎么运行
参考下上面代码,自己改下
JButton button1 = new JButton();
JButton button2 = new JButton();