所以我有这个代码:
public class myPanel extends JPanel implements ActionListener{
int x = 0;
JButton myButton = new JButton("Click me");
public myPanel(){
super().setLayout(null);
myButton.setBounds(50,50,50,50);
super().add(myButton);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
if(x==1)
g.drawString("hi",10,10);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==myButton){
x=1;
//Do something here
}
}
}
如何再次调用paintComponent?假设当我按下按钮时,它会再次调用 PaintComponent 并在检查整数 x 是否等于 1 后绘制字符串“Hi”?先感谢您!!!对 Java 还很陌生,你们能像向 10 岁的 child 一样解释一下吗?
最佳答案
调用repaint()
,它会再次绘制它。
关于java - 如何重新绘制/再次调用paintComponent?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26085124/