用户将输入全名,地址,城市,省份,邮政编码, 电话号码和电子邮件在文本字段控件中。
将选择学生的专业(计算机科学或商业) 从两个单选按钮。
组合框将随时显示每个程序的课程列表 用户选择所需的程序。课程将添加到列表中 每当用户从相应的组合中选择课程时,框就会出现 框。
确保用户无法多次添加课程。额外 有关学生的信息将通过一组支票提供 盒子(如参与各种活动等)。
有关学生的所有信息都将显示在文本区域中 成分
我的TextArea未显示在表单中。甚至信息也没有显示在textarea中。
我的代码如下:
//运行prog
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);
}
}
}
答案 0 :(得分:0)
您要将txtArea
添加到同一个面板两次。由于您先在botLayout()
中添加,因此可以在panelBot.add(txtArea);
方法中删除createAndShowGUI()
。您的txtArea没有任何文本,并且不在JScrollPane中,因此不会出现任何内容。文本未添加到txtArea
的原因是因为TextField
在您输入时没有要监听的操作。您不希望在您的字段上使用侦听器,因为它会产生无意义的输出。根据您的描述,您的JTextFields,JRadioButtons,JCheckBoxes和JList需要在方法之外声明,以便可以在submitBtn
侦听器中访问它们。从那里,您现在可以访问用户输入的内容并将其放在txtArea
内。
// 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);
}
});