如何关闭 JDialog 并保存设置?

穆吉莫

嗨,我正在开发一个程序,当我从 JDialog 中选择一些设置然后单击“确定”时遇到了一个问题,即设置没有保存而是返回到原始设置。

PS:我不会说英语,所以也许你会发现我上面的文字有一些错误。

图片在此输入图片说明

class DrawingSettingWindow extends JDialog {


    public DrawingSettingWindow() {

        this.setTitle("Drawing Setting Window");
        this.setSize(550, 550);
        this.setLocationRelativeTo(null);

        this.setModal(true);

        this.setLayout(new GridLayout(4, 1));

        JLabel selectColorText = new JLabel("Select Drawing Color");

        colorsList = new JComboBox(colors);
        JPanel panel1 = new JPanel();
        panel1.add(selectColorText);
        panel1.add(colorsList);
        add(panel1);

        JLabel selectStyleText = new JLabel("Select Drawing Style");
        JPanel panel2 = new JPanel();

        normal = new JRadioButton("Normal");
        normal.setSelected(true);
        filled = new JRadioButton("Filled");
        ButtonGroup bg = new ButtonGroup();

        bg.add(normal);
        bg.add(filled);
        panel2.add(selectStyleText);
        panel2.add(normal);
        panel2.add(filled);
        add(panel2);

        JButton ok = new JButton("OK");

        add(ok);

        ok.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                setVisible(false);
            }
        });




        this.pack();
        this.setVisible(true);

    }
知道的不多,但越来越好

信息就在那里,您只需在用户完成使用后从对话框中提取它。我会给上面的代码至少提供两个新方法,一个是getColor()返回的公共方法,colorsList.getSelectedItem();用户的颜色选择(我不确定这是什么类型的对象,所以我还不能显示该方法)。也是另一个获取用户填充设置的,也许

public boolean getFilled() {
    return filled.isSelected();
}

由于对话框是模态的,您将在调用代码中将其设置为可见后立即知道用户已完成使用它。这就是您调用上述方法来提取数据的地方。

在下面的代码中,我在本节中展示了这一点:drawingSettings.setVisible(true);

        // here you extract the data
        Object color = drawingSettings.getColor();
        boolean filled = drawingSettings.getFilled();
        textArea.append("Color: " + color + "\n");
        textArea.append("Filled: " + filled + "\n");                
    }

例如(见评论):

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Window;
import java.awt.Dialog.ModalityType;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

@SuppressWarnings("serial")
public class UseDrawingSettings extends JPanel {
    private JTextArea textArea = new JTextArea(20, 40);
    private DrawingSettingWindow drawingSettings;

    public UseDrawingSettings() {
        JPanel topPanel = new JPanel();
        topPanel.add(new JButton(new ShowDrawSettings()));
        setLayout(new BorderLayout());

        add(new JScrollPane(textArea));
        add(topPanel, BorderLayout.PAGE_START);
    }

    private class ShowDrawSettings extends AbstractAction {
        public ShowDrawSettings() {
            super("Get Drawing Settings");
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            if (drawingSettings == null) {
                Window win = SwingUtilities.getWindowAncestor(UseDrawingSettings.this);
                drawingSettings = new DrawingSettingWindow(win);
            }
            drawingSettings.setVisible(true);

            // here you extract the data
            Object color = drawingSettings.getColor();
            boolean filled = drawingSettings.getFilled();
            textArea.append("Color: " + color + "\n");
            textArea.append("Filled: " + filled + "\n");                
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }

    private static void createAndShowGui() {
        UseDrawingSettings mainPanel = new UseDrawingSettings();
        JFrame frame = new JFrame("UseDrawingSettings");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }
}

@SuppressWarnings("serial")
class DrawingSettingWindow extends JDialog {

    private static final String TITLE = "Drawing Setting Window";
    private JComboBox<String> colorsList;
    private JRadioButton normal;
    private JRadioButton filled;

    // not sure what colors is, but I'll make it a String array for testing
    private String[] colors = {"Red", "Orange", "Yellow", "Green", "Blue"};


    public DrawingSettingWindow(Window win) {
        super(win, TITLE, ModalityType.APPLICATION_MODAL);
        // this.setTitle("Drawing Setting Window");
        this.setSize(550, 550); // !! this is not recommended
        this.setLocationRelativeTo(null);

        this.setModal(true);

        this.setLayout(new GridLayout(4, 1));

        JLabel selectColorText = new JLabel("Select Drawing Color");

        colorsList = new JComboBox(colors);
        JPanel panel1 = new JPanel();
        panel1.add(selectColorText);
        panel1.add(colorsList);
        add(panel1);

        JLabel selectStyleText = new JLabel("Select Drawing Style");
        JPanel panel2 = new JPanel();

        normal = new JRadioButton("Normal");
        normal.setSelected(true);
        filled = new JRadioButton("Filled");
        ButtonGroup bg = new ButtonGroup();

        bg.add(normal);
        bg.add(filled);
        panel2.add(selectStyleText);
        panel2.add(normal);
        panel2.add(filled);
        add(panel2);

        JButton ok = new JButton("OK");

        add(ok);

        ok.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                setVisible(false);
            }
        });

        this.pack();
        // this.setVisible(true); // this should be the calling code's responsibility

    }

    public Object getColor() {
        return colorsList.getSelectedItem();
    }

    public boolean getFilled() {
        return filled.isSelected();
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Foo");
    }
}

旁注:

  • 我已将您的类的构造函数更改为接受 Window 参数、JFrame、JDialog 等的基类,并添加了对 super 构造函数的调用。这样,对话框是调用代码的真正子窗口(或者,null如果您不希望它被传入,可以传入)。
  • 我建议不要让对话框在其构造函数中可见。执行此操作是调用代码的责任,并且在某些情况下,调用代码希望在创建对话框后不使其可见,例如,如果它想在使其可见之前将 PropertyChangeListener 附加到它。这对于模态对话框最重要,但只是良好的编程实践。
  • 我不知道你的组合框所持有的对象的类型,所以为了演示目的制作了一个 String 数组。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章