我正在使用JButton的Action侦听器绘制不同的形状。它工作正常,但如何保持面板上以前绘制的形状?因为当另一个按钮按下以前的形状已经消失。显示JPanel上的所有绘制形状
jButton1.setText("Button1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("Button2");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
s = evt.getActionCommand();
repaint();
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
s = evt.getActionCommand();
repaint();
}
.......和方法的paintComponent是
protected void paintComponent(Graphics g) {
super.paintComponent(g);
System.out.println("====>>> " + s);
switch (s) {
case "Button1":
g.drawRoundRect(20,20,40,40,100,200);
break;
case "Button2":
g.drawRect(0, 0, 200, 200);
break;
default:
g.drawOval(40, 40, 100, 100);
这里的String包含按下按钮字幕。
基本问题是,在每个绘画循环中,您需要将图形恢复到您需要的状态。也就是说,您将需要重新绘制您之前绘制(并希望保留)的任何东西 – MadProgrammer 2013-04-30 07:38:30