gpt4 book ai didi

java - JComboBox 和 JButton 问题

转载 作者:行者123 更新时间:2023-12-01 11:55:33 26 4
gpt4 key购买 nike

我的 JButtons 和 JComboBox 遇到问题,如果用户没有选择状态,我需要显示一条消息,然后他会收到警告,直到他选择状态。但它一直搞砸,当我选择一个状态时,它仍然告诉我选择一个状态。然后,对于 JButtons,用户应该选择一种性别,但他可以同时选择两种性别,我不确定为什么它应该只选择一种性别时这样做。

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JRadioButton;

private JPanel infoPanel = new JPanel();
private JTextField firstname;
private JTextField lastname;
private JTextField textArea;
private JLabel labelAge;
private JTextField age;
private JLabel lblState;
private JComboBox stateBox;
private JLabel labelSex;
private JRadioButton maleButton;
private JRadioButton femaleButton;

// creating the frame
public EnhancedWelcomeGUI() {

// title
super("Enhanced Welcome GUI");

// Layout set up
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 506, 203);
setContentPane(infoPanel);

// Label to display Programmer Info
JLabel programmerInfo = new JLabel(
"Patrick McCully, CMIS 242, Homework 1");

// Labeling for first name
JLabel labelFirstName = new JLabel("First Name:");

// text field for the first name
firstname = new JTextField();
firstname.setColumns(10);

// labeling for the last name
JLabel labelLastName = new JLabel("Last Name:");

// text field for the last name
lastname = new JTextField();
lastname.setColumns(10);

// labeling for the text field
labelAge = new JLabel("Age:");

// text field for the age
age = new JTextField();
age.setColumns(10);

// the text field to display back to the user
textArea = new JTextField();
textArea.setEditable(false);

lblState = new JLabel("State:");

stateBox = new JComboBox();
stateBox.setModel(new DefaultComboBoxModel(new String[] { "",
"Alabama", "Alaska", "Arizona", "Arkansas", "California",
"Colorado", "Connecticut", "Delaware", "Florida", "Georgia",
"Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas",
"Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts",
"Michigan", "Minnesota", "Mississippi", "Missouri", "Montana",
"Nebraska", "Nevada", "New Hampshire", "New Jersey",
"New Mexico", "New York", "North Carolina", "Ohio", "Oklahoma",
"Oregon", "Pennsylvania", "Rhode Island", "South Carolina",
"Tennessee", "Texas", "Utah", "Vermont", "Virginia",
"Washington", "West Virginia", "Wisconsin", "Wyoming" }));

labelSex = new JLabel("Sex:");
maleButton = new JRadioButton("Male");
femaleButton = new JRadioButton("Female");

// the submit button when the user has entered the information
JButton buttonWelcome = new JButton("Submit");

//layout
GroupLayout groupLayoutPanel = new GroupLayout(infoPanel);

groupLayoutPanel.setHorizontalGroup(
groupLayoutPanel.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayoutPanel.createSequentialGroup()
.addGap(10)
.addGroup(groupLayoutPanel.createParallelGroup(Alignment.LEADING)
.addComponent(programmerInfo, GroupLayout.PREFERRED_SIZE, 226, GroupLayout.PREFERRED_SIZE)
.addGroup(groupLayoutPanel.createSequentialGroup()
.addComponent(labelFirstName, GroupLayout.PREFERRED_SIZE, 66, GroupLayout.PREFERRED_SIZE)
.addGap(52)
.addComponent(labelLastName, GroupLayout.PREFERRED_SIZE, 66, GroupLayout.PREFERRED_SIZE)
.addGap(53)
.addComponent(labelAge, GroupLayout.PREFERRED_SIZE, 46, GroupLayout.PREFERRED_SIZE)
.addGap(71)
.addComponent(lblState, GroupLayout.PREFERRED_SIZE, 59, GroupLayout.PREFERRED_SIZE))
.addGroup(groupLayoutPanel.createSequentialGroup()
.addComponent(firstname, GroupLayout.PREFERRED_SIZE, 108, GroupLayout.PREFERRED_SIZE)
.addGap(10)
.addComponent(lastname, GroupLayout.PREFERRED_SIZE, 108, GroupLayout.PREFERRED_SIZE)
.addGap(10)
.addComponent(age, GroupLayout.PREFERRED_SIZE, 108, GroupLayout.PREFERRED_SIZE)
.addGap(10)
.addComponent(stateBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGroup(groupLayoutPanel.createSequentialGroup()
.addGroup(groupLayoutPanel.createParallelGroup(Alignment.LEADING)
.addComponent(labelSex, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE)
.addGroup(groupLayoutPanel.createSequentialGroup()
.addGap(28)
.addComponent(maleButton))
.addGroup(groupLayoutPanel.createSequentialGroup()
.addGap(77)
.addComponent(femaleButton)))
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(buttonWelcome, GroupLayout.PREFERRED_SIZE, 135, GroupLayout.PREFERRED_SIZE))
.addComponent(textArea, GroupLayout.PREFERRED_SIZE, 471, GroupLayout.PREFERRED_SIZE)))
);
groupLayoutPanel.setVerticalGroup(
groupLayoutPanel.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayoutPanel.createSequentialGroup()
.addGap(11)
.addComponent(programmerInfo)
.addGap(10)
.addGroup(groupLayoutPanel.createParallelGroup(Alignment.LEADING)
.addComponent(labelFirstName)
.addComponent(labelLastName)
.addComponent(labelAge)
.addComponent(lblState))
.addGap(11)
.addGroup(groupLayoutPanel.createParallelGroup(Alignment.LEADING)
.addComponent(firstname, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(lastname, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(age, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(stateBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(10)
.addGroup(groupLayoutPanel.createParallelGroup(Alignment.LEADING)
.addComponent(maleButton)
.addGroup(groupLayoutPanel.createSequentialGroup()
.addGroup(groupLayoutPanel.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayoutPanel.createSequentialGroup()
.addGap(4)
.addComponent(labelSex))
.addGroup(groupLayoutPanel.createParallelGroup(Alignment.BASELINE)
.addComponent(femaleButton)
.addComponent(buttonWelcome)))
.addGap(6)
.addComponent(textArea, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
.addGap(26))
);
infoPanel.setLayout(groupLayoutPanel);

buttonWelcome.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent pressing) {

int userAge;

// a try/catch to catch any error and display
// back to the user that he must finish
try {

String first = firstname.getText();
String last = lastname.getText();
Object state = stateBox.getSelectedItem();
userAge = Integer.parseInt(age.getText());
boolean male = maleButton.isSelected();
boolean female = femaleButton.isSelected();

// warning section
if (userAge <= 0) {

JOptionPane.showMessageDialog(null,
"Must be a positive age");

} else if (!state.equals(state)) {

JOptionPane.showMessageDialog(null,
"Must select state");

}

if (male) {

textArea.setText("Welcome Male " + first + ", " + last
+ " of " + userAge + " years old from " + state);

} else if (female) {

textArea.setText("Welcome Female " + first + ", "
+ last + " of " + userAge + " years old from "
+ state);

}

// display back the error as a pop up
} catch (Exception e) {

JOptionPane.showMessageDialog(null,
"Must Complete your Info");

}

}
});

}

// to execute the program
public static void main(String[] args) {

EnhancedWelcomeGUI frame = new EnhancedWelcomeGUI();
frame.setVisible(true);

}
}

最佳答案

为了更友好的组合框,不要存储和清空状态。相反,使用自定义渲染器来显示“选择状态”等消息。

然后您可以通过确保 JComboBox 的 getSelectedIndex() 方法不为 -1 来简单测试是否已选择状态。

查看Combo Box Prompt用于为您执行此操作的自定义渲染器。

关于java - JComboBox 和 JButton 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28470718/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com