1
我是新來的java,我想做一個矩形,使用掃描儀的用戶輸入來獲得矩形的大小。問題是它需要用戶輸入,但不顯示矩形。我相信這是因爲我的整數是在一個靜態函數,但我不知道如何解決這個問題。我搜索了很長時間的谷歌,但我找不到答案。任何人都可以幫我嗎?謝謝。 :)我想做一個矩形,需要一個用戶inut得到一個大小
import java.util.Scanner;
import java.awt.*;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Shape extends JPanel implements ActionListener{
Timer tm = new Timer(5, this);
public static void main(String[] args){
System.out.println("Place in the width of your vaccum cleaner here:");
Scanner myY = new Scanner(System.in);
int y = myY.nextInt();
JFrame jf = new JFrame("Title");
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Shape s = new Shape();
jf.add(s);
jf.setSize(600, 400);
jf.setVisible(true);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
this.setBackground(Color.PINK);
g.setColor(Color.BLACK);
g.fillRect(0, 0, 40, y);
tm.start();
}
}
謝謝,讚賞。 – user62