当前位置: 技术问答>java相关
关于Java核心编程中的一个例子----设置JPanel的背景色
来源: 互联网 发布时间:2015-04-11
本文导语: 原例是这样的:在一个JPanel放三个按钮,按下以后分别使背景色变为红色、蓝色和黄色。 我想给例子再加个按钮,按下后使背景色变为原来的颜色(好像就是灰色);可是按下总是不起作用。代码如下(仅JPanel类): ...
原例是这样的:在一个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();
}
}
我想给例子再加个按钮,按下后使背景色变为原来的颜色(好像就是灰色);可是按下总是不起作用。代码如下(仅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();
}
}
|
当然不起作用了。当初得到的颜色originalcolor,不管是什么,当你按下restorebtn时,又被你设为背景色了。本来就是背景色,你又弄了一边,看起来当然没有效果。
|
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时当然还是使用当前色啦!
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时当然还是使用当前色啦!