📄 exercise19_1.java
字号:
// Exercise19_1.java: Display output from three threads to text area
import javax.swing.*;
public class Exercise19_1 extends JFrame {
private JTextArea jta = new JTextArea();
public static void main(String[] args) {
Exercise19_1 frame = new Exercise19_1();
frame.setTitle("Exercise19_1");
frame.setSize(200, 200);
frame.setVisible(true);
}
public Exercise19_1() {
JScrollPane jsp = new JScrollPane(jta);
getContentPane().add(jsp);
jta.setLineWrap(true);
PrintChar printA = new PrintChar('a', 100);
PrintChar printB = new PrintChar('b', 100);
PrintNum print100 = new PrintNum(100);
// Start threads
print100.start();
printA.start();
printB.start();
}
//The thread class for printing a specified character in specified times
class PrintChar extends Thread {
private char charToPrint; //the character to print
private int times; //the times to repeat
//The thread class constructor
public PrintChar(char c, int t) {
charToPrint = c;
times = t;
}
//override the run() method to tell the system what the thread will do
public void run() {
for (int i=1; i < times; i++)
jta.append(charToPrint+"");
}
}
//The thread class for printing number from 1 to n for a given n.
class PrintNum extends Thread {
private int lastNum;
public PrintNum(int i) {
lastNum = i;
}
public void run() {
for (int i=1; i <= lastNum; i++)
jta.append(" "+i);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -