?? counterthreadapplet.java
字號(hào):
//CounterThreadApplet.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CounterThreadApplet extends JApplet implements Runnable
{
//控制記數(shù)文本框是否繼續(xù)記數(shù)。
private boolean countFlag=true;
//記數(shù)窗口已經(jīng)顯示的文本框數(shù)目。
private int number = 0;
private JPanel textPanel = new JPanel();
private Container contentPane;
private JFrame counterFrame;
public static final int WIDTH = 450;
public static final int HEIGHT = 350;
public void init()
{
//彈出記數(shù)窗口。
counterFrame = new JFrame();
counterFrame.setTitle("CounterThreadAppletFrame");
counterFrame.setSize(WIDTH, HEIGHT);
contentPane = counterFrame.getContentPane();
JPanel buttonPanel = new JPanel();
//增加記數(shù)文本框的按鈕。
addButton(buttonPanel, "Add",
new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
countFlag = true;
startCount();
}
}
);
//停止記數(shù)按鈕,并關(guān)閉窗口。
addButton(buttonPanel, "Close",
new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
stopCount();
}
}
);
contentPane.add(buttonPanel, BorderLayout.SOUTH);
counterFrame.show();
}
public void startCount()
{
//受窗口大小限制,控制能夠增加的文本框的數(shù)量為16個(gè)。
if(number >= 16)
{
return;
}
number++;
//生成新線程。
Thread counterRunner = new Thread(this);
counterRunner.start();
}
public void stopCount()
{
if (countFlag)
{
countFlag = false;
}
showStatus("stop counting!");
//關(guān)閉窗口
counterFrame.dispose();
}
public void run()
{
TextField textField = new TextField(20);
textPanel.add(textField);
contentPane.add(textPanel, BorderLayout.NORTH);
counterFrame.show();
//循環(huán)50次,每次遞增1。
for (int i = 0; i < 50; i++)
{
try
{
if(countFlag)
textField.setText(Integer.toString(i));
//暫時(shí)停止當(dāng)前線程50毫秒。
Thread.sleep(50);
}
catch (InterruptedException e)
{
}
}
}
public void addButton(Container c, String title, ActionListener
listener)
{
JButton button = new JButton(title);
c.add(button);
button.addActionListener(listener);
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -