关于Java核心编程中的一个例子----设置JPanel的背景色 当然不起作用了。当初得到的颜色originalcolor,不管是什么,当你按下restorebtn时,又被你设为背景色了。本来就是背景色,你又弄了一边,看起来当然没有效果。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 furarmy,不是你说的意思:比如:我先按下了第一个按钮,背景变成黄色;我接着按restore按钮时也不起作用! 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时当然还是使用当前色啦! 我知道了,昨天晚上下楼时突然省悟过来了,应该把originalcolor = getBackground();放在构造函数中。谢谢两位! java io 问题(j2se) 正则表达式原理 关于大括号的问题 高人帮忙看看代码 能取得jLabel中的图像开始的坐标吗? 求一个结果 大家一想来总结!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 字符串转化问题,急!! 在java中调用javac.exe java好在何处 **请大家帮忙看一下:一个判断string是否为空的奇怪问题** 怎么写个bean啊?
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时当然还是使用当前色啦!
originalcolor = getBackground();放在构造函数中。谢谢两位!