事件监听

csupanmin 2010-05-03 04:31:34
我在写关于时间监听的小程序,但是不知道为什么只能显示一种颜色,不能换颜色,代码如下:
package com.panmin.ActionDemo;

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

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 {
public ButtonFrame(){
setTitle("事件监听");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

ButtonPanel panel = new ButtonPanel();
add(panel);
}
public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 300;

}


class ButtonPanel extends JPanel {
public ButtonPanel(){
JButton redButton = new JButton("RED");
JButton yellowButton = new JButton("YELLO");
JButton blueButton = new JButton("BLUE");

add(redButton);
add(yellowButton);
add(blueButton);


ColorAction yellow = new ColorAction(Color.YELLOW);
ColorAction red = new ColorAction(Color.RED);
ColorAction blue = new ColorAction(Color.BLUE);

redButton.addActionListener(red);
yellowButton.addActionListener(yellow);
blueButton.addActionListener(blue);

}

private class ColorAction implements ActionListener {
public ColorAction( Color c){
color = c;
}
public void actionPerformed(ActionEvent e) {

setBackground(color);
}
}
private Color color;
}
...全文
138 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
csupanmin 2010-05-04
  • 打赏
  • 举报
回复
各位大虾,格式我知道,只是在发帖的时候我是复制粘贴的,所以格式有点乱,谢谢各位大虾的帮助,在后来运行的时候我就发现错误了,只是一是蒙了
superzhaohailiang 2010-05-03
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wd9053 的回复:]
引用楼主 csupanmin 的回复:
setBackground(color);
}
}
private Color color;
}

color放在ButtonFrame里面了,结果最后被赋值为Color.BLUE
应该为
setBackground(color);
}
private Color color;
}
}
注意缩进。。
[/Quote]
格式很重要哦!!
shrekting777 2010-05-03
  • 打赏
  • 举报
回复

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

public class ButtonTest {

public static void main(String[] args) {

ButtonFrame frame = new ButtonFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}//

}//class


@SuppressWarnings("serial")
class ButtonFrame extends JFrame {
public ButtonFrame(){
setTitle("事件监听");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

ButtonPanel panel = new ButtonPanel();
add(panel);
}//
public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 300;

}//class


@SuppressWarnings("serial")
class ButtonPanel extends JPanel {
public ButtonPanel(){
JButton redButton = new JButton("RED");
JButton yellowButton = new JButton("YELLO");
JButton blueButton = new JButton("BLUE");

add(redButton);
add(yellowButton);
add(blueButton);


ColorAction yellow = new ColorAction(Color.YELLOW);
ColorAction red = new ColorAction(Color.RED);
ColorAction blue = new ColorAction(Color.BLUE);

redButton.addActionListener(red);
yellowButton.addActionListener(yellow);
blueButton.addActionListener(blue);

}//

private class ColorAction implements ActionListener {
public ColorAction( Color c){
color = c;
}
public void actionPerformed(ActionEvent e) {

setBackground(color);
}
private Color color;
}//



}//class

private Color color;
写在了class ColorAction类的外面
dabanphoto 2010-05-03
  • 打赏
  • 举报
回复
格式很重要呀楼主。
欢乐极客 2010-05-03
  • 打赏
  • 举报
回复
将[ code=java][ /code]中的第一个空格去掉。
欢乐极客 2010-05-03
  • 打赏
  • 举报
回复

package com.xiaoyong;

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

public class ButtonTest {

public static void main(String[] args) {

ButtonFrame frame = new ButtonFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

}

@SuppressWarnings("serial")
class ButtonFrame extends JFrame {
public ButtonFrame() {
setTitle("事件监听");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

ButtonPanel panel = new ButtonPanel();
add(panel);
}

public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 300;

}

@SuppressWarnings("serial")
class ButtonPanel extends JPanel {
public ButtonPanel() {
JButton redButton = new JButton("RED");
JButton yellowButton = new JButton("YELLO");
JButton blueButton = new JButton("BLUE");

add(redButton);
add(yellowButton);
add(blueButton);

ColorAction yellow = new ColorAction(Color.YELLOW);
ColorAction red = new ColorAction(Color.RED);
ColorAction blue = new ColorAction(Color.BLUE);

redButton.addActionListener(red);
yellowButton.addActionListener(yellow);
blueButton.addActionListener(blue);

}

private class ColorAction implements ActionListener {
private Color color;

public ColorAction(Color c) {
color = c;
}

public void actionPerformed(ActionEvent e) {

setBackground(color);
}
}
}


这样就没问题了...


PS:LZ发帖的时候注意,将java代码写入[ code=Java ][ /code ]中进行格式化显示
wd9053 2010-05-03
  • 打赏
  • 举报
回复
[Quote=引用楼主 csupanmin 的回复:]
setBackground(color);
}
}
private Color color;
}
[/Quote]
color放在ButtonFrame里面了,结果最后被赋值为Color.BLUE
应该为
setBackground(color);
}
private Color color;
}
}
注意缩进。。

62,621

社区成员

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

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