Java 从文本字段接受学生信息并在组合框中显示的GUI应用程序
用户将输入全名、地址、城市、省、邮政编码、, 文本字段控件中的电话号码和电子邮件 学生的专业(计算机科学或商业)将被选择 从两个单选按钮 一个组合框将显示每个课程的课程列表 用户选择所需的程序。课程将添加到列表中 每当用户从相应的组合中选择课程时 盒子 确保用户不能多次添加课程。附加的 学生信息将通过一组检查提供 方框(如参与各种活动等) 所有关于学生的信息都将显示在文本区域中 组成部分 我的文本区域未显示在表单中。甚至信息也没有显示在文本区域中 我的代码如下: //运行程序Java 从文本字段接受学生信息并在组合框中显示的GUI应用程序,java,swing,Java,Swing,用户将输入全名、地址、城市、省、邮政编码、, 文本字段控件中的电话号码和电子邮件 学生的专业(计算机科学或商业)将被选择 从两个单选按钮 一个组合框将显示每个课程的课程列表 用户选择所需的程序。课程将添加到列表中 每当用户从相应的组合中选择课程时 盒子 确保用户不能多次添加课程。附加的 学生信息将通过一组检查提供 方框(如参与各种活动等) 所有关于学生的信息都将显示在文本区域中 组成部分 我的文本区域未显示在表单中。甚至信息也没有显示在文本区域中 我的代码如下: //运行程序 public st
public static void main(String[] args)
{
new Test();
}
//creating the main frame, and stick the other panels inside
public Test()
{
super("Student Information");
JFrame frame = new JFrame("Student Information");
BoxLayout bl = new BoxLayout(frame.getContentPane(),BoxLayout.Y_AXIS);
FlowLayout fl = new FlowLayout(FlowLayout.LEFT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(bl);
frame.setSize(800, 300);
//frame.setPreferredSize(new Dimension(1200,700));
panelTop = new JPanel();
panelBot = new JPanel();
panelTop.add(gridLayout(), "Left to right");
panelTop.add(midLayout(), "Left to right");
//panelTop.add(comboLayout(), "Left to right");
JPanel pane = new JPanel();
model = new DefaultListModel<String>();
courseList = new JList<>(model);
JScrollPane scrpn = new JScrollPane(courseList);
pane.setLayout(new GridLayout(3,1));
radioCS = new JRadioButton("Computer Science");
radioBus = new JRadioButton("Business");
radioCS.setSelected(true);
ButtonGroup btngrp = new ButtonGroup();
Person person = new Person();
radioCS.addActionListener(person);
radioBus.addActionListener(person);
JPanel topPane = new JPanel();
topPane.setLayout(new GridLayout(0, 2));
JPanel innerPane = new JPanel();
innerPane.setLayout(new GridLayout(2, 0));
btngrp.add(radioCS);
btngrp.add(radioBus);
topPane.add(radioCS);
topPane.add(radioBus);
courseList.setSize(500,500);
courseList.setPreferredSize(new Dimension(200,200));
comboBox.addItemListener(new ItemListener()
{
@Override
public void itemStateChanged(ItemEvent event)
{
if (event.getStateChange() == ItemEvent.SELECTED)
{
if (!s.equals(comboBox.getSelectedItem().toString()))
{
if (comboBox.getSelectedItem().toString().equals(options1[1]) && a == 0) {
a = 1;
model.addElement(comboBox.getSelectedItem().toString());
items += "\n" + comboBox.getSelectedItem().toString();
}
if (comboBox.getSelectedItem().toString().equals(options1[2]) && b == 0) {
b = 1;
model.addElement(comboBox.getSelectedItem().toString());
items += "\n" + comboBox.getSelectedItem().toString();
}
if (comboBox.getSelectedItem().toString().equals(options1[3]) && c == 0) {
c = 1;
model.addElement(comboBox.getSelectedItem().toString());
items += "\n" + comboBox.getSelectedItem().toString();
}
if (comboBox.getSelectedItem().toString().equals(options2[1]) && d == 0) {
d = 1;
model.addElement(comboBox.getSelectedItem().toString());
items += "\n" + comboBox.getSelectedItem().toString();
}
if (comboBox.getSelectedItem().toString().equals(options2[2]) && e == 0) {
e = 1;
model.addElement(comboBox.getSelectedItem().toString());
items += "\n" + comboBox.getSelectedItem().toString();
}
}
}
}
});
innerPane.add(comboBox);
innerPane.add(scrpn);
pane.add(topPane);
pane.add(innerPane);
panelBot.add(botLayout(), "Left to right");
submitBtn = new JButton("Submit");
panelBot.add(submitBtn);
frame.add(panelTop);
frame.add(panelBot);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
// Set a size to the the JTextArea
txtArea.setPreferredSize(new Dimension(400, 30));
submitBtn.addActionListener(this);
}
//grid layout panel for label and textboxes section
public static JPanel gridLayout() {
JPanel panel = new JPanel(new GridLayout(0, 2));
panel.setSize(500, 500);
panel.setPreferredSize(new Dimension(400, 400));
Label labelArr[] = new Label[] { new Label("Name: "), new Label("Address: "), new Label("Province: "),
new Label("City: "), new Label("Postal Code: "), new Label("Phone Number: "), new Label("Email: ") };
nameField = new JTextField();
addrField = new JTextField();
provField = new JTextField();
cityField = new JTextField();
postField = new JTextField();
phoneField = new JTextField();
emailField = new JTextField();
JTextField txtArr[] = new JTextField[] { nameField, addrField, provField, cityField, postField, phoneField,
emailField };
for (int i = 0; i < txtArr.length; i++) {
panel.add(labelArr[i]);
panel.add(txtArr[i]);
}
return panel;
} public static JPanel midLayout() {
JPanel panel = new JPanel(new FlowLayout());
checkSC = new JCheckBox("Student Council");
checkVW = new JCheckBox("Volunteer Work");
panel.add(checkSC);
panel.add(checkVW);
return panel;
}
public static JPanel botLayout()
{
JPanel panel = new JPanel(new FlowLayout());
// Add your txtArea inside a JScrollPane.
panel.add(new JScrollPane(txtArea));
return panel;
}
@Override
public void actionPerformed(ActionEvent e)
{
String name = nameField.getText();
String addr = addrField.getText();
String prov = provField.getText();
String post = postField.getText();
String phone = phoneField.getText();
String email = emailField.getText();
boolean isCS = radioCS.isSelected(); // If false then Business.
boolean isCouncil = checkSC.isSelected();
boolean isVolunteer = checkVW.isSelected();
ArrayList<String> courses = new ArrayList<String>();
for(int i=0; i<courseList.getModel().getSize(); i++) {
courses.add(courseList.getModel().getElementAt(i));
}
txtArea.setText(name + " (" + phone + ") " + email + ". " + addr + ", " + prov + ", " + post+"\r\n");
txtArea.append((isCS ? "Computer Science" : "Business")+". "+(isCouncil ? "Student Council, " : "")+(isVolunteer ? "Volunteer Work" : "")+". "+courses);
}
private class Person implements ActionListener
{
@Override
public void actionPerformed(ActionEvent arg0)
{
if (radioCS.isSelected()) {
comboBox.setModel(comboBox1);
}
if (radioBus.isSelected()) {
comboBox.setModel(comboBox2);
}
}
}
publicstaticvoidmain(字符串[]args)
{
新测试();
}
//创建主框架,并将其他面板粘贴到内部
公开考试()
{
超级(“学生信息”);
JFrame=新JFrame(“学生信息”);
BoxLayout bl=新的BoxLayout(frame.getContentPane(),BoxLayout.Y_轴);
FlowLayout fl=新的FlowLayout(FlowLayout.LEFT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
框架布局(bl);
框架设置尺寸(800300);
//框架。设置首选尺寸(新尺寸(1200700));
panelTop=新的JPanel();
panelBot=新的JPanel();
添加(gridLayout(),“从左到右”);
panelTop.add(midLayout(),“从左到右”);
//添加(comboLayout(),“从左到右”);
JPanel窗格=新的JPanel();
model=新的DefaultListModel();
courseList=新的JLList(模型);
JScrollPane scrpn=新的JScrollPane(课程列表);
窗格.setLayout(新的GridLayout(3,1));
radioCS=新的JRadioButton(“计算机科学”);
无线总线=新JRadioButton(“业务”);
radioCS.setSelected(正确);
ButtonGroup btngrp=新建ButtonGroup();
Person=新人();
radioCS.addActionListener(个人);
radioBus.addActionListener(个人);
JPanel-topPane=新的JPanel();
设置布局(新的网格布局(0,2));
JPanel innerPane=新的JPanel();
setLayout(新的GridLayout(2,0));
btngrp.add(无线电通信);
btngrp.add(无线总线);
添加(无线电通信);
添加(无线总线);
课程列表设置大小(500500);
courseList.setPreferredSize(新维度(200200));
comboBox.addItemListener(新的ItemListener()
{
@凌驾
公共无效itemStateChanged(ItemEvent事件)
{
if(event.getStateChange()==ItemEvent.SELECTED)
{
如果(!s.equals(comboBox.getSelectedItem().toString()))
{
if(comboBox.getSelectedItem().toString().equals(options1[1])&&a==0){
a=1;
model.addElement(comboBox.getSelectedItem().toString());
items+=“\n”+组合框。getSelectedItem().toString();
}
if(comboBox.getSelectedItem().toString().equals(options1[2])&&b==0){
b=1;
model.addElement(comboBox.getSelectedItem().toString());
items+=“\n”+组合框。getSelectedItem().toString();
}
if(comboBox.getSelectedItem().toString().equals(options1[3])&&c==0){
c=1;
model.addElement(comboBox.getSelectedItem().toString());
items+=“\n”+组合框。getSelectedItem().toString();
}
if(comboBox.getSelectedItem().toString().equals(options2[1])&&d==0){
d=1;
model.addElement(comboBox.getSelectedItem().toString());
items+=“\n”+组合框。getSelectedItem().toString();
}
if(comboBox.getSelectedItem().toString().equals(options2[2])&&e==0){
e=1;
model.addElement(comboBox.getSelectedItem().toString());
items+=“\n”+组合框。getSelectedItem().toString();
}
}
}
}
});
innerPane.add(组合框);
innerPane.add(scrpn);
窗格。添加(托潘);
添加(内部窗格);
add(botLayout(),“从左到右”);
submitBtn=新的JButton(“提交”);
panelBot.add(submitBtn);
框架。添加(面板顶部);
frame.add(panelBot);
frame.setLocationRelativeTo(空);
frame.pack();
frame.setVisible(true);
//为JTextArea设置一个大小
txtArea.setPreferredSize(新尺寸(400,30));
submitBtn.addActionListener(此);
}
//标签和文本框部分的网格布局面板
公共静态JPanel gridLayout(){
JPanel面板=新JPanel(新网格布局(0,2));
面板设置尺寸(500500);
面板。设置首选尺寸(新尺寸(400400));
标签labelArr[]=新标签[]{新标签(“名称”)、新标签(“地址”)、新标签(“省份”),
新标签(“城市:”),新标签(“邮政编码:”),新标签(“电话号码:”),新标签(“电子邮件:”)};
nameField=新的JTextField();
addrField=newjtextfield();
provField=新的JTextField();
cityField=new JTextField();
postField=新的JTextField();
phoneField=新的JTextField();
emailField=newjtextfield();
JTextField txtArr[]
// This is what is outside your methods alongside txtArea.
public static JTextArea txtArea = new JTextArea();
public static JTextField nameField, addrField, provField, cityField, postField, phoneField, emailField;
public static JRadioButton radioCS, radioBus;
public static JCheckBox checkSC, checkVW;
public static JList<String> courseList;
// This is what you need to change inside comboLayout()
radioCS = new JRadioButton("Computer Science");
radioBus = new JRadioButton("Business");
radioCS.setSelected(true);
ButtonGroup btngrp = new ButtonGroup();
JPanel topPane = new JPanel();
topPane.setLayout(new GridLayout(0, 2));
JPanel innerPane = new JPanel();
innerPane.setLayout(new GridLayout(2, 0));
btngrp.add(radioCS);
btngrp.add(radioBus);
topPane.add(radioCS);
topPane.add(radioBus);
// Changing midLayout()
public static JPanel midLayout() {
JPanel panel = new JPanel(new FlowLayout());
checkSC = new JCheckBox("Student Council");
checkVW = new JCheckBox("Volunteer Work");
panel.add(checkSC);
panel.add(checkVW);
return panel;
}
// Changing gridLayout()
public static JPanel gridLayout() {
JPanel panel = new JPanel(new GridLayout(0, 2));
panel.setSize(500, 500);
panel.setPreferredSize(new Dimension(400, 400));
Label labelArr[] = new Label[] { new Label("Name: "), new Label("Address: "), new Label("Province: "),
new Label("City: "), new Label("Postal Code: "), new Label("Phone Number: "), new Label("Email: ") };
nameField = new JTextField();
addrField = new JTextField();
provField = new JTextField();
cityField = new JTextField();
postField = new JTextField();
phoneField = new JTextField();
emailField = new JTextField();
JTextField txtArr[] = new JTextField[] { nameField, addrField, provField, cityField, postField, phoneField,
emailField };
for (int i = 0; i < txtArr.length; i++) {
panel.add(labelArr[i]);
panel.add(txtArr[i]);
}
return panel;
}
// This is what your submit action would look like in createAndShowGUI().
submitBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String name = nameField.getText();
String addr = addrField.getText();
String prov = provField.getText();
String post = postField.getText();
String phone = phoneField.getText();
String email = emailField.getText();
boolean isCS = radioCS.isSelected(); // If false then Business.
boolean isCouncil = checkSC.isSelected();
boolean isVolunteer = checkVW.isSelected();
List<String> courses = new ArrayList<String>();
for(int i=0; i<courseList.getModel().getSize(); i++) {
courses.add(courseList.getModel().getElementAt(i));
}
txtArea.setText(name + " (" + phone + ") " + email + ". " + addr + ", " + prov + ", " + post+"\r\n");
txtArea.append((isCS ? "Computer Science" : "Business")+". "+(isCouncil ? "Student Council, " : "")+(isVolunteer ? "Volunteer Work" : "")+". "+courses);
}
});