问题描述
- java的repaint()没有重绘,怎么回事呀。
-
package chapter17;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;public class showRadioButton extends JFrame{
/** * @param args */ showRadioButton(){ creatButtonPanel bp =new creatButtonPanel(); messagePanel mp = new messagePanel(); setLayout(new BorderLayout()); add(bp,BorderLayout.SOUTH); add(mp,BorderLayout.CENTER); messagePanel messagepanel = new messagePanel(); setSize(800,600); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } public static void main(String[] args) { // TODO 自动生成的方法存根 showRadioButton srb = new showRadioButton(); } //生成String,并操作String class messagePanel extends JPanel{ private String s = "Welcome to Java"; private boolean bl = true; private int x ; private int y ; // 第一次绘制图形 protected void paintComponent(Graphics g){ super.paintComponent(g); // 设置string的位置 if(bl == true){ FontMetrics fm = g.getFontMetrics(); int sw = fm.stringWidth(s)/2; int sh = fm.getAscent()/2; x = getWidth()/2 - sw; y = getHeight()/2 - sh; //开始绘制sting并且设置布尔值 g.drawString(s,x,y); bl = false; }else{ g.drawString(s,x,y); } } //重绘方法 protected void left(){ x = x - 50; repaint(); } public void right() { x = x + 50; repaint(); } } //绘制button按钮并出册事件 class creatButtonPanel extends JPanel{ public creatButtonPanel(){ JButton jbtl = new JButton("left"); JButton jbtr = new JButton("rigtht"); jbtl.setMnemonic('L'); jbtr.setMnemonic('R'); //showRadioButton srb = new showRadioButton(); add(jbtl); add(jbtr); jbtl.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ messagePanel mp = new messagePanel(); mp.left(); // 重要 外部类调用内部类 } }); jbtl.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ new messagePanel().right(); } }); //return buttonPanel; } }
}
解决方案
你对象是内部类的,超出这个作用域之后会直接销毁,那么你每次重绘的都是不一样的对象,而不是同一个,每次都新建对象,你将这个对象设置为全局的看一下
解决方案二:
java中panel.repaint()没有实时重绘的问题
解决方案三:
package chapter17;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class showRadioButton extends JFrame{
/**
* @param args
*/
showRadioButton(){
creatButtonPanel bp =new creatButtonPanel();
messagePanel mp = new messagePanel();
setLayout(new BorderLayout());
add(bp,BorderLayout.SOUTH);
add(mp,BorderLayout.CENTER);
messagePanel messagepanel = new messagePanel();
setSize(800,600);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
// TODO 自动生成的方法存根
showRadioButton srb = new showRadioButton();
}
//生成String,并操作String
class messagePanel extends JPanel{
private String s = "Welcome to Java";
private boolean bl = true;
private int xn ;
private int yn ;
private int q = 1;
// 第一次绘制图形
protected void paintComponent(Graphics g){
System.out.println("a");
super.paintComponent(g);
if(bl == true){
// 设置string的位置
FontMetrics fm = g.getFontMetrics();
int sw = fm.stringWidth(s)/2;
int sh = fm.getAscent()/2;
int x = getWidth()/2 - sw;
int y = getHeight()/2 - sh;
xn = x;
yn = y;
//开始绘制sting并且设置布尔值
g.drawString(s,xn,yn);
bl = false;
System.out.println(q++);
}else{
g.drawString(s,xn,yn);
}
}
//重绘方法
protected void left(){
xn = xn - 50;
repaint();
System.out.println("left" + q++);
}
public void right() {
xn = xn + 50;
repaint();
}
}
//绘制button按钮并出册事件
class creatButtonPanel extends JPanel{
public creatButtonPanel(){
JButton jbtl = new JButton("left");
JButton jbtr = new JButton("rigtht");
jbtl.setMnemonic('L');
jbtr.setMnemonic('R');
//showRadioButton srb = new showRadioButton();
add(jbtl);
add(jbtr);
jbtl.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
new messagePanel().left();
// 重要 外部类调用内部类
}
});
jbtl.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
new messagePanel().right();
}
});
//return buttonPanel;
}
}
}
时间: 2024-12-09 22:18:01