[英]Why does one have to use the paintComponent method to draw in Java?
[英]Java Why my method paintComponent() does not work?
package work;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.util.Timer;
import java.util.TimerTask;
class NewJPanel extends JPanel{
//private MoveButton button;
static MoveButton button;
JTextField textfield;
NewJPanel(){
setLayout(null);
textfield=new JTextField();
textfield.setBounds(600, 50, 100, 40);
button=new MoveButton();
//button.setBounds(0, 0, 50, 50);
//add(button);
add(textfield);
//repaint();
System.out.println("Newpanel construct");
//System.out.println("1");
//button=new MoveButton();
//button.addActionListener(button);
//add(button);
}
public static void main(String[] args){
JFrame gui=new JFrame();
gui.setSize(800, 800);
gui.setVisible(true);
NewJPanel panel=new NewJPanel();
//panel.setLayout(null);
panel.add(button);
gui.add(panel);
}
@Override public void paintComponent(Graphics g){
System.out.println("drawing");
super.paintComponent(g);
for(int i=0;i<400;i+=100)
for(int j=0;j<400;j+=100)
g.drawOval(i, j, 100, 100);
//button.setBounds(button.getx(),button.gety(),50,50);
textfield.setText(Integer.toString(button.get()));
}
}
class MoveButton extends JButton implements ActionListener{
private Random randomgenerator=new Random();
private int indexX=0;
private int indexY=0;
private int score=0;
public MoveButton(){
setBounds(0, 0, 50, 50);
addActionListener(this);
System.out.println("movebutton construct");
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//setBounds(200,0,100,100);
indexX=100*randomgenerator.nextInt(4);
indexY=100*randomgenerator.nextInt(4);
score+=1;
//System.out.println(indexX);
setBounds(indexX, indexY, 50, 50);
repaint();
}
public int getx(){
return indexX;
}
public int gety(){
return indexY;
}
public int get(){
return score;
}
}
class TimerTaskTest extends java.util.TimerTask{
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("Start!");
}
}
這是我的代碼,我嘗試在JPanel中繪制圓,我真的不明白為什么paintComponent不起作用。 誰能幫我嗎?
您的代碼可以正常工作-如果您可以在面板上觸發repaint()
事件。
問題是您正在主線程而不是EDT上初始化GUI。 因此,在將NewJPanel
添加到JFrame之前,先顯示JFrame。 並且僅添加具有固定布局的NewJPanel
不會觸發重新繪制事件。
您應該將主要方法更改為
public static void main(String[] args){
SwingUtilities.invokeLater(() -> {
JFrame gui=new JFrame();
gui.setSize(800, 800);
gui.setVisible(true);
NewJPanel panel=new NewJPanel();
//panel.setLayout(null);
panel.add(button);
gui.add(panel);
});
}
或者,如果您不能使用Java8:
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame gui=new JFrame();
gui.setSize(800, 800);
gui.setVisible(true);
NewJPanel panel=new NewJPanel();
//panel.setLayout(null);
panel.add(button);
gui.add(panel);
}
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.