关于Java核心编程中的一个例子----设置JPanel的背景色

Ljglory 2002-03-06 10:38:24
原例是这样的:在一个JPanel放三个按钮,按下以后分别使背景色变为红色、蓝色和黄色。

我想给例子再加个按钮,按下后使背景色变为原来的颜色(好像就是灰色);可是按下总是不起作用。代码如下(仅JPanel类):

class MyPanel extends JPanel implements ActionListener
{
private JButton yellowbtn;
private JButton bluebtn;
private JButton redbtn;
private JButton restorebtn;

public MyPanel()
{
yellowbtn = new JButton("Yellow");
bluebtn = new JButton("Blue");
redbtn = new JButton("Red");
restorebtn = new JButton("Restore");

add(yellowbtn);
add(bluebtn);
add(redbtn);
add(restorebtn);

yellowbtn.addActionListener(this);
bluebtn.addActionListener(this);
redbtn.addActionListener(this);
restorebtn.addActionListener(this);
}

public void actionPerformed(ActionEvent evt)
{
Object evtSource = evt.getSource();
Color originalcolor = getBackground();
Color color = getBackground();
if ( evtSource == yellowbtn ) color = Color.yellow;
else if ( evtSource == bluebtn ) color = Color.blue;
else if ( evtSource == redbtn ) color = Color.red;
else if ( evtSource == restorebtn ) color = originalcolor;
setBackground(color);
repaint();

}
}
...全文
277 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Ljglory 2002-03-07
  • 打赏
  • 举报
回复
我知道了,昨天晚上下楼时突然省悟过来了,应该把
originalcolor = getBackground();放在构造函数中。

谢谢两位!
peacock_king 2002-03-06
  • 打赏
  • 举报
回复
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class MyPanel extends JPanel implements ActionListener
{
private JButton yellowbtn;
private JButton bluebtn;
private JButton redbtn;
private JButton restorebtn;
Color originalcolor = getBackground();

public MyPanel()
{
yellowbtn = new JButton("Yellow");
bluebtn = new JButton("Blue");
redbtn = new JButton("Red");
restorebtn = new JButton("Restore");

add(yellowbtn);
add(bluebtn);
add(redbtn);
add(restorebtn);

yellowbtn.addActionListener(this);
bluebtn.addActionListener(this);
redbtn.addActionListener(this);
restorebtn.addActionListener(this);
}

public void actionPerformed(ActionEvent evt)
{
Object evtSource = evt.getSource();
Color color = getBackground();
if ( evtSource == yellowbtn ) color = Color.yellow;
else if ( evtSource == bluebtn ) color = Color.blue;
else if ( evtSource == redbtn ) color = Color.red;
else if ( evtSource == restorebtn ) color = originalcolor;
setBackground(color);
repaint();

}
public static void main(String[] args){
JDialog.setDefaultLookAndFeelDecorated(true);
JFrame.setDefaultLookAndFeelDecorated(true);
Toolkit.getDefaultToolkit().setDynamicLayout(true);
System.setProperty("sun.awt.noerasebackground","true");

try {
javax.swing.plaf.metal.MetalLookAndFeel.setCurrentTheme( new javax.swing.plaf.metal.DefaultMetalTheme());
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
}
catch ( UnsupportedLookAndFeelException e ) {
System.out.println ("Metal Look & Feel not supported on this platform. \nProgram Terminated");
System.exit(0);
}
catch ( IllegalAccessException e ) {
System.out.println ("Metal Look & Feel could not be accessed. \nProgram Terminated");
System.exit(0);
}
catch ( ClassNotFoundException e ) {
System.out.println ("Metal Look & Feel could not found. \nProgram Terminated");
System.exit(0);
}
catch ( InstantiationException e ) {
System.out.println ("Metal Look & Feel could not be instantiated. \nProgram Terminated");
System.exit(0);
}
catch ( Exception e ) {
System.out.println ("Unexpected error. \nProgram Terminated");
e.printStackTrace();
System.exit(0);
}

JFrame jf=new JFrame("arron");
jf.setContentPane(new MyPanel());
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jf.setLocation(200,200);
jf.pack();
jf.show();

}
}
j2sdk1.4.0-rc下调试通过,实现你所说的。
你的restore是这样的,每选一种色,就使originalcolor变为该色,当你按restore时当然还是使用当前色啦!
Ljglory 2002-03-06
  • 打赏
  • 举报
回复
furarmy,不是你说的意思:

比如:我先按下了第一个按钮,背景变成黄色;我接着按restore按钮时也不起作用!
furarmy 2002-03-06
  • 打赏
  • 举报
回复
当然不起作用了。当初得到的颜色originalcolor,不管是什么,当你按下restorebtn时,又被你设为背景色了。本来就是背景色,你又弄了一边,看起来当然没有效果。

62,629

社区成员

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

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