希望在Java Swing GUI中验证textField和comboBox

时间:2018-05-04 09:32:56

标签: java swing validation combobox textfield

请保持温和,这是我第一次使用Swing和GUI。

我的问题/疑问:

我使用Java Swing GUI编写了一些代码。我的代码的验证部分只会识别textField是否填写错误,如果组合框保持为空则忽略。谁能告诉我这里做错了什么?我的目标是确保在用户点击提交时填写所有字段。这是我的代码:

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.layout.FormSpecs;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class FeliciasStore_GUI extends JFrame {

private JPanel contentPane;
private JTextField textField;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                FeliciasStore_GUI frame = new FeliciasStore_GUI();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public FeliciasStore_GUI() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 558, 364);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JLabel lblNewLabel = new JLabel("Size:");
    lblNewLabel.setBounds(67, 39, 23, 14);
    contentPane.add(lblNewLabel);

    JComboBox<String> comboBox = new JComboBox<String>();
    comboBox.setBounds(96, 36, 161, 20);
    contentPane.add(comboBox);
    comboBox.addItem("Small");
    comboBox.addItem("Medium");
    comboBox.addItem("Large");
    comboBox.addItem("XLarge");
    comboBox.setSelectedItem(null);

    JLabel lblColor = new JLabel("Color:");
    lblColor.setBounds(61, 90, 29, 14);
    contentPane.add(lblColor);

    JComboBox<String> comboBox_1 = new JComboBox<String>();
    comboBox_1.setBounds(96, 87, 161, 20);
    contentPane.add(comboBox_1);
    comboBox_1.addItem("Blue");
    comboBox_1.addItem("Red");
    comboBox_1.addItem("Silver");
    comboBox_1.addItem("Gold");
    comboBox_1.addItem("Aqua");
    comboBox_1.addItem("Green");
    comboBox_1.addItem("Pink");
    comboBox_1.addItem("Purple");
    comboBox_1.addItem("White");
    comboBox_1.addItem("Black");
    comboBox_1.addItem("Orange");
    comboBox_1.addItem("Maroon");
    comboBox_1.setSelectedItem(null);

    JLabel label = new JLabel("");
    label.setBounds(518, 122, 19, 0);
    contentPane.add(label);

    JLabel lblStyle = new JLabel("Style:");
    lblStyle.setBounds(62, 141, 28, 14);
    contentPane.add(lblStyle);


    JComboBox<String> comboBox_2 = new JComboBox<String>();
    comboBox_2.setBounds(96, 138, 161, 20);
    contentPane.add(comboBox_2);
    comboBox_2.addItem("Queen");
    comboBox_2.addItem("King");
    comboBox_2.addItem("Pawn");
    comboBox_2.addItem("Knight");
    comboBox_2.addItem("Joker");
    comboBox_2.addItem("Babydoll");
    comboBox_2.addItem("Superstar");
    comboBox_2.setSelectedItem(null);

    JLabel lblInscription = new JLabel("Inscription:");
    lblInscription.setBounds(36, 192, 54, 14);
    contentPane.add(lblInscription);

    textField = new JTextField();
    textField.setBounds(96, 189, 161, 20);
    contentPane.add(textField);
    textField.setColumns(10);

    JButton btnSubmit_1 = new JButton("SUBMIT");
    btnSubmit_1.setBounds(96, 240, 161, 23);
    btnSubmit_1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if(textField.getText().length()>15 || comboBox == null || comboBox_1 == null || comboBox_2 == null) {
                JOptionPane.showMessageDialog(null,  "Insufficient input.", "Input Error", JOptionPane.ERROR_MESSAGE);
                return;
            }
            String enscription = textField.getText();
        }
    });
    contentPane.add(btnSubmit_1);
}

}

0 个答案:

没有答案