在 java swing 中绘制矩形时出错

error with drawing rectangle in java swing

程序编译但我看不到 window 上的矩形,有人可以帮助我吗,我是新手。 我的目标只是在 window 上绘制三个矩形。用于红绿灯程序。

import java.io.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.Color;
import java.awt.Canvas;
import java.lang.String;
import java.awt.Graphics;

class traffic extends Canvas implements ActionListener{
static JRadioButton b1,b2,b3;
  static JPanel jp = new JPanel();
static JFrame win= new JFrame("Traffic light");
traffic(){
  b1= new JRadioButton("red");
  b2= new JRadioButton("green");
  b3= new JRadioButton("yellow");
  jp.add(b1);
  jp.add(b2);
  jp.add(b3);

  win.add(jp);
    win.setLayout(new FlowLayout());
  win.setSize(500,500);
  win.setVisible(true);

  win.setDefaultCloseOperation(win.DISPOSE_ON_CLOSE);
  b1.addActionListener(this);
  b2.addActionListener(this);
  b3.addActionListener(this);

}
public void actionPerformed(ActionEvent e) throws ArithmeticException
{ }
public void paint(Graphics g){

   g.setColor(Color.RED);
      g.fillRect(130, 30,100, 80);
}
public static void main(String[] args)
{    traffic tr= new traffic();
     tr.repaint();
}
}
  • 不要扩展 Canvas(甚至使用它),但一定要扩展 JPanel
  • JPanel 添加到 JFrame - (win.add(this))
  • 您的按钮正在填充面板,隐藏了背景。给他们一个尺码
  • 只需使用 add(b1) 等将它们添加到 JPanel
  • 不要覆盖 paint,但要覆盖 paintComponent。并按如下方式进行:
@Override
public void paintComponent(Graphics g) {
   super.paintComponent(g);
   // your stuff here
  • 不要设置 JFrame 的大小。设置 JPanel 的大小。否则,您的 JFrame 边框会吸收一些尺寸,使您的面板比您想要的要小。如下操作。
@Override
public Dimension getPreferredSize() {
   return new Dimension(500,500);
}

您还有其他逻辑需要解决,但这应该可以帮助您入门。

样式更正

这些对于代码的执行并不重要,但学习起来很重要。

  • 按照惯例,classes 以大写字符开头。
  • 在引用静态值时使用 class 名称,而不是实例。
win.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
import java.io.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.Color;
import java.awt.Canvas;
import java.lang.String;
import java.awt.Graphics;

  class traffic extends JPanel implements ActionListener{
static JRadioButton b1,b2,b3;
static JLabel l1;
traffic(){

JFrame win= new JFrame("Traffic light");
  l1= new JLabel("my name");
  b1= new JRadioButton("red");
  b2= new JRadioButton("green");
  b3= new JRadioButton("yellow");
  this.getPreferredSize();
  l1.setBounds(40,100,60,50);
  win.setSize(500,500);
  b1.setBounds(70,100,60,50);
  b2.setBounds(150,100,60,50);
  b3.setBounds(140,150,60,50);
  this.add(b1);
  this.add(b2);
  this.add(b3);
  this.add(l1);
  win.add(this);

  win.setVisible(true);
  win.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  b1.addActionListener(this);
  b2.addActionListener(this);
  b3.addActionListener(this);

}
@Override
public Dimension getPreferredSize() {
   return new Dimension(500,500);
}
public void actionPerformed(ActionEvent e)
{     if(e.getSource()==b1)
  {     b2.setSelected(false);
        b3.setSelected(false);
        this.repaint();
  }
     else if(e.getSource()==b2)
     { this.repaint();
       b1.setSelected(false);
       b3.setSelected(false);
     }

     else if(e.getSource()==b3)
     {  this.repaint();
       b1.setSelected(false);
       b2.setSelected(false);
     }

}
@Override
public void paintComponent(Graphics g) {
   super.paintComponent(g);
 if(b1.isSelected())
 {
   g.setColor(Color.RED);
   g.fillRect(150, 60,100, 100);
 }
 else if(b2.isSelected())
 { g.setColor(Color.GREEN);
  g.fillRect(150, 60,100, 100);
}
else if(b3.isSelected())
{
  g.setColor(Color.YELLOW);
  g.fillRect(150, 60,100, 100);

}
else{
  g.setColor(Color.WHITE);
  g.fillRect(150, 60,100, 100);
}
}
public static void main(String[] args)
{
      traffic tr= new traffic();



}





}