저는이 작업을 얼마 동안 해왔으며 지금 당장 도움을 청합니다. actionPerformed 메서드에서 닫을 텍스트 입력 필드를 포함하는 JFrame 얻으려면 노력하고있어,하지만 아무것도 작동하도록 얻을 수 없습니다. JFrame.dispose는 올바른 Jframe에 액세스 할 수 없으며 setVisible (false)도 완전히 잘못하지 않는 한 똑같이 쓸모가 없습니다.다른 메서드에서 다른 JFrame 닫기
//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 (이) .dispose를 사용하여(); 내 actionPerformed 메서드에서 활성 JFrame을 닫을 수 있습니다. –
좋은 직장 :-) 자신의 질문에 대답 해보십시오. – kleopatra