gpt4 book ai didi

java - 如何关闭JDialog并保存设置?

转载 作者:行者123 更新时间:2023-12-02 02:28:20 26 4
gpt4 key购买 nike

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

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

图片 enter image description here

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);

}

最佳答案

信息就在那里,您只需在用户使用完它后从对话框中提取它即可。我将为上面的代码提供至少两个新方法,其中一个是公共(public) 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 附加到它。这对于模式对话框来说是最重要的,但这只是良好的编程实践。
  • 我不知道组合框所保存的对象类型,因此创建了一个字符串数组用于演示目的。

关于java - 如何关闭JDialog并保存设置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47497257/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com