我对JTextField
,JRadioButton
有疑问。我无法使用TextArea
注册它们。我需要输入姓名,年龄和薪水,并在TextArea
上显示。
package gui2;
import java.awt.*;
import javax.swing.*;
public class GUI2 {
JPanel gui = new JPanel(new BorderLayout(2, 2));
JPanel labelFields = new JPanel(new BorderLayout(2, 2));
JPanel labels = new JPanel(new GridLayout(0, 1, 3, 4));
JPanel fields = new JPanel(new GridLayout(0, 1, 3, 4));
/* JTextField name1 = new JTextField(20);
JTextField age1 = new JTextField(20);
JTextField s = new JTextField(20);*/
JPanel guiCenter = new JPanel(new BorderLayout(1, 0));
/* JRadioButton male = new JRadioButton("male");
JRadioButton female = new JRadioButton("Female");*/
JPanel gu = new JPanel(new FlowLayout());
// JTextArea e = new JTextArea(5, 30);
GUI2() {
labelFields.setBorder(new TitledBorder("Employee"));
labels.setBorder(new TitledBorder(" Info "));
fields.setBorder(new TitledBorder("Please enter your info"));
labels.add(new JLabel("Name"));
fields.add(name1);
labels.add(new JLabel("Age"));
fields.add(age1);
labels.add(new JLabel("Salary"));
fields.add(s);
labelFields.add(labels, BorderLayout.CENTER);
labelFields.add(fields, BorderLayout.EAST);
guiCenter.setBorder(new TitledBorder("Gender"));
ButtonGroup bG = new ButtonGroup();
bG.add(male);
bG.add(female);
guiCenter.add(male, BorderLayout.WEST);
guiCenter.add(female, BorderLayout.CENTER);
gu.setBorder(new TitledBorder("Employee Info"));
gu.add(new JScrollPane(e));
gui.add(guiCenter, BorderLayout.CENTER);
gui.add(labelFields, BorderLayout.NORTH);
gui.add(gu, BorderLayout.SOUTH);
JOptionPane.showMessageDialog(null, gui);
int input = JOptionPane.showOptionDialog(null, null, null, JOptionPane.OK_OPTION, JOptionPane.INFORMATION_MESSAGE, null, null, null);
for (int i =0 ; i < JOptionPane.OK_OPTION ; i++){
if(input == JOptionPane.OK_OPTION) {
e.setText(age1.getText());
e.setText(name1.getText());
e.setText(s.getText());
}
}
}
public static void main(String[] args) throws Exception {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new GUI2();
}
} );
}
}
答案 0 :(得分:0)
您正在覆盖JTextArea的内容 e
e.setText(e.getText()+" "+age1.getText());
e.setText(e.getText()+" "+name1.getText());
e.setText(e.getText()+" "+s.getText());
PS:我不知道你是否知道你在做什么,但你的textFields没有被声明,因为声明是在评论中。