首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >从另一个类中释放JDialog

从另一个类中释放JDialog
EN

Stack Overflow用户
提问于 2019-07-21 23:10:31
回答 1查看 287关注 0票数 0

我希望处理来自另一个类的JDialog,因为我试图保持类和方法的整洁,而不是在同一个类中创建按钮和处理侦听器。这就是问题所在。

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

代码语言:javascript
复制
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);
    }
}
}

当点击OK并创建文件时,我应该怎么做,这样对话框就会放在第三个对话框上?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-22 00:22:09

更改另一个对象的状态的方法是: 1)引用该对象,2)在该对象上调用公共方法。

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

因此,不要这样做--不要埋没引用,而是将其放入需要它的类的字段中。

如果你绝对需要有独立的ActionListener类,那么我认为最简单的事情就是把对话框从listener类中移到视图类中,然后让listener调用视图上的方法。例如

假设对话框类称为SomeDialog,主图形用户界面称为MainGui。然后将对对话框的引用放在MainGui类中:

代码语言:javascript
复制
public class MainGui extends JFrame {
    private SomeDialog someDialog = new SomeDialog(this);

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

代码语言:javascript
复制
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可以拥有:

代码语言:javascript
复制
public void displaySomeDialog() {
    someDialog.setVisible(true);
}

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

代码语言:javascript
复制
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);
        });
    }
}
代码语言:javascript
复制
@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);
    }
}
代码语言:javascript
复制
@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);
    }
}
代码语言:javascript
复制
class ShowDialogListener implements ActionListener {
    private MainGui mainGui;

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

    @Override
    public void actionPerformed(ActionEvent e) {
        mainGui.displaySomeDialog();
    }
}
代码语言:javascript
复制
class CloseSomeDialogListener implements ActionListener {
    private MainGui mainGui;

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

    @Override
    public void actionPerformed(ActionEvent e) {
        mainGui.closeSomeDialog();
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57134444

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档