• java 数据传递? 两个对话框之间


    简介

    java 数据传递,简单通过类对象传递?
    两个对话框之间

    Q&A

    但是不知道是如何实现模态的对话框的,看了几遍也还不知道?
    JDialog 第二个参数就是模态和非模态的选择

    code

    package dataExchange;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    /**
     * A frame with a menu whose File->Connect action shows a password dialog.
     */
    public class DataExchangeFrame extends JFrame {
       public static final int TEXT_ROWS = 20;
       public static final int TEXT_COLUMNS = 40;
       private PasswordChooser dialog = null;
       private JTextArea textArea;
    
       public DataExchangeFrame() {
          // construct a File menu
    
          var mbar = new JMenuBar();
          setJMenuBar(mbar);
          var fileMenu = new JMenu("File");
          mbar.add(fileMenu);
    
          // add Connect and Exit menu items
    
          var connectItem = new JMenuItem("Connect");
          connectItem.addActionListener(new ConnectAction());
          fileMenu.add(connectItem);
    
          // the Exit item exits the program
    
          var exitItem = new JMenuItem("Exit");
          exitItem.addActionListener(event -> System.exit(0));
          fileMenu.add(exitItem);
    
          textArea = new JTextArea(TEXT_ROWS, TEXT_COLUMNS);
          add(new JScrollPane(textArea), BorderLayout.CENTER);
          pack();
       }
    
       /**
        * The Connect action pops up the password dialog.
        */
       private class ConnectAction implements ActionListener {
          public void actionPerformed(ActionEvent event) {
             // if first time, construct dialog
    
             if (dialog == null)
                dialog = new PasswordChooser();
    
             // set default values
             dialog.setUser(new User("yourname", null));
    
             // pop up dialog
             if (dialog.showDialog(DataExchangeFrame.this, "Connect")) {
                // if accepted, retrieve user input
                User u = dialog.getUser();
                textArea.append("user name = " + u.getName() + ", password = " + (new String(u.getPassword())) + "
    ");
             }
          }
       }
    }
    
    
    /*
     * @Author: your name
     * @Date: 2020-11-08 14:59:28
     * @LastEditTime: 2020-11-08 15:00:13
     * @LastEditors: your name
     * @Description: In User Settings Edit
     * @FilePath: /java/dataExchange/DataExchangeTest.java
     */
    package dataExchange;
    
    import java.awt.*;
    import javax.swing.*;
    
    /**
     * @version 1.35 2018-04-10
     * @author Cay Horstmann
     */
    public class DataExchangeTest {
       public static void main(String[] args) {
          EventQueue.invokeLater(() -> {
             var frame = new DataExchangeFrame();
             frame.setTitle("DataExchangeTest");
             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             frame.setVisible(true);
          });
       }
    }
    
    
    package dataExchange;
    
    import java.awt.BorderLayout;
    import java.awt.Component;
    import java.awt.Frame;
    import java.awt.GridLayout;
    
    import javax.swing.JButton;
    import javax.swing.JDialog;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JPasswordField;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
    
    /**
     * A password chooser that is shown inside a dialog.
     */
    public class PasswordChooser extends JPanel {
       private JTextField username;
       private JPasswordField password;
       private JButton okButton;
       private boolean ok;
       private JDialog dialog;
    
       public PasswordChooser() {
          setLayout(new BorderLayout());
    
          // construct a panel with user name and password fields
    
          var panel = new JPanel();
          panel.setLayout(new GridLayout(2, 2));
          panel.add(new JLabel("User name:"));
          panel.add(username = new JTextField(""));
          panel.add(new JLabel("Password:"));
          panel.add(password = new JPasswordField(""));
          add(panel, BorderLayout.CENTER);
    
          // create Ok and Cancel buttons that terminate the dialog
    
          okButton = new JButton("Ok");
          okButton.addActionListener(event -> {
             ok = true;
             dialog.setVisible(false);
          });
    
          var cancelButton = new JButton("Cancel");
          cancelButton.addActionListener(event -> dialog.setVisible(false));
    
          // add buttons to southern border
    
          var buttonPanel = new JPanel();
          buttonPanel.add(okButton);
          buttonPanel.add(cancelButton);
          add(buttonPanel, BorderLayout.SOUTH);
       }
    
       /**
        * Sets the dialog defaults.
        * 
        * @param u the default user information
        */
       public void setUser(User u) {
          username.setText(u.getName());
       }
    
       /**
        * Gets the dialog entries.
        * 
        * @return a User object whose state represents the dialog entries
        */
       public User getUser() {
          return new User(username.getText(), password.getPassword());
       }
    
       /**
        * Show the chooser panel in a dialog.
        * 
        * @param parent a component in the owner frame or null
        * @param title  the dialog window title
        */
       public boolean showDialog(Component parent, String title) {
          ok = false;
    
          // locate the owner frame
    
          Frame owner = null;
          if (parent instanceof Frame)
             owner = (Frame) parent;
          else
             owner = (Frame) SwingUtilities.getAncestorOfClass(Frame.class, parent);
    
          // if first time, or if owner has changed, make new dialog
    
          if (dialog == null || dialog.getOwner() != owner) {
             dialog = new JDialog(owner, true);
             dialog.add(this);
             dialog.getRootPane().setDefaultButton(okButton);
             dialog.pack();
          }
    
          // set title and show dialog
    
          dialog.setTitle(title);
          dialog.setVisible(true);
          return ok;
       }
    }
    
    
    package dataExchange;
    
    /**
     * A user has a name and password. For security reasons, the password is stored
     * as a char[], not a String.
     */
    public class User {
       private String name;
       private char[] password;
    
       public User(String aName, char[] aPassword) {
          name = aName;
          password = aPassword;
       }
    
       public String getName() {
          return name;
       }
    
       public char[] getPassword() {
          return password;
       }
    
       public void setName(String aName) {
          name = aName;
       }
    
       public void setPassword(char[] aPassword) {
          password = aPassword;
       }
    }
    
    
    Hope is a good thing,maybe the best of things,and no good thing ever dies.----------- Andy Dufresne
  • 相关阅读:
    知识工程及语义网技术 2020-03-19 (第一节)-构建本体
    知识工程及语义网技术 2020-03-12 (第二节)-构建本体
    知识工程及语义网技术 2020-03-12 (第二节)、RDF(S)、OWL
    知识工程及语义网技术(一)-XML、RDF(S)、OWL-2020.3.5第一节
    知识工程及语义网技术(一)-知识工程,万维网、语义网、本体工程 2020-02-20 (第一节)
    本体
    语义网技术及其应用(四)-欧石燕
    一文深度揭秘3GPP:2G/3G/4G/Pre-5G标准化制定流程是这样的
    3GPP更新5G标准时间表
    一张图了解3GPP
  • 原文地址:https://www.cnblogs.com/eat-too-much/p/13944458.html
Copyright © 2020-2023  润新知