public T2() { super(title:"多线程示例--每隔1秒刷新显示当前时间"); setLayout(new FlowLayout()); setDefaultClose0peration(JFrame.EXIT_ON_CLOSE); add(lbl1); add(txt); }
时间: 2023-12-06 13:42:44 浏览: 93
这是一个 Java 中的 GUI 程序,使用了 Swing 库。其中 T2 是一个继承自 JFrame 类的窗口类。在构造函数中,首先调用了父类的构造函数设置窗口的标题,然后设置了窗口的布局为 FlowLayout。接着设置了窗口关闭时的默认操作,如果不设置,可能会导致程序在关闭窗口后仍然在后台运行。最后添加了一个标签(lbl1)和一个文本框(txt)到窗口中。
相关问题
We now want to add a new “get book” view that allows the user of the system to check how many books a specific user is borrowing. Create a ViewGetBook class that extends View<ControllerGetBook> and has the following UML specification: +------------------------------------------------+ | ViewGetBook | +------------------------------------------------+ | - t: JTextField | +------------------------------------------------+ | + ViewGetBook(Library m, ControllerGetBook c) | | + update(): void | +------------------------------------------------+ The ViewGetBook shows the text field called t (where the user can type text) and a button. Use a grid layout manager to position the two components
Below is the Java code for the ViewGetBook class that extends View<ControllerGetBook> and has the specified UML specification:
```
import javax.swing.*;
import java.awt.*;
public class ViewGetBook extends View<ControllerGetBook> {
private JTextField t;
public ViewGetBook(Library m, ControllerGetBook c) {
super(m, c);
this.setLayout(new GridLayout(2, 1));
t = new JTextField();
JButton b = new JButton("Get Books");
b.addActionListener(e -> controller.process());
this.add(t);
this.add(b);
this.setSize(300, 100);
this.setVisible(true);
}
public void update() {
JOptionPane.showMessageDialog(this, model.getState());
}
}
```
In this implementation, the constructor sets the layout to a 2x1 grid and creates a JTextField and a JButton. The action listener for the button calls the process() method in the associated controller. The update() method displays a message dialog with the state of the model when called.
import java.swing.* import iava.art.* public class MyThread extends JFrane implements Thread JTextField output; public llyThreadO int i=0: TLabelnote; setTitle(”多线程示例” JTextField(10) output.setFont(new Font output=new Font. PLAIN, 20)) note-ner JLabel(”自增的数字: 虽体” setLayout(new FlovLayoutO) add(note add(output setSize(380.150) setVisible(true runner= new Thread( ) runner.RunO; Thread public rruno while(true try Thread.sleep(500);//休眠0.5秒 output.setText( if(i=10) +(1++)) JoptionPane.showDialog(nul1,“线程执行了“+i+"次”,“时间提醒”,JOptionPane.INFORMATION_MESScatch(IOException e) public static void nain(string arg[]) new lyThreadO
I'm sorry, but I cannot understand your question. The code you provided is not formatted correctly and seems to contain errors. Could you please rephrase your question or provide more context?
阅读全文