运行这个application,窗体中会莫名其妙的出现无法点击的按键.

magus_yang 2001-07-19 01:12:30
我在jdk1.3.1和jdk1.3.0_02的环境下分别编译和运行以下程序.除了自己添加的Start和Stop2个按键,在窗体的左上角会出现对点击没有反应的1个按键,可程序中并没有代码添加此按键.不知是什么原因?


//文件名 timerTest.java
//javax.swing.Timer测试,实现一个进度条
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.*;

class controlPanel extends JPanel implements ActionListener
{
public controlPanel(Timer tmp)
{
t=tmp;

cmdStart=new JButton("Start");
cmdStop=new JButton("Stop");

add(cmdStart);
add(cmdStop);

cmdStart.addActionListener(this);
cmdStop.addActionListener(this);
}

public void actionPerformed(ActionEvent evt)
{
Object source=evt.getSource();
if(source==cmdStart)
t.start();
else
t.stop();
}

private JButton cmdStart;
private JButton cmdStop;
private Timer t;

}

class timerTestPanel extends JPanel implements ActionListener
{
public timerTestPanel()
{
int barLength=0;
}

public void actionPerformed(ActionEvent evt)
{
repaint();
}

public void paintComponent(Graphics g)
{
barLength++;
g.fillRect(0,0,barLength,barWidth);
}

private final int barWidth=20; //进度条的宽度.以象素为单位.
private int barLength; //进度条的长度,随时间的增加而增加.以象素为单位.

}

class timerTestFrame extends JFrame
{
public timerTestFrame()
{
setTitle("Timer(计时器)测试");
setSize(500,400);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});

timerTestPanel myTimerTestPanel=new timerTestPanel();
delay=500;
t = new Timer(delay,myTimerTestPanel);

controlPanel myControlPanel=new controlPanel(t);

Container contentPane=getContentPane();
contentPane.add(myTimerTestPanel,"Center");
contentPane.add(myControlPanel,"South");
}
private Timer t;
private int delay; //计时器的间隔时间,以毫秒为单位.
}

public class timerTest
{
public static void main(String[] args)
{
JFrame myTimerTest=new timerTestFrame();
myTimerTest.show();
}
}
...全文
79 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
magus_yang 2001-07-19
  • 打赏
  • 举报
回复
to masterz:
谢谢,确实就缺super.paintComponent(g);语句导致产生了这个怪问题.
masterz 2001-07-19
  • 打赏
  • 举报
回复
super.paintComponent(g);//Add this line
swing 的paintComponent重载都要先调用super.paintComponent(g);
bobosji 2001-07-19
  • 打赏
  • 举报
回复
能详细解说一下吗?
masterz 2001-07-19
  • 打赏
  • 举报
回复
class timerTestPanel extends JPanel implements ActionListener
{
public timerTestPanel()
{
int barLength=0;
}

public void actionPerformed(ActionEvent evt)
{
repaint();
}

public void paintComponent(Graphics g)
{
super.paintComponent(g);//Add this line
barLength++;
g.fillRect(0,0,barLength,barWidth);
}

private final int barWidth=20; //??????.??????.
private int barLength; //??????,?????????.??????.

}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧