2012-03-31 9 views
4

Grundsätzlich habe ich irgendwo in meinem Haupt-Code, wo diese Codezeile aufgerufen wird, öffnetWie gibst du einen Wert aus einem Java-Swing-Fenster von einer Schaltfläche zurück?

editwindow clubeditwindow = new editwindow(1,"Club Edit"); 

Diese Linie eine neue JFrame, die im Grunde eine Reihe von Informationen aus der Hauptklasse bearbeiten. Ich habe 2 Tasten namens Speichern und Abbrechen. Wenn auf Speichern geklickt wird, möchte ich die Werte aus den Textfeldern übernehmen und sie dann in ein neues Objekt einfügen und dieses an die Hauptklasse zurückgeben und das Fenster schließen. Wenn Abbrechen geklickt wird, möchte ich einfach nichts tun, was einfach genug ist.

Vielen Dank im Voraus.

+0

bitte lernen Java-Namenskonventionen angezeigt wird, und halten an ihnen – kleopatra

Antwort

10

Zeigen Sie das Fenster nicht als JFrame an, sondern zeigen Sie es als modalen Dialog an. Nachdem es nicht mehr sichtbar ist, kann die Haupt-GUI das Objekt nach Informationen abfragen, die es enthält. Der einfachste Weg, dies zu tun, ist ein JOptionPane - sie sind clevere kleine Biester, wenn sie richtig verwendet werden.

Hier ist ein Beispiel, was ich meine. Das JOptionPane enthält JPane, die Informationen mithilfe eines GridBagLayout anzeigt.

ComplexOptionPane.java: Zeigt den Haupt-JFrame an.

import java.awt.event.*; 
import javax.swing.*; 

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

    public ComplexOptionPane() { 
     add(new JScrollPane(textArea)); 
     add(new JButton(new AbstractAction("Get Player Information") { 

     @Override 
     public void actionPerformed(ActionEvent arg0) { 
      int result = JOptionPane.showConfirmDialog(null, playerEditorPanel, 
        "Edit Player", 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))); 
       } 
      } 
     } 
     })); 
    } 

    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(); 
     } 
     }); 
    } 
} 

PlayerEditorPanel.java die die JPanel hält, die in einem JOptionPane

import java.awt.*; 
import java.util.HashMap; 
import java.util.Map; 
import javax.swing.*; 

@SuppressWarnings("serial") 
class PlayerEditorPanel extends JPanel { 
    enum FieldTitle { 
     NAME("Name"), SPEED("Speed"), STRENGTH("Strength"); 
     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(); 
    } 

} 
+0

Sie können mit JOptionPane das tun? Gut danke = D. – gammaraptor

+0

@gammaraptor: siehe Beispielcode oben als "zum Beispiel" –

+0

@gammaraptor: Sie haben gefragt, wie man einem Dialog mehrere Textfelder hinzufügen könnte. Die Antwort ist: Wenn Sie einem JPanel mehrere JTextFields hinzufügen können, können Sie dieses JPanel in einem JOptionPane (wie ich oben) oder einem JDialog anzeigen. –

Verwandte Themen