简体   繁体   English

在 java swing 中绘制矩形时出错

[英]error with drawing rectangle in java swing

The program compiles but I can't see the rectangle on the window, Can someone help me, I am new to this.程序可以编译,但我在 window 上看不到矩形,有人可以帮帮我,我是新手。 My goal is just to draw three rectangles on the window.我的目标只是在 window 上绘制三个矩形。 for a traffic light program.用于交通信号灯程序。

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();
}
}
  • Don't extend Canvas (or even use it), but do extend JPanel .不要扩展Canvas (甚至使用它),但要扩展JPanel
  • add the JPanel to the JFrame - ( win.add(this) )JPanel添加到JFrame - ( win.add(this) )
  • Your button's are filling the panel, hiding the background.您的按钮正在填充面板,隐藏背景。 Give them a size给他们一个尺寸
  • Add them to the JPanel just using add(b1) etc只需使用add(b1)等将它们添加到JPanel
  • Don't override paint , but do override paintComponent .不要覆盖paint ,但要覆盖paintComponent And do it as follows:并执行以下操作:
@Override
public void paintComponent(Graphics g) {
   super.paintComponent(g);
   // your stuff here
  • Don't set the size of the JFrame .不要设置JFrame的大小。 Set the size of the JPanel .设置JPanel的大小。 Otherwise your JFrame border absorbs some of the size, making your panel smaller than you may want.否则,您的JFrame边框会吸收一些尺寸,使您的面板比您想要的要小。 Do it as follows.请按以下方式进行。
@Override
public Dimension getPreferredSize() {
   return new Dimension(500,500);
}

You still have other logic to work out but this should get you started.您还有其他逻辑要解决,但这应该可以帮助您入门。

Style corrections样式更正

These are not critical to the execution of your code but important to learn.这些对代码的执行并不重要,但对学习很重要。

  • By convention, classes begin with an upper case character.按照惯例,类以大写字符开头。
  • Use the class name, not an instance when referring to static values.在引用 static 值时,使用 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();



}





}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM