첫째로, 사용자가 동물의 유형을 선택할 수있게 해주는 두 번째 창은 독립적 인 응용 프로그램 창으로 작동하지 않고 주 창과 관련되어 있고 종속적 인 대화 상자로 작동합니다. 예를 들어,이 두 번째 창은 절대로 표시하지 않고 주 창/클래스에 대한 정보를 얻기 위해 표시합니다. 따라서 JFrame이 아닌 JOptionPane 또는 모달 JDialog 중 하나의 대화 상자 유형이어야합니다. 여기에는 z- 순서로 주 창 앞에 남아 있도록 보장하고 처분되는 경우 전체 응용 프로그램을 종료하지 않는 등 몇 가지 다른 이점이 있습니다.
setVisible (true/false)에 대해서는 정상적으로 작동합니다.
두 번째 창/대화 상자에서 동물 유형 정보 추출 : 두 번째 창을 모달 JDialog 또는 JOptionPane (실제로는 특수화 된 모달 JDialog)로 호출하면 사용자가 작업을 완료 한 시점을 정확하게 알 수 있습니다 두 번째 창에서 setVisible (true)을 호출하거나 JOptionPane.showXXXX()를 호출 한 지점 바로 다음에서 주 윈도우의 코드가 시작되므로 두 번째 창을 사용하십시오. 이 시점에서 두 번째 창/클래스에이 정보를 반환하는 getAnimalType()과 같은 공용 메서드를 제공하여 주 창/클래스에서 두 번째 창에서 동물 유형을 요청하도록 할 수 있습니다. 예를 들어
, 여기 GridBagLayout을 사용하고있는 JOptionPane에 두 개의 JTextField를 텍스트를 허용하는 JPanel의 쇼 두 윈도우 프로그램입니다 : 내가 당신을 잘 이해한다면
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TwoWindowEg {
private static void createAndShowUI() {
JFrame frame = new JFrame("Two Window Eg");
frame.getContentPane().add(new MainPanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}
class MainPanel extends JPanel {
private JTextField textField = new JTextField(20);
private DialogPanel dialogPanel = null;
public MainPanel() {
textField.setEditable(false);
textField.setFocusable(false);
textField.setBackground(Color.white);
JButton openDialogBtn = new JButton("Open Dialog");
openDialogBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (dialogPanel == null) {
dialogPanel = new DialogPanel();
}
int result = JOptionPane.showConfirmDialog(MainPanel.this, dialogPanel,
"Enter First and Last Name", JOptionPane.OK_CANCEL_OPTION,
JOptionPane.PLAIN_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
String text = dialogPanel.getText();
textField.setText(text);
}
}
});
add(textField);
add(openDialogBtn);
setPreferredSize(new Dimension(400, 300));
setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));
}
}
class DialogPanel extends JPanel {
private static final Insets INSETS = new Insets(0, 10, 0, 10);
private JTextField firstNameField = new JTextField(10);
private JTextField lastNameField = new JTextField(10);
public DialogPanel() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0,
GridBagConstraints.LINE_START, GridBagConstraints.BOTH,
INSETS, 0, 0);
add(new JLabel("First Name"), gbc);
gbc = new GridBagConstraints(1, 0, 1, 1, 1.0, 1.0,
GridBagConstraints.LINE_END, GridBagConstraints.BOTH,
INSETS, 0, 0);
add(new JLabel("Last Name"), gbc);
gbc = new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0,
GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL,
INSETS, 0, 0);
add(firstNameField, gbc);
gbc = new GridBagConstraints(1, 1, 1, 1, 1.0, 1.0,
GridBagConstraints.LINE_END, GridBagConstraints.HORIZONTAL,
INSETS, 0, 0);
add(lastNameField, gbc);
}
public String getText() {
return firstNameField.getText() + " " + lastNameField.getText();
}
}
, 그것이 내가 클래스를 생성해야한다는 것입니다을 그 JFrame을 확장하지 않고 JDialog를 확장하거나 Listener에서 JDialog를 만들고 거기에서 사용해야 만합니까? 또 다른 문제는 동물을 만드는 첫 번째 창이 다소 복잡하다는 것입니다. 콤보 박스, 체크 박스, 패널 등이 포함되어 있습니다. 그래서 JFrame을 사용했습니다. 이것이 JDialog에서 가능할까요? – mkab
@mkab : JFrame에 표시되는 모든 유형의 복잡한 GUI를 JDialog에 표시 할 수 있습니다. JOptionPane의 show 메소드의 두 번째 매개 변수 (객체를 가져 오는 메소드)는 Swing 구성 요소가 될 수 있으므로 JOptionPanes에 복잡한 GUI를 표시 할 수 있습니다. 예를 들어, 위의 코드를 편집 내 대답에 추가했습니다. –
와우 나는 그것을 지금 얻는다. 사용자가 예를 들어 응용 프로그램을 종료할지 또는 사용자가 자신의 작업을 저장하려고 하는지를 확인하기 위해 항상 기본 JOptionPane.showXXX 대화 상자를 사용했습니다. 더 복잡한 GUI를 포함하는 것은 사용자 정의 할 수 있다고 생각한 적은 없지만 구성 요소 매개 변수에 null을 넣었 기 때문입니다. 도움과 통찰력에 감사드립니다. 건배. – mkab