从另一个方法关闭另一个JFrame

时间:2012-03-01 23:17:49

标签: swing window jframe action

我已经做了一段时间了,我现在非常感谢你的帮助。 我正在尝试从我的actionPerformed方法中获取包含文本输入字段的JFrame,但我似乎无法获得任何工作。 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方法中实例化的框架。

1 个答案:

答案 0 :(得分:0)

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

在另一个静态方法里面(比如main) 从类引用PersonInput.createAndShowGUI() 或者从一个对象,但该方法或属性将始终相同,静态属性是共享的。

我可以建议两种方法来解决你的问题。

一个是将对象框传递给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