Java dialog box (a dialog box pops up, and the string entered in the dialog box will be displayed in the text field)

From , 5 Years ago, written in Java, viewed 134 times.
URL https://pastebin.vip/view/6a10bbd4
  1. import java.awt.FlowLayout;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4.  
  5. import javax.swing.JButton;
  6. import javax.swing.JDialog;
  7. import javax.swing.JFrame;
  8. import javax.swing.JLabel;
  9. import javax.swing.JPanel;
  10. import javax.swing.JScrollPane;
  11. import javax.swing.JTextArea;
  12. import javax.swing.JTextField;
  13.  
  14. /**
  15.  * 使用对话框。 功能介绍:界面包括一个文本域,一个按钮,点击按钮弹出一个对话框, 在对话框中输入的字符串将在文本域中显示。
  16.  */
  17. public class DialogWindow extends JFrame implements ActionListener {
  18.  
  19.         private SimpleDialog dialog;
  20.         private JTextArea textArea;
  21.         // 文本域中行之间的分隔符
  22.         String lineSeparator;
  23.  
  24.         public DialogWindow() {
  25.                 super("对话框示例");
  26.  
  27.                 // 添加一个不可修改的文本域,能显示5行30个字符的内容。
  28.                 textArea = new JTextArea(5, 30);
  29.                 textArea.setEditable(false);
  30.                 getContentPane().add("Center", new JScrollPane(textArea));
  31.  
  32.                 // 添加一个按钮,点击按钮弹出对话框
  33.                 JButton button = new JButton("添加内容");
  34.                 button.addActionListener(this);
  35.                 JPanel panel = new JPanel();
  36.                 panel.add(button);
  37.                 getContentPane().add("South", panel);
  38.                 // 获取文本域中行之间的分隔符。这里调用了系统的属性
  39.                 lineSeparator = System.getProperty("line.separator");
  40.  
  41.                 // 调整窗体布局大小
  42.                 this.pack();
  43.         }
  44.  
  45.         public void actionPerformed(ActionEvent event) {
  46.                 // 点击按钮时显示对话框
  47.                 if (dialog == null) {
  48.                         dialog = new SimpleDialog(this, "输入对话框");
  49.                 }
  50.                 dialog.setVisible(true);
  51.         }
  52.  
  53.         /**
  54.          * 添加内容到文本域的后面,每次都新起一行。
  55.          */
  56.         public void setText(String text) {
  57.                 textArea.append(text + lineSeparator);
  58.         }
  59.  
  60.         public static void main(String args[]) {
  61.                 DialogWindow window = new DialogWindow();
  62.                 window.setVisible(true);
  63.                 window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  64.         }
  65. }
  66.  
  67. /**
  68.  * 自定义对话框 对话框包括一个label、一个文本框和2个按钮。
  69.  */
  70. class SimpleDialog extends JDialog implements ActionListener {
  71.  
  72.         // 文本框,用于输入字符串
  73.         JTextField field;
  74.         // 对话框的父窗体。
  75.         DialogWindow parent;
  76.         // “确定”按钮
  77.         JButton setButton;
  78.  
  79.         /**
  80.          * 构造函数,参数为父窗体和对话框的标题
  81.          */
  82.         SimpleDialog(JFrame prentFrame, String title) {
  83.                 // 调用父类的构造函数,
  84.                 // 第三个参数用false表示允许激活其他窗体。为true表示不能够激活其他窗体
  85.                 super(prentFrame, title, false);
  86.                 parent = (DialogWindow) prentFrame;
  87.  
  88.                 // 添加Label和输入文本框
  89.                 JPanel p1 = new JPanel();
  90.                 JLabel label = new JLabel("请输入要添加的文本:");
  91.                 p1.add(label);
  92.                 field = new JTextField(30);
  93.                 field.addActionListener(this);
  94.                 p1.add(field);
  95.                 getContentPane().add("Center", p1);
  96.  
  97.                 // 添加确定和取消按钮
  98.                 JPanel p2 = new JPanel();
  99.                 p2.setLayout(new FlowLayout(FlowLayout.RIGHT));
  100.                 JButton cancelButton = new JButton("取 消");
  101.                 cancelButton.addActionListener(this);
  102.                 setButton = new JButton("确 定");
  103.                 setButton.addActionListener(this);
  104.                 p2.add(setButton);
  105.                 p2.add(cancelButton);
  106.                 getContentPane().add("South", p2);
  107.  
  108.                 // 调整对话框布局大小
  109.                 pack();
  110.         }
  111.  
  112.         /**
  113.          * 事件处理
  114.          */
  115.         public void actionPerformed(ActionEvent event) {
  116.  
  117.                 Object source = event.getSource();
  118.                 if ((source == setButton)) {
  119.                         // 如果确定按钮被按下,则将文本矿的文本添加到父窗体的文本域中
  120.                         parent.setText(field.getText());
  121.                 }
  122.                 field.selectAll();
  123.                 // 隐藏对话框
  124.                 setVisible(false);
  125.         }
  126. }

Reply to "Java dialog box (a dialog box pops up, and the string entered in the dialog box will be displayed in the text field)"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website