簡體   English   中英

在 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);
}

您還有其他邏輯要解決,但這應該可以幫助您入門。

樣式更正

這些對代碼的執行並不重要,但對學習很重要。

  • 按照慣例,類以大寫字符開頭。
  • 在引用 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