[英]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);
}
您还有其他逻辑要解决,但这应该可以帮助您入门。
样式更正
这些对代码的执行并不重要,但对学习很重要。
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.