java - 两个 JFrame 之间交换 Swing 信息的模式

标签 java swing design-patterns jframe

他们是如何设计 JFileChooser 的?

JFileChooser chooser = new JFileChooser();
int returnVal = chooser.showOpenDialog(this);
if(returnVal == JFileChooser.APPROVE_OPTION) {
  System.out.println("You chose to open this file: " +
                chooser.getSelectedFile().getName());

如何设计这样一个具有信息交换功能的框架。例如,我有 Frame1 和 Frame2。 Frame1 打开 Frame2。 Frame2 有一个 JTextArea,我将在其中设置一些文本并对其进行编辑。在第 2 帧中按下“确定”按钮后,它会关闭,我希望将文本保存在第 1 帧中的变量中。

或者说我是否想制作一个字体选择器对话框。

JOptionPane 不适合我。在第 2 帧中,我将有一个 HTML 编辑器。在第 1 帧中我有 JTable。单击表格上的一行将使用 HTML 编辑器打开框架 2。我正在使用SHEF以此目的。当我按“确定/保存”按钮关闭frame2时,我想要frame1中的html文本String。并相应地设置行内容。但frame2可以是模式对话框。

最佳答案

首先通读 The Use of Multiple JFrames: Good or Bad Practice?

然后阅读 How to make dialogs .

JFileChooser 是一个组件,它具有还显示对话框的方法。您的需求可能会有所不同,但这不是一个坏模式,因为它不会让您的组件开始需要始终显示在对话框上

已更新

您可以使用JOptionPane

enter image description here

import java.awt.EventQueue;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestOptionPane12 {

    public static void main(String[] args) {
        new TestOptionPane12();
    }

    public TestOptionPane12() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JTextField field = new JTextField();
                int option = JOptionPane.showConfirmDialog(null, field, "Fill it out", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
                switch (option) {
                    case JOptionPane.OK_OPTION:
                        System.out.println("You entered " + field.getText());
                        break;
                }

            }

        });
    }

}

或者您可以创建更自定义的解决方案...

enter image description here

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestOptionPane12 {

    public static void main(String[] args) {
        new TestOptionPane12();
    }

    public TestOptionPane12() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                FieldsPane pane = new FieldsPane();
                switch (pane.showDialog(null)) {
                    case JOptionPane.OK_OPTION:
                        String text = pane.getText();
                        System.out.println("You entered: " + text);
                        break;
                }                
            }
        });    
    }

    protected class FieldsPane extends JPanel {

        private JTextField field;
        private int state = JOptionPane.CANCEL_OPTION;

        public FieldsPane() {
            setLayout(new GridBagLayout());
            field = new JTextField(10);
            add(field);
        }

        public String getText() {
            return field.getText();
        }

        public int showDialog(Component parent) {

            JButton btnOkay = new JButton("Ok");
            JButton btnCancel = new JButton("Cancel");
            JPanel buttons = new JPanel();
            buttons.add(btnOkay);
            buttons.add(btnCancel);

            btnOkay.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    state = JOptionPane.OK_OPTION;
                    Window win = SwingUtilities.getWindowAncestor((Component)e.getSource());
                    win.dispose();
                }
            });
            btnCancel.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    state = JOptionPane.CANCEL_OPTION;
                    Window win = SwingUtilities.getWindowAncestor((Component)e.getSource());
                    win.dispose();
                }
            });

            JDialog dialog = new JDialog(parent == null ? (Window)null : SwingUtilities.getWindowAncestor(parent), "Fill it out");
            dialog.setModal(true);
            dialog.add(this);
            dialog.add(buttons, BorderLayout.SOUTH);
            dialog.pack();
            dialog.setLocationRelativeTo(null);
            dialog.setVisible(true);

            return state;            
        }        
    }    
}

更新了 JOptionPane 和 JEditorPane 示例

enter image description here

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JEditorPane;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestOptionPane12 {

    public static void main(String[] args) {
        new TestOptionPane12();
    }

    public TestOptionPane12() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JEditorPane editorPane = new JEditorPane("text/html", null);
                JScrollPane scrollPane = new JScrollPane(editorPane);
                scrollPane.setPreferredSize(new Dimension(200, 200));
                int option = JOptionPane.showConfirmDialog(null, scrollPane, "Fill it out", JOptionPane.OK_CANCEL_OPTION, -1);
                switch (option) {
                    case JOptionPane.OK_OPTION:
                        System.out.println("You entered " + editorPane.getText());
                        break;
                }

            }

        });
    }
}

关于java - 两个 JFrame 之间交换 Swing 信息的模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17292864/

相关文章:

javascript - 这是一个新的 javascript 工厂模式吗?

java - Bean set属性

java - 在java中使用JTextfields将数据更新到oracle数据库中的问题

c# - 如何在现有项目中实现 SOLID 原则

model-view-controller - 什么是后端上下文中的MVC?

java - 大写锁定检测在重新聚焦后返回错误值

无法解析 java.time.format.DateTimeParseException 从 Java 8 迁移到 Java 11

java - 使用 spring SecurityContextHolder 在 tomcat 中 session id 为 null

Java 字符串拆分为 "."(点)

java - java.awt 中的屏幕外图形分辨率