哪里错了,谢谢。20分

freezhATsis 2009-04-09 10:52:00


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

public class ButtonTest{

public static void main(String[] args){
ButtonFrame frame = new ButtonFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

}

class ButtonFrame extends JFrame{

private JPanel buttonPanel;
private JButton yellowButton;
private JButton redButton;
private JButton blueButton;

public ButtonFrame(){
setTitle("ButtonTest");
setSize(300,200);
yellowButton = new JButton("Yellow");
redButton = new JButton("Red");
blueButton = new JButton("Blue");
buttonPanel = new JPanel();

buttonPanel.add(yellowButton);
buttonPanel.add(blueButton);
buttonPanel.add(redButton);

add(buttonPanel);

yellowButton.addActionListener(this);
redButton.addActionListener(this);
blueButton.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
Object eventSource = e.getSource();
if(eventSource == yellowButton){
buttonPanel.setBackground(Color.yellow);
}else if(eventSource == redButton){
buttonPanel.setBackground(Color.red);
}else if(eventSource == blueButton){
buttonPanel.setBackground(Color.blue);
}
}
}
...全文
68 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
pspyan 2009-04-09
  • 打赏
  • 举报
回复
class ButtonFrame extends JFrame implements ActionListener
实现ActionListener
freezhATsis 2009-04-09
  • 打赏
  • 举报
回复


恍然大悟,谢谢了!
lzbang 2009-04-09
  • 打赏
  • 举报
回复

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class ButtonTest{

public static void main(String[] args){
ButtonFrame frame = new ButtonFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

}

class ButtonFrame extends JFrame{

private JPanel buttonPanel;
private JButton yellowButton;
private JButton redButton;
private JButton blueButton;

public ButtonFrame(){
setTitle("ButtonTest");
setSize(300,200);
yellowButton = new JButton("Yellow");
redButton = new JButton("Red");
blueButton = new JButton("Blue");
buttonPanel = new JPanel();

buttonPanel.add(yellowButton);
buttonPanel.add(blueButton);
buttonPanel.add(redButton);

add(buttonPanel);

yellowButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
buttonPanel.setBackground(Color.yellow);
}
});

redButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
buttonPanel.setBackground(Color.red);
}
});
blueButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
buttonPanel.setBackground(Color.blue);
}
});
}

}

yuyeyi 2009-04-09
  • 打赏
  • 举报
回复
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ButtonTest{

public static void main(String[] args){
ButtonFrame frame = new ButtonFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

}

class ButtonFrame extends JFrame implements ActionListener{

private JPanel buttonPanel;
private JButton yellowButton;
private JButton redButton;
private JButton blueButton;

public ButtonFrame(){
setTitle("ButtonTest");
setSize(300,200);
yellowButton = new JButton("Yellow");
redButton = new JButton("Red");
blueButton = new JButton("Blue");
buttonPanel = new JPanel();

buttonPanel.add(yellowButton);
buttonPanel.add(blueButton);
buttonPanel.add(redButton);

add(buttonPanel);

yellowButton.addActionListener(this);
redButton.addActionListener(this);
blueButton.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
Object eventSource = e.getSource();
if(eventSource == yellowButton){
buttonPanel.setBackground(Color.yellow);
}else if(eventSource == redButton){
buttonPanel.setBackground(Color.red);
}else if(eventSource == blueButton){
buttonPanel.setBackground(Color.blue);
}
}
}
lzbang 2009-04-09
  • 打赏
  • 举报
回复
yellowButton.addActionListener(this);
redButton.addActionListener(this);
blueButton.addActionListener(this);

没有声明监听接口
yuyeyi 2009-04-09
  • 打赏
  • 举报
回复
上面的粘贴到你错误的
不好意思

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

public class ButtonTest{

public static void main(String[] args){
ButtonFrame frame = new ButtonFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

}

class ButtonFrame extends JFrame implements ActionListener{

private JPanel buttonPanel;
private JButton yellowButton;
private JButton redButton;
private JButton blueButton;

public ButtonFrame(){
setTitle("ButtonTest");
setSize(300,200);
yellowButton = new JButton("Yellow");
redButton = new JButton("Red");
blueButton = new JButton("Blue");
buttonPanel = new JPanel();

buttonPanel.add(yellowButton);
buttonPanel.add(blueButton);
buttonPanel.add(redButton);

add(buttonPanel);

yellowButton.addActionListener(this);
redButton.addActionListener(this);
blueButton.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
Object eventSource = e.getSource();
if(eventSource == yellowButton){
buttonPanel.setBackground(Color.yellow);
}else if(eventSource == redButton){
buttonPanel.setBackground(Color.red);
}else if(eventSource == blueButton){
buttonPanel.setBackground(Color.blue);
}
}
}
yuyeyi 2009-04-09
  • 打赏
  • 举报
回复
没有加入监听器接口加implements

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

public class ButtonTest{

public static void main(String[] args){
ButtonFrame frame = new ButtonFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

}

class ButtonFrame extends JFrame{

private JPanel buttonPanel;
private JButton yellowButton;
private JButton redButton;
private JButton blueButton;

public ButtonFrame(){
setTitle("ButtonTest");
setSize(300,200);
yellowButton = new JButton("Yellow");
redButton = new JButton("Red");
blueButton = new JButton("Blue");
buttonPanel = new JPanel();

buttonPanel.add(yellowButton);
buttonPanel.add(blueButton);
buttonPanel.add(redButton);

add(buttonPanel);

yellowButton.addActionListener(this);
redButton.addActionListener(this);
blueButton.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
Object eventSource = e.getSource();
if(eventSource == yellowButton){
buttonPanel.setBackground(Color.yellow);
}else if(eventSource == redButton){
buttonPanel.setBackground(Color.red);
}else if(eventSource == blueButton){
buttonPanel.setBackground(Color.blue);
}
}
}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧