【问题标题】:Closing another JFrame from another method从另一个方法关闭另一个 JFrame
【发布时间】:2012-03-20 12:12:50
【问题描述】:

我已经为此工作了一段时间,现在我非常感谢一些帮助。 我试图让包含文本输入字段的 JFrame 从我的 actionPerformed 方法中关闭,但我似乎无法正常工作。 JFrame.dispose 不会让我访问正确的 Jframe,并且 setVisible(false) 同样没用,除非我这样做完全错误。

//halp
import javax.swing.*;
import java.awt.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

class PersonInput extends JPanel
              implements ActionListener {

//Fields for data entry
private JFormattedTextField firstField, lastField, dateField;

public String x[] = new String[3];

public PersonInput() {

    //Values for the fields
    String first = "First Name";
    String last = "Last Name";
    String date = "MM/DD/YYYY";


    //Create the text fields and set them up.
    firstField = new JFormattedTextField();
    firstField.setValue(new String(first));

    lastField = new JFormattedTextField();
    lastField.setValue(new String(last));

    dateField = new JFormattedTextField();
    dateField.setValue(new String(date));
    dateField.setColumns(10);

    JButton ok = new JButton("OK");
    ok.setVerticalTextPosition(AbstractButton.BOTTOM);
    ok.setHorizontalTextPosition(AbstractButton.CENTER);
    ok.setActionCommand("ok");
    ok.addActionListener(this);
    ok.setToolTipText("Confirms user input and continues with the program.");

    JPanel buttons = new JPanel(new GridLayout(0,1));
    buttons.add(ok);


    //Layout the text fields in a panel.
    JPanel fieldPane = new JPanel(new GridLayout(0,1));
    fieldPane.add(firstField);
    fieldPane.add(lastField);
    fieldPane.add(dateField);

    //Put the panels in this panel, labels on left,
    //text fields on right.
    setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
    add(fieldPane, BorderLayout.CENTER);
    add(buttons, BorderLayout.LINE_END);


}

public void actionPerformed(ActionEvent e) {
    if ("ok".equals(e.getActionCommand()))
    {
            JFrame frame1 = new JFrame("People Sorter");

        x[0] = firstField.getText();
        x[1] = lastField.getText();
        x[2] = dateField.getText();

        JOptionPane.showMessageDialog(frame1, "Person has been added.");
        dispPerson();
        frame.setVisible(false);
    }
}

public void dispPerson()
{
    System.out.println(x[0] + x[1] + x[2]);

}
public static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("Person Input");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Add contents to the window.
    frame.add(new PersonInput());

    //Display the window.
    frame.pack();
    frame.setVisible(true);
}

public static void main(String[] args) {

    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            //Turn off metal's use of bold fonts
        UIManager.put("swing.boldMetal", Boolean.FALSE);
            createAndShowGUI();
        }
    });
}
}

如果有人有任何想法,我会全力以赴;我整天为此感到压力。非常感谢您抽出宝贵的时间!

编辑:为了澄清起见,我试图关闭的框架是在 createAndShowGUI 方法中实例化的框架。

【问题讨论】:

  • 没关系,我发现通过使用 SwingUtilities.getWindowAncestor(this).dispose();在我的 actionPerformed 方法中,我可以关闭活动的 JFrame。
  • 干得好 :-) 你可以考虑回答你自己的问题

标签: swing window jframe action


【解决方案1】:

似乎问题在于我们正在尝试合并静态和非静态内容。简单来说,可以引用静态内容,而无需创建该类的实例(对象)。这意味着可以调用createAndShowGUI:

在另一个静态方法中(如 main) 从类参考 PersonInput.createAndShowGUI() 或来自一个对象,但该方法或属性将始终相同,静态属性是共享的。

我可以建议 2 种方法来解决您的问题。

一个是将对象框架传递给PersonInput

//halp
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
class PersonInput extends JPanel
              implements ActionListener {
//Fields for data entry
private JFormattedTextField firstField, lastField, dateField;

public String x[] = new String[3];


JFrame frame;
public PersonInput(JFrame frame) {
this.frame = frame;
//the rest of your code
}

另一种方法是将框架对象放在方法之外并将其声明为静态。

static JFrame frame = new JFrame("Person Input");;
public static void createAndShowGUI() {
    //Create and set up the window.
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //Add contents to the window.
    frame.add(new PersonInput());
    //Display the window.
    frame.pack();
    frame.setVisible(true);
}

记住static variable cannot be referenced from a static context

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-15
    • 1970-01-01
    相关资源
    最近更新 更多