zhangszdaoke 2011-09-21 14:12
浏览 390
已采纳

一个简单的java图形绘制

题目大意:做一个简单的交通灯,要求框架里有三个单选按钮(red,green,yellow),一组交通指示灯(三个灯泡)。选择单选按钮后,相应的灯会亮,一次只能亮一种灯。
以下是我的程序,执行以后面板上有按钮,可就是没有图像,求各位指点啊
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TrafficLight extends JFrame
{
PaintPanel paintPanel=new PaintPanel();//申请画图面板,按钮面板和三个单选按钮
JPanel buttons=new JPanel();
JRadioButton red=new JRadioButton("red");
JRadioButton green=new JRadioButton("green");
JRadioButton yellow=new JRadioButton("yellow");
TrafficLight()
{
ButtonGroup ni=new ButtonGroup();//将单选按钮加入按钮组
ni.add(red);
ni.add(green);
ni.add(yellow);
buttons.setLayout(new FlowLayout());//将单选按钮加入按钮面板
buttons.add(red);
buttons.add(green);
buttons.add(yellow);
setLayout(new BorderLayout());//将按钮面板和画图面板加入框架
add(paintPanel,BorderLayout.NORTH);
add(buttons,BorderLayout.SOUTH );
red.addActionListener(new ActionListener(){//为三个单选按钮设置监听
public void actionPerformed(ActionEvent e){repaint();}});
yellow.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){repaint();}});

green.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){repaint();}});
}
public class PaintPanel extends JPanel
{
protected void paintComponent(Graphics g)
{
super.paintComponents(g);
g.drawRect(getWidth()/2-10,20, 20, 60);//按要求绘制交通灯
if(!red.isSelected())
g.drawOval(getWidth()/2,30,15,15);//亮红灯
else
g.fillOval(getWidth()/2,30,15,15);//熄灭红灯
if(!green.isSelected())
g.drawOval(getWidth()/2,50,15,15);//亮绿灯
else
g.fillOval(getWidth()/2,50,15,15);//熄灭绿灯
if(!yellow.isSelected())
g.drawOval(getWidth()/2,70,15,15);//亮黄灯
else
g.fillOval(getWidth()/2,70,15,15);//熄灭黄灯
}

}
public static void main(String [] args)//主函数
{
    TrafficLight mine=new TrafficLight();
    mine.setTitle("TrafficLight");
    mine.setSize(800,500);
    mine.setVisible(true);
}

}

  • 写回答

1条回答 默认 最新

  • _1_1_7_ 2011-09-21 15:15
    关注

    [code="java"]

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    public class TrafficLight extends JFrame {
    PaintPanel paintPanel = new PaintPanel();// 申请画图面板,按钮面板和三个单选按钮
    JPanel buttons = new JPanel();
    JRadioButton red = new JRadioButton("red", true);
    JRadioButton green = new JRadioButton("green");
    JRadioButton yellow = new JRadioButton("yellow");

    TrafficLight() {
        paintPanel.setPreferredSize(new Dimension(600, 400));
        ButtonGroup ni = new ButtonGroup();// 将单选按钮加入按钮组
        ni.add(red);
        ni.add(green);
        ni.add(yellow);
        buttons.setLayout(new FlowLayout());// 将单选按钮加入按钮面板
        buttons.add(red);
        buttons.add(green);
        buttons.add(yellow);
        setLayout(new BorderLayout());// 将按钮面板和画图面板加入框架
        add(paintPanel, BorderLayout.NORTH);
        add(buttons, BorderLayout.SOUTH);
        red.addActionListener(new ActionListener() {// 为三个单选按钮设置监听
                    public void actionPerformed(ActionEvent e) {
                        repaint();
                    }
                });
        yellow.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                repaint();
            }
        });
        green.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                repaint();
            }
        });
    }
    
    public class PaintPanel extends JPanel {
        protected void paintComponent(Graphics g) {
            super.paintComponents(g);
            g.clearRect(0, 0, getWidth(), getHeight());
            g.drawRect(getWidth() / 2, 20, 30, 80);// 按要求绘制交通灯
            if (red.isSelected()) {
                lightRed(g);
            } else if (green.isSelected()) {
                lightGreen(g);
            } else
                lightYellow(g);
        }
    
    }
    
    private void lightRed(Graphics g) {
        g.setColor(Color.red);
        g.fillOval(getWidth() / 2, 30, 15, 15);
        g.setColor(Color.black);
        g.drawOval(getWidth() / 2, 50, 15, 15);
        g.drawOval(getWidth() / 2, 70, 15, 15);
    }
    
    private void lightGreen(Graphics g) {
        g.setColor(Color.green);
        g.fillOval(getWidth() / 2, 50, 15, 15);
        g.setColor(Color.black);
        g.drawOval(getWidth() / 2, 30, 15, 15);
        g.drawOval(getWidth() / 2, 70, 15, 15);
    }
    
    private void lightYellow(Graphics g) {
        g.setColor(Color.yellow);
        g.fillOval(getWidth() / 2, 70, 15, 15);
        g.setColor(Color.black);
        g.drawOval(getWidth() / 2, 30, 15, 15);
        g.drawOval(getWidth() / 2, 50, 15, 15);
    }
    
    public static void main(String[] args)// 主函数
    {
        TrafficLight mine = new TrafficLight();
        mine.setTitle("TrafficLight");
        mine.setSize(800, 600);
        mine.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mine.setVisible(true);
    }
    

    }
    [/code]

    代码还是有很多地方待改进

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 (标签-STM32|关键词-智能小车)
  • ¥20 关于#stm32#的问题,请各位专家解答!
  • ¥15 (标签-python)
  • ¥15 第一个已完成,求第二个做法
  • ¥20 搭建awx,试了很多版本都有错
  • ¥15 java corba的客户端该如何指定使用本地某个固定IP去连接服务端?
  • ¥15 activiti工作流问题,求解答
  • ¥15 有人写过RPA后台管理系统么?
  • ¥15 Bioage计算生物学年龄
  • ¥20 如何将FPGA Alveo U50恢复原来出厂设置哇?