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]

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

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

报告相同问题?

悬赏问题

  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证