java 红绿灯 为什么我用repaint()图像就显示不出来 10
importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassTrafficLightex...
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TrafficLight extends JFrame implements ActionListener {
private JRadioButton jrbRed,jrbYellow,jrbGreen;
private LightPanel light=new LightPanel();
public TrafficLight(){
getContentPane().setLayout(new BorderLayout());
JPanel jpRadioButtons=new JPanel();
jpRadioButtons.setLayout(new GridLayout(1,3));
jpRadioButtons.add(jrbRed=new JRadioButton("红灯"));
jpRadioButtons.add(jrbYellow=new JRadioButton("黄灯"));
jpRadioButtons.add(jrbGreen=new JRadioButton("绿灯"));
ButtonGroup group=new ButtonGroup();
group.add(jrbRed);
group.add(jrbYellow);
group.add(jrbGreen);
jrbRed.addActionListener(this);
jrbYellow.addActionListener(this);
jrbGreen.addActionListener(this);
getContentPane().add(jpRadioButtons,BorderLayout.SOUTH);
getContentPane().add(light);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//light.repaint();
if(e.getSource()==jrbRed){
light.ShowRed();
light.repaint();
}
if(e.getSource()==jrbYellow) {
light.ShowYellow();
light.repaint();
}
if(e.getSource()==jrbGreen){
light.ShowGreen();
light.repaint();
}
}
public static void main(String[]args){
TrafficLight frame=new TrafficLight();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
}
}
class LightPanel extends JPanel{
private Graphics g;
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.drawRect(getWidth()/2-25, getHeight()/2-65, 50, 140);
g.drawOval(getWidth()/2-20, getHeight()/2-60, 40, 40);
g.drawOval(getWidth()/2-20, getHeight()/2-15, 40, 40);
g.drawOval(getWidth()/2-20, getHeight()/2+30, 40, 40);
}
public void ShowRed(){
//repaint();
g=getGraphics();
g.setColor(Color.RED);
g.fillOval(getWidth()/2-20, getHeight()/2-60, 40, 40);
}
public void ShowYellow(){
g=getGraphics();
g.setColor(Color.yellow);
g.fillOval(getWidth()/2-20, getHeight()/2-15, 40, 40);
}
public void ShowGreen(){
g=getGraphics();
g.setColor(Color.green);
g.fillOval(getWidth()/2-20, getHeight()/2+30, 40, 40);
}
} 展开
import java.awt.event.*;
import javax.swing.*;
public class TrafficLight extends JFrame implements ActionListener {
private JRadioButton jrbRed,jrbYellow,jrbGreen;
private LightPanel light=new LightPanel();
public TrafficLight(){
getContentPane().setLayout(new BorderLayout());
JPanel jpRadioButtons=new JPanel();
jpRadioButtons.setLayout(new GridLayout(1,3));
jpRadioButtons.add(jrbRed=new JRadioButton("红灯"));
jpRadioButtons.add(jrbYellow=new JRadioButton("黄灯"));
jpRadioButtons.add(jrbGreen=new JRadioButton("绿灯"));
ButtonGroup group=new ButtonGroup();
group.add(jrbRed);
group.add(jrbYellow);
group.add(jrbGreen);
jrbRed.addActionListener(this);
jrbYellow.addActionListener(this);
jrbGreen.addActionListener(this);
getContentPane().add(jpRadioButtons,BorderLayout.SOUTH);
getContentPane().add(light);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//light.repaint();
if(e.getSource()==jrbRed){
light.ShowRed();
light.repaint();
}
if(e.getSource()==jrbYellow) {
light.ShowYellow();
light.repaint();
}
if(e.getSource()==jrbGreen){
light.ShowGreen();
light.repaint();
}
}
public static void main(String[]args){
TrafficLight frame=new TrafficLight();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
}
}
class LightPanel extends JPanel{
private Graphics g;
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.drawRect(getWidth()/2-25, getHeight()/2-65, 50, 140);
g.drawOval(getWidth()/2-20, getHeight()/2-60, 40, 40);
g.drawOval(getWidth()/2-20, getHeight()/2-15, 40, 40);
g.drawOval(getWidth()/2-20, getHeight()/2+30, 40, 40);
}
public void ShowRed(){
//repaint();
g=getGraphics();
g.setColor(Color.RED);
g.fillOval(getWidth()/2-20, getHeight()/2-60, 40, 40);
}
public void ShowYellow(){
g=getGraphics();
g.setColor(Color.yellow);
g.fillOval(getWidth()/2-20, getHeight()/2-15, 40, 40);
}
public void ShowGreen(){
g=getGraphics();
g.setColor(Color.green);
g.fillOval(getWidth()/2-20, getHeight()/2+30, 40, 40);
}
} 展开
1个回答
展开全部
repaint()会调用paintComponent方法,而super.paintComponent(g);这句会清空组件
所以你的代码最后什么都不会变
给你改了一下,请参考
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TrafficLight extends JFrame implements ActionListener {
private JRadioButton jrbRed,jrbYellow,jrbGreen;
private LightPanel light=new LightPanel();
public TrafficLight(){
getContentPane().setLayout(new BorderLayout());
JPanel jpRadioButtons=new JPanel();
jpRadioButtons.setLayout(new GridLayout(1,3));
jpRadioButtons.add(jrbRed=new JRadioButton("红灯"));
jpRadioButtons.add(jrbYellow=new JRadioButton("黄灯"));
jpRadioButtons.add(jrbGreen=new JRadioButton("绿灯"));
ButtonGroup group=new ButtonGroup();
group.add(jrbRed);
group.add(jrbYellow);
group.add(jrbGreen);
jrbRed.addActionListener(this);
jrbYellow.addActionListener(this);
jrbGreen.addActionListener(this);
getContentPane().add(jpRadioButtons,BorderLayout.SOUTH);
getContentPane().add(light);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//light.repaint();
if(e.getSource()==jrbRed){
light.ShowRed();
}
if(e.getSource()==jrbYellow) {
light.ShowYellow();
}
if(e.getSource()==jrbGreen){
light.ShowGreen();
}
}
public static void main(String[]args){
TrafficLight frame=new TrafficLight();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
}
}
class LightPanel extends JPanel{
int light_num = -1;
Color[] colors = {Color.RED, Color.YELLOW, Color.GREEN};
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.drawRect(getWidth()/2-25, getHeight()/2-65, 50, 140);
for(int i = 0; i < 3; i++) {
if(i==light_num) {
g.setColor(colors[i]);
g.fillOval(getWidth()/2-20, getHeight()/2-60+i*45, 40, 40);
}
else {
g.setColor(Color.BLACK);
g.drawOval(getWidth()/2-20, getHeight()/2-60+i*45, 40, 40);
}
}
}
public void ShowRed(){
light_num = 0;
repaint();
}
public void ShowYellow(){
light_num = 1;
repaint();
}
public void ShowGreen(){
light_num = 2;
repaint();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询