gpt4 book ai didi

java - 从另一个类中处置 JDialog

转载 作者:行者123 更新时间:2023-12-02 02:47:29 25 4
gpt4 key购买 nike

我想从另一个类中处理 JDialog,因为我试图保持类和方法的干净,而不是在同一个类中创建按钮和处理监听器。所以这就是问题所在。

我尝试从第一个类创建一个 get 方法来获取对话框,然后将其处理在第三个类上,但没有成功。

public class AddServiceListener extends JFrame implements ActionListener {

/**
* Creates listener for the File/New/Service button.
*/
public AddServiceListener() {

}

/**
* Performs action.
*/
public void actionPerformed(ActionEvent e) {
AddServiceWindow dialog = new AddServiceWindow();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
}
}


public class AddServiceWindow extends JDialog {

private JPanel contentPanel;
private JFrame frame;
private JPanel buttonPanel;
private JLabel nameLabel;
private JTextField nameField;
private JLabel destinationLabel;
private JTextField destinationField;
private JButton destinationButton;
private JButton okButton;
private JButton cancelButton;

/**
* Creates the dialog window.
*/
public AddServiceWindow() {
ManageMinder mainFrame = new ManageMinder();
frame = mainFrame.getFrame();

contentPanel = new JPanel();
contentPanel.setLayout(null);

setTitle("New Service File");
setSize(340, 220);
setLocationRelativeTo(frame);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);

buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPanel, BorderLayout.SOUTH);

createLabels();
createTextFields();
createButtons();
addListeners();
}

/**
* Creates the labels.
*/
private void createLabels() {
nameLabel = new JLabel("Name:");
nameLabel.setHorizontalAlignment(SwingConstants.RIGHT);
nameLabel.setBounds(25, 25, 52, 16);
contentPanel.add(nameLabel);

destinationLabel = new JLabel("Path:");
destinationLabel.setHorizontalAlignment(SwingConstants.RIGHT);
destinationLabel.setBounds(7, 70, 70, 16);
contentPanel.add(destinationLabel);
}

/**
* Creates the text fields.
*/
private void createTextFields() {
nameField = new JTextField();
nameField.setBounds(87, 22, 220, 22);
contentPanel.add(nameField);
nameField.setColumns(10);

destinationField = new JTextField();
destinationField.setBounds(87, 68, 220, 20);
contentPanel.add(destinationField);
destinationField.setColumns(10);
}

/**
* Creates the buttons of the window.
*/
private void createButtons() {
destinationButton = new JButton("Select...");
destinationButton.setBounds(87, 99, 82, 23);
destinationButton.setFocusPainted(false);
contentPanel.add(destinationButton);

okButton = new JButton("OK");
okButton.setFocusPainted(false);
buttonPanel.add(okButton);

cancelButton = new JButton("Cancel");
cancelButton.setFocusPainted(false);
buttonPanel.add(cancelButton);
}

/**
* Adds listeners to buttons.
*/
private void addListeners() {
ActionListener destinationAction = new AddDestinationListener(destinationField);
destinationButton.addActionListener(destinationAction);

ActionListener okAction = new SaveNewServiceFileListener(nameField, destinationField);
okButton.addActionListener(okAction);
}
}


public class SaveNewServiceFileListener extends JFrame implements ActionListener {

private JTextField nameField;
private JTextField destinationField;
private String path;
private File newService;

/**
* Creates listener for the File/New/Add Service/OK button.
*/
public SaveNewServiceFileListener(JTextField nameField, JTextField destinationField) {
this.nameField = nameField;
this.destinationField = destinationField;
}

/**
* Performs action.
*/
public void actionPerformed(ActionEvent e) {
path = destinationField.getText() + "\\" + nameField.getText() + ".csv";

try {
newService = new File(path);
if(newService.createNewFile()) {
System.out.println("Done!");
// DISPOSE HERE
}
else {
System.out.println("Exists!");
}
} catch (IOException io) {
throw new RuntimeException(io);
}
}
}

当单击“确定”并创建文件时,我应该做什么,以便对话框在第三个对话框上进行处理?

最佳答案

更改另一个对象状态的方法是 1) 拥有对该对象的引用,2) 调用该对象的公共(public)方法。

这里另一个对象是 JDialog,您希望通过调用 .close().dispose() 来更改其可见性状态。您遇到的问题是,对 JDialog 的引用不容易获得,因为它隐藏在 AddServiceListener 类的 actionPerformed(...) 方法中。

所以不要这样做——不要隐藏引用,而是将其放入需要它的类的字段中。

如果您绝对需要拥有独立的 ActionListener 类,那么我认为最简单的做法是将对话框从监听器类中取出并放入其中 View 类,您的主 GUI,然后让监听器调用 View 上的方法。例如

假设对话框类名为 SomeDialog,主 GUI 名为 MainGui。然后将对对话框的引用放入 MainGui 类中:

public class MainGui extends JFrame {
private SomeDialog someDialog = new SomeDialog(this);

不要让监听器创建对话框,而是让他们调用主类中的方法来执行此操作:

public class ShowDialogListener implements ActionListener {
private MainGui mainGui;

public ShowDialogListener(MainGui mainGui) {
// pass the main GUI reference into the listener
this.mainGui = mainGui;
}

@Override
public void actionPerformed(ActionEvent e) {
// tell the main GUI to display the dialog
mainGui.displaySomeDialog();
}
}

然后MainGUI可以有:

public void displaySomeDialog() {
someDialog.setVisible(true);
}

示例 MRE程序可能如下所示:

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Window;
import java.awt.event.*;

import javax.swing.*;

public class FooGui002 {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
MainGui mainGui = new MainGui();
mainGui.setVisible(true);
});
}
}
@SuppressWarnings("serial")
class MainGui extends JFrame {
private SomeDialog someDialog;
private JButton showSomeDialogButton = new JButton("Show Some Dialog");
private JButton closeSomeDialogButton = new JButton("Close Some Dialog");

public MainGui() {
super("Main GUI");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setPreferredSize(new Dimension(500, 200));

someDialog = new SomeDialog(this);

showSomeDialogButton.addActionListener(new ShowDialogListener(this));
showSomeDialogButton.setMnemonic(KeyEvent.VK_S);

closeSomeDialogButton.addActionListener(new CloseSomeDialogListener(this));
closeSomeDialogButton.setMnemonic(KeyEvent.VK_C);

setLayout(new FlowLayout());
add(showSomeDialogButton);
add(closeSomeDialogButton);
pack();
setLocationByPlatform(true);
}

public void displaySomeDialog() {
someDialog.setVisible(true);
}

public void closeSomeDialog() {
someDialog.setVisible(false);
}
}
@SuppressWarnings("serial")
class SomeDialog extends JDialog {
public SomeDialog(Window window) {
super(window, "Some Dialog", ModalityType.MODELESS);
setPreferredSize(new Dimension(300, 200));
add(new JLabel("Some Dialog", SwingConstants.CENTER));
pack();
setLocationByPlatform(true);
}
}
class ShowDialogListener implements ActionListener {
private MainGui mainGui;

public ShowDialogListener(MainGui mainGui) {
this.mainGui = mainGui;
}

@Override
public void actionPerformed(ActionEvent e) {
mainGui.displaySomeDialog();
}
}
class CloseSomeDialogListener implements ActionListener {
private MainGui mainGui;

public CloseSomeDialogListener(MainGui mainGui) {
this.mainGui = mainGui;
}

@Override
public void actionPerformed(ActionEvent e) {
mainGui.closeSomeDialog();
}
}

关于java - 从另一个类中处置 JDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57134444/

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