当前位置: 技术问答>java相关
运行这个application,窗体中会莫名其妙的出现无法点击的按键.
来源: 互联网 发布时间:2015-01-16
本文导语: 我在jdk1.3.1和jdk1.3.0_02的环境下分别编译和运行以下程序.除了自己添加的Start和Stop2个按键,在窗体的左上角会出现对点击没有反应的1个按键,可程序中并没有代码添加此按键.不知是什么原因? //文件名 timerTest.java //java...
我在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();
}
}
//文件名 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();
}
}
|
super.paintComponent(g);//Add this line
swing 的paintComponent重载都要先调用super.paintComponent(g);
swing 的paintComponent重载都要先调用super.paintComponent(g);