当前位置: 技术问答>java相关
问题:repaint()重绘时原字符不消失
来源: 互联网 发布时间:2017-04-02
本文导语: 代码如下: import javax.swing.*; import java.awt.*; import java.awt.event.*; class KeyTest { public static void main(String[] args) { TestFrame frame = new TestFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.show(); } } class TestFra...
代码如下:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class KeyTest
{
public static void main(String[] args)
{
TestFrame frame = new TestFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class TestFrame extends JFrame
{
public TestFrame()
{
setTitle("KeyFrame");
setSize(WIDTH, HEIGHT);
TestPanel panel = new TestPanel();
Container content = getContentPane();
content.add(panel);
}
public static final int WIDTH = 250;
public static final int HEIGHT = 300;
}
class TestPanel extends JPanel
{
public TestPanel()
{
account = new BankAccount(1000, this);
account.start(5);
}
public void paintComponent(Graphics g)
{
super.paintComponents(g);
double testbanlance = account.getBanlance();
g.drawString(" is "+testbanlance, 20, 45);
}
// private static String str = "Your Banlance Now is";
private BankAccount account;
}
class BankAccount
{
public BankAccount(double abanlance, JPanel apane)
{
banlance = abanlance;
accountpane = (TestPanel)apane;
}
public void start(final double rate)
{
ActionListener add = new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
double interest = banlance * rate;
banlance += interest;
accountpane.repaint();
}
};
Timer t = new Timer(5000, add);
t.start();
}
public double getBanlance()
{
return banlance;
}
private double banlance;
private TestPanel accountpane;
}
用Timer过一段时间重绘,但原来的字符没有消失,而是与新的字符重叠起来了
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class KeyTest
{
public static void main(String[] args)
{
TestFrame frame = new TestFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class TestFrame extends JFrame
{
public TestFrame()
{
setTitle("KeyFrame");
setSize(WIDTH, HEIGHT);
TestPanel panel = new TestPanel();
Container content = getContentPane();
content.add(panel);
}
public static final int WIDTH = 250;
public static final int HEIGHT = 300;
}
class TestPanel extends JPanel
{
public TestPanel()
{
account = new BankAccount(1000, this);
account.start(5);
}
public void paintComponent(Graphics g)
{
super.paintComponents(g);
double testbanlance = account.getBanlance();
g.drawString(" is "+testbanlance, 20, 45);
}
// private static String str = "Your Banlance Now is";
private BankAccount account;
}
class BankAccount
{
public BankAccount(double abanlance, JPanel apane)
{
banlance = abanlance;
accountpane = (TestPanel)apane;
}
public void start(final double rate)
{
ActionListener add = new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
double interest = banlance * rate;
banlance += interest;
accountpane.repaint();
}
};
Timer t = new Timer(5000, add);
t.start();
}
public double getBanlance()
{
return banlance;
}
private double banlance;
private TestPanel accountpane;
}
用Timer过一段时间重绘,但原来的字符没有消失,而是与新的字符重叠起来了
|
先removeAll(),再drawString()。
|
public void paintComponent(Graphics g)
{
super.paintComponents(g);
double testbanlance = account.getBanlance();
g.drawString(" is "+testbanlance, 20, 45);
}
-->//你要注意paintComponent方法与paint方法不同的地方就是重画时是否清除原来所画的东西
public void paint(Graphics g)
{
super.paint(g);
double testbanlance = account.getBanlance();
g.drawString(" is "+testbanlance, 20, 45);
}
---------------------------
{
super.paintComponents(g);
double testbanlance = account.getBanlance();
g.drawString(" is "+testbanlance, 20, 45);
}
-->//你要注意paintComponent方法与paint方法不同的地方就是重画时是否清除原来所画的东西
public void paint(Graphics g)
{
super.paint(g);
double testbanlance = account.getBanlance();
g.drawString(" is "+testbanlance, 20, 45);
}
---------------------------