2012-03-25 2 views
2

많은 추가 매개 변수를 입력하기 위해 하나의 기본 JFrame과 하나의 추가 JFrame이 있습니다. 따라서, 두 번째 jframe이 주 jframe ("저장"버튼의 ActionListener 사용)으로 데이터를 반환해야하지만, 어떻게 할 수 있는지 알지 못합니다. 제발, 도와주세요. 당신이 나를 도울 수 있기를 바랍니다. 고맙습니다.모달 JFrame 또는 다른 JComponent를 만드는 방법은 무엇입니까?

답변

5

두 번째 종속 창을 JFrame으로 만들지 마십시오. 대신 모달 JDialog 또는 JOptionPane을 사용하십시오. 많은 것은 JOptionPane가 GUI를 보관 유지하는 JPanel를 JOptionPane에 배치하는 것으로, JOptionPane가 복잡한 GUI를 보관할 수있는 것을 인식하지 않습니다. 예를 들어

:

당신이 모달 할 때 JFrame의보다 JDialog를 사용하는 것이 좋습니다
import java.awt.*; 
import java.awt.event.*; 
import java.util.HashMap; 
import java.util.Map; 

import javax.swing.*; 

@SuppressWarnings("serial") 
public class ComplexOptionPane extends JPanel { 
    private PlayerEditorPanel playerEditorPanel = new PlayerEditorPanel(); 
    private JTextArea textArea = new JTextArea(12, 30); 

    public ComplexOptionPane() { 
     textArea.setEditable(false); 
     textArea.setFocusable(false); 
     textArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 16)); 
     JPanel bottomPanel = new JPanel(); 
     bottomPanel.add(new JButton(new AbstractAction("Get Player Information") { 

     @Override 
     public void actionPerformed(ActionEvent arg0) { 
      int result = JOptionPane.showConfirmDialog(null, playerEditorPanel, 
        "Edit Player JOptionPane", JOptionPane.OK_CANCEL_OPTION, 
        JOptionPane.PLAIN_MESSAGE); 
      if (result == JOptionPane.OK_OPTION) { 
       for (PlayerEditorPanel.FieldTitle fieldTitle : PlayerEditorPanel.FieldTitle 
        .values()) { 
        textArea.append(String.format("%10s: %s%n", 
         fieldTitle.getTitle(), 
         playerEditorPanel.getFieldText(fieldTitle))); 
       } 
      } 
     } 
     })); 
     setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); 
     setLayout(new BorderLayout(5, 5)); 
     add(new JScrollPane(textArea), BorderLayout.CENTER); 
     add(bottomPanel, BorderLayout.PAGE_END); 
    } 

    private static void createAndShowGui() { 
     ComplexOptionPane mainPanel = new ComplexOptionPane(); 

     JFrame frame = new JFrame("ComplexOptionPane"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(mainPanel); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowGui(); 
     } 
     }); 
    } 
} 

@SuppressWarnings("serial") 
class PlayerEditorPanel extends JPanel { 
    enum FieldTitle { 
     NAME("Name"), SPEED("Speed"), STRENGTH("Strength"), HEALTH("Health"); 
     private String title; 

     private FieldTitle(String title) { 
     this.title = title; 
     } 

     public String getTitle() { 
     return title; 
     } 
    }; 

    private static final Insets WEST_INSETS = new Insets(5, 0, 5, 5); 
    private static final Insets EAST_INSETS = new Insets(5, 5, 5, 0); 
    private Map<FieldTitle, JTextField> fieldMap = new HashMap<FieldTitle, JTextField>(); 

    public PlayerEditorPanel() { 
     setLayout(new GridBagLayout()); 
     setBorder(BorderFactory.createCompoundBorder(
      BorderFactory.createTitledBorder("Player Editor"), 
      BorderFactory.createEmptyBorder(5, 5, 5, 5))); 
     GridBagConstraints gbc; 
     for (int i = 0; i < FieldTitle.values().length; i++) { 
     FieldTitle fieldTitle = FieldTitle.values()[i]; 
     gbc = createGbc(0, i); 
     add(new JLabel(fieldTitle.getTitle() + ":", JLabel.LEFT), gbc); 
     gbc = createGbc(1, i); 
     JTextField textField = new JTextField(10); 
     add(textField, gbc); 

     fieldMap.put(fieldTitle, textField); 
     } 
    } 

    private GridBagConstraints createGbc(int x, int y) { 
     GridBagConstraints gbc = new GridBagConstraints(); 
     gbc.gridx = x; 
     gbc.gridy = y; 
     gbc.gridwidth = 1; 
     gbc.gridheight = 1; 

     gbc.anchor = (x == 0) ? GridBagConstraints.WEST : GridBagConstraints.EAST; 
     gbc.fill = (x == 0) ? GridBagConstraints.BOTH 
      : GridBagConstraints.HORIZONTAL; 

     gbc.insets = (x == 0) ? WEST_INSETS : EAST_INSETS; 
     gbc.weightx = (x == 0) ? 0.1 : 1.0; 
     gbc.weighty = 1.0; 
     return gbc; 
    } 

    public String getFieldText(FieldTitle fieldTitle) { 
     return fieldMap.get(fieldTitle).getText(); 
    } 

} 
관련 문제