一个关于背景颜色改变的问题

magicshop 2002-06-03 03:50:09
这是一个改变背景颜色的程序,但运行后颜色没改变阿?
请帮忙看看。谢谢

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

class FirstPanel extends JPanel
implements ActionListener
{ public FirstPanel()
{ JButton redButton = new JButton("Red");
JButton blueButton = new JButton("Blue");
JButton yellowButton = new JButton("Yellow");

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

redButton.addActionListener(this);
blueButton.addActionListener(this);
yellowButton.addActionListener(this);
}

public void actionPerformed(ActionEvent evt)
{ Object source = evt.getSource();
Color color = getBackground();
if (source == redButton) color = Color.red;
else if (source == blueButton) color = Color.blue;
else if (source == yellowButton) color = Color.yellow;
setBackground(color);
repaint();
}
private JButton redButton;
private JButton blueButton;
private JButton yellowButton;
}
class FirstFrame extends JFrame
{ public FirstFrame()
{ setTitle("FirstFrame");
setSize(300, 200);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
});

Container contentPane = getContentPane();
contentPane.add(new FirstPanel());
}
}

public class FirstTest
{ public static void main(String[] args)
{ JFrame frame = new FirstFrame();
frame.setVisible(true);
}
}
...全文
98 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
magicshop 2002-06-03
  • 打赏
  • 举报
回复
谢谢你们的帮助
magicshop 2002-06-03
  • 打赏
  • 举报
回复
to 喝酒的蚂蚁
我一点基础知识也没有居然都让你给看出来了,你咋这么聪明呢。我为我还能不能有美好的未来而郁闷
drinkant 2002-06-03
  • 打赏
  • 举报
回复
cow
被你打败了
很简单,在构造器中你的JButton redButton跟private JButton redButton根本就不是同一个JButton,作用域不同 !
看来你一点基础知识也没有呀. ..

good good study,day day up !
shihb 2002-06-03
  • 打赏
  • 举报
回复
你在构造函数中又定义了一组button,这是不对的!因为你在构造函数中定义的button只会在你的构造函数中有效,而在你的perform方法中就不知道是什么了!所以你的这组button应该在定义成成员变量,而不是一个方法中的变量!
magicshop 2002-06-03
  • 打赏
  • 举报
回复
真的阿!
我不明白为什么要有private JButton redButton;这几句话,作用是什么阿?
为什么有了这几句话前面加上JButton就无法改变颜色呢?请赐教!
drinkant 2002-06-03
  • 打赏
  • 举报
回复
错误在于
private JButton redButton;
private JButton blueButton;
private JButton yellowButton;
然后构造器中再
JButton redButton = new JButton("Red");
JButton blueButton = new JButton("Blue");
JButton yellowButton = new JButton("Yellow");
明白 ? 一开始偶也被迷惑了. . . . .

shihb 2002-06-03
  • 打赏
  • 举报
回复
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class FirstPanel extends JPanel
implements ActionListener
{ public FirstPanel()
{ JButton redButton = new JButton("Red");//你在这个地方不应该再定义三个JButton,你把这三个button前的JButton去掉就可以了!你试试!
JButton blueButton = new JButton("Blue");
JButton yellowButton = new JButton("Yellow");

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

redButton.addActionListener(this);
blueButton.addActionListener(this);
yellowButton.addActionListener(this);
}

public void actionPerformed(ActionEvent evt)
{ Object source = evt.getSource();
Color color = getBackground();
if (source == redButton) color = Color.red;
else if (source == blueButton) color = Color.blue;
else if (source == yellowButton) color = Color.yellow;
setBackground(color);
repaint();
}
private JButton redButton;
private JButton blueButton;
private JButton yellowButton;
}
class FirstFrame extends JFrame
{ public FirstFrame()
{ setTitle("FirstFrame");
setSize(300, 200);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
});

Container contentPane = getContentPane();
contentPane.add(new FirstPanel());
}
}

public class FirstTest
{ public static void main(String[] args)
{ JFrame frame = new FirstFrame();
frame.setVisible(true);
}
}
magicshop 2002-06-03
  • 打赏
  • 举报
回复
自己推一下吧:((((

62,614

社区成员

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

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