📄 dialogwindow.java
字号:
import java.awt.*;import java.awt.event.*;public class DialogWindow extends Frame implements ActionListener { private SimpleDialog dialog; private TextArea textArea; String newline; public DialogWindow() { textArea = new TextArea(5, 40); textArea.setEditable(false); add("Center", textArea); Button button = new Button("打开对话框"); button.addActionListener(this); add("South", button); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); newline = System.getProperty("line.separator");//获取行分隔符。 } //响应点击"打开对话框"按钮。 public void actionPerformed(ActionEvent event) { if (dialog == null) { dialog = new SimpleDialog(this, "A Simple Dialog"); } dialog.setVisible(true); } //设置文本区textArea的内容 。 public void setText(String text) { textArea.append(text + newline); } public static void main(String args[]) { DialogWindow window = new DialogWindow(); window.setTitle("DialogWindow Application"); window.pack(); window.setVisible(true); }}//实现一个简单对话框的类。class SimpleDialog extends Dialog implements ActionListener { TextField field; DialogWindow parent; Button setButton; SimpleDialog(Frame dw, String title) { super(dw, title, false); parent = (DialogWindow)dw; //创建标签与文本域。 Panel p1 = new Panel(); Label label = new Label("请随意输入文字:"); p1.add(label); field = new TextField(20); field.addActionListener(this); //响应用户回车操作。 p1.add(field); add("Center", p1); //创建两个按钮. Panel p2 = new Panel(); p2.setLayout(new FlowLayout(FlowLayout.RIGHT)); Button b = new Button("Cancel"); b.addActionListener(this); setButton = new Button("Set"); setButton.addActionListener(this); p2.add(b); p2.add(setButton); add("South", p2); //将对话框设置为最佳大小。 pack(); } //响应点击两个按钮和文本域回车操作。 public void actionPerformed(ActionEvent event) { Object source = event.getSource(); if ( (source == setButton) | (source == field)) { parent.setText(field.getText()); } field.selectAll(); setVisible(false); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -