当前位置: 技术问答>java相关
一个关于背景颜色改变的问题
来源: 互联网 发布时间:2015-06-14
本文导语: 这是一个改变背景颜色的程序,但运行后颜色没改变阿? 请帮忙看看。谢谢 import java.awt.*; import java.awt.event.*; import javax.swing.*; class FirstPanel extends JPanel implements ActionListener { public FirstPanel() ...
这是一个改变背景颜色的程序,但运行后颜色没改变阿?
请帮忙看看。谢谢
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);
}
}
请帮忙看看。谢谢
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);
}
}
|
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);
}
}
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);
}
}
|
错误在于
private JButton redButton;
private JButton blueButton;
private JButton yellowButton;
然后构造器中再
JButton redButton = new JButton("Red");
JButton blueButton = new JButton("Blue");
JButton yellowButton = new JButton("Yellow");
明白 ? 一开始偶也被迷惑了. . . . .
private JButton redButton;
private JButton blueButton;
private JButton yellowButton;
然后构造器中再
JButton redButton = new JButton("Red");
JButton blueButton = new JButton("Blue");
JButton yellowButton = new JButton("Yellow");
明白 ? 一开始偶也被迷惑了. . . . .