请帮我看一下:用java语言写的BUTTON事件处理,背景色总是红色,哪里有点点小问题呢~~~~~~

yao450860487 2008-12-15 04:27:51

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ButtonTest {

public static void main(String[] args)
{
Myframe frame=new Myframe();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class Myframe extends JFrame
{
public Myframe()
{ setTitle("MY FRAME");
setSize(300,400);
Mypanel panel=new Mypanel();
add(panel);
}
}
class Mypanel extends JPanel
{
private Color backgroundColor;
public Mypanel()
{
JButton button1 =new JButton("yellow");
JButton button2=new JButton("blue");
JButton button3=new JButton("red");
add(button1);
add(button2);
add(button3);
ColorAction ColorYellow=new ColorAction(Color.YELLOW);
ColorAction ColorBlue=new ColorAction(Color.BLUE);
ColorAction ColorRed=new ColorAction(Color.RED);
button1.addActionListener(ColorYellow);
button2.addActionListener(ColorBlue);
button3.addActionListener(ColorRed);
}
private class ColorAction implements ActionListener
{
public ColorAction(Color n)
{
backgroundColor=n;
}

public void actionPerformed(ActionEvent e)

{
setBackground(backgroundColor);

}
}
}
...全文
210 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
酸枣 2008-12-18
  • 打赏
  • 举报
回复
这样写也是可以的啊!

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ButtonTest extends JFrame{
JButton b1,b2,b3;
JPanel pan;
public ButtonTest(String title){
super(title);
b1=new JButton("yellow");
b2=new JButton("red");
b3=new JButton("blue");
pan=new JPanel();
setContentPane(pan);
pan.setLayout(new FlowLayout());
pan.add(b1);
pan.add(b2);
pan.add(b3);
b1.addActionListener(new Button());
b2.addActionListener(new Button());
b3.addActionListener(new Button());
}
class Button implements ActionListener{
public void actionPerformed(ActionEvent e){
String s=e.getActionCommand();
if (s.equals("yellow")) pan.setBackground(Color.YELLOW);
if (s.equals("red")) pan.setBackground(Color.RED);
if (s.equals("blue")) pan.setBackground(Color.BLUE);

}
}
public static void main(String[] args)
{
ButtonTest b=new ButtonTest("背景颜色");
b.setSize(400,800);
b.setVisible(true);
b.addWindowListener(new WindowAdapter(){
public void windowClosing( WindowEvent e){
System.exit(0);
}
});
}
}
gongfuliang 2008-12-15
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 bzwm 的回复:]
问题出在你的 ColerAction 类中。

应该把 private Color backgroundColor; 定义在ColerAction 中,而不是Mypanel类里

如下:

Java code
private class ColorAction implements ActionListener
{
private Color backgroundColor;
public ColorAction(Color n)
{
backgroundColor = n;
}

public void actionPerformed(ActionEvent e)

[/Quote]

你将backgroundColor定义在Mypanel中,就导致
ColorAction ColorYellow = new ColorAction(Color.YELLOW);//backgroundColor=Color.YELLOW
ColorAction ColorBlue = new ColorAction(Color.BLUE);//backgroundColor=Color.BLUE
ColorAction ColorRed = new ColorAction(Color.RED);//backgroundColor=Color.RED

也就是说最后一次backgroundColor=Color.RED,并且backgroundColor没有机会去更改
bzwm 2008-12-15
  • 打赏
  • 举报
回复
按照你的写法,只有最后一次new 出最后一个ColerAction类时给 backgroundColor 的赋值是有效的

也就是红色的那次:ColorAction ColorRed = new ColorAction(Color.RED);

此时 背景色的全局变量已经变成红色的了,

但你并没有去对JPanle设置背景色,

所以当你按任何一个按钮的时候,就进行了 设置背景色,

所以不管按哪个按钮,都是红色。
bzwm 2008-12-15
  • 打赏
  • 举报
回复
问题出在你的 ColerAction 类中。

应该把 private Color backgroundColor; 定义在ColerAction 中,而不是Mypanel类里

如下:

private class ColorAction implements ActionListener
{
private Color backgroundColor;
public ColorAction(Color n)
{
backgroundColor = n;
}

public void actionPerformed(ActionEvent e)
{
setBackground(backgroundColor);
}
}
酸枣 2008-12-15
  • 打赏
  • 举报
回复
看了看还是无能为力啊!我都不知道setbackground()的参数是什么啊,怎样设置背景颜色啊?
hp5212000 2008-12-15
  • 打赏
  • 举报
回复

package com.csdn;

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

public class ButtonTest {

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

class Myframe extends JFrame {
public Myframe() {
setTitle("MY FRAME");
setSize(300, 400);
Mypanel panel = new Mypanel();
add(panel);
}
}

class Mypanel extends JPanel {
// private Color backgroundColor;
public Mypanel() {
JButton button1 = new JButton("yellow");
JButton button2 = new JButton("blue");
JButton button3 = new JButton("red");
add(button1);
add(button2);
add(button3);
ColorAction ColorYellow = new ColorAction(Color.YELLOW);
ColorAction ColorBlue = new ColorAction(Color.BLUE);
ColorAction ColorRed = new ColorAction(Color.RED);

button1.addActionListener(ColorYellow);
button2.addActionListener(ColorBlue);
button3.addActionListener(ColorRed);
}

private class ColorAction implements ActionListener {

Color backgroundColor;

public ColorAction(Color n) {
this.backgroundColor = n;
}

public void actionPerformed(ActionEvent e)

{
setBackground(backgroundColor);

}
}
}
lisl2003 2008-12-15
  • 打赏
  • 举报
回复
纯顶帖

62,616

社区成员

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

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