2015-03-19 51 views
1

我想將幾個JTextFields放到JOptionPane中,這樣我就可以驗證放入它們的信息。我知道showInputDialog()會產生一個輸入,但我將如何去實現3/4JOptionPane.ShowInputDialog中的多個JTextFields?

編輯:(在運行時,它只是顯示一個輸入字段)

//JDIALOG(FOR END PAYMENT) 
    dialogPanel = new JPanel(); 
    creditCardNoInput = new JTextField(); 
    sortCodeInput = new JTextField(); 
    secNoInput = new JTextField(); 
    cardHolderName = new JTextField(); 

    dialogPanel.add(creditCardNoInput); 
    dialogPanel.add(sortCodeInput); 
    dialogPanel.add(secNoInput); 
    dialogPanel.add(cardHolderName); 

    int result = JOptionPane.showConfirmDialog(null, dialogPanel, 
        "Please Enter your card details", JOptionPane.OK_CANCEL_OPTION); 

     if (result == JOptionPane.OK_OPTION) { 
      //Execute desired code 
+0

顯示你已經嘗試並解釋發生了什麼。 – 2015-03-19 12:18:38

+0

@ Sippy剛剛更新了我的代碼。 :) – 2015-03-19 12:22:11

+0

你的代碼將顯示5個JTextfields,但是它們都很窄。將構造函數更改爲像'new JTextField(5);'給JTextFields一些列寬。 – 2015-03-19 12:25:20

回答

1

使用一個JPanel。

將JTextFields和JLabel(因爲您可能還需要這些)放到JPanel或JPanels中,並將主JPanel放入JOptionPane中。如果需要,您可以將完整的複雜GUI放入JPanel中,並將其顯示在JOptionPane中。

例如:

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

} 

而且 - 使用this answer


的MCVE您的代碼示例:

import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.Insets; 

import javax.swing.*; 
import javax.swing.border.Border; 

public class Foo1 { 
    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 JPanel dialogPanel; 
    private JTextField creditCardNoInput; 
    private JTextField sortCodeInput; 
    private JTextField secNoInput; 
    private JTextField cardHolderName; 

    public Foo1() { 
     dialogPanel = new JPanel(new GridBagLayout()); 

     Border titleBorder = BorderFactory.createTitledBorder("Credit Card Information"); 
     Border emptyBorder = BorderFactory.createEmptyBorder(10, 10, 10, 10); 
     Border combinedBorder = BorderFactory.createCompoundBorder(titleBorder, emptyBorder); 
     dialogPanel.setBorder(combinedBorder); 
     creditCardNoInput = new JTextField(5); 
     sortCodeInput = new JTextField(5); 
     secNoInput = new JTextField(5); 
     cardHolderName = new JTextField(5); 

     dialogPanel.add(new JLabel("Credit Card Number:"), createGbc(0, 0)); 
     dialogPanel.add(creditCardNoInput, createGbc(1, 0)); 
     dialogPanel.add(new JLabel("Sort Code:"), createGbc(0, 1)); 
     dialogPanel.add(sortCodeInput, createGbc(1, 1)); 
     dialogPanel.add(new JLabel("Second Number:"), createGbc(0, 2)); 
     dialogPanel.add(secNoInput, createGbc(1, 2)); 
     dialogPanel.add(new JLabel("Cardholder Name:"), createGbc(0, 3)); 
     dialogPanel.add(cardHolderName, createGbc(1, 3)); 

     int result = JOptionPane.showConfirmDialog(null, dialogPanel, 
      "Please Enter your card details", JOptionPane.OK_CANCEL_OPTION, 
      JOptionPane.PLAIN_MESSAGE); 

     if (result == JOptionPane.OK_OPTION) { 
     // Execute desired code 
     } 
    } 

    private static 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; 
    } 

    private static void createAndShowGui() { 
     new Foo1(); 
    } 

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

感謝您的解決方案! – 2015-03-19 19:46:25

0

解決方案:

當我跑我的鱈魚e,出於某種原因,它更喜歡我的其他GUI啓動。當我註釋掉代碼並且JOptionPane仍在執行時,我才發現這一點,所以我手動選擇了我的GUI,運行它,然後出現了4個盒子。

P.S請投票這個答案,以便我可以得到一個徽章。謝謝! :)