建一个窗体,添加组件标签伩文本域文本区/按钮,用以录入 Stundent信息:姓名/性别/学号及高等数学
在文本区显示该学生信息,运行结果如下图所示
提示:1.除提供源程序外,必须提供程序运行截囱
package Exam; import java.awt.Container; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.lang.reflect.InvocationTargetException; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.SwingUtilities; public class Main { public void init() { JFrame frame = new JFrame("学生信息"); frame.setSize(600, 400); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container container = frame.getContentPane(); container.setLayout(new FlowLayout() ); JLabel label_name = new JLabel("姓名"); JLabel label_sex = new JLabel("性别"); JLabel label_id = new JLabel("学号"); JLabel label_math = new JLabel("高等数学"); JLabel label_eng = new JLabel("英语"); JTextField field_name = new JTextField(" "); JTextField field_sex = new JTextField(" "); JTextField field_id = new JTextField(" "); JTextField field_math = new JTextField(" "); JTextField field_eng = new JTextField(" "); JTextArea textArea = new JTextArea(20,40); textArea.setSize(600, 200); JButton button = new JButton("显示信息"); button.setSize(100, 200); container.add(label_name); container.add(field_name); container.add(label_sex); container.add(field_sex); container.add(label_id); container.add(field_id); container.add(label_math); container.add(field_math); container.add(label_eng); container.add(field_eng); container.add(button); container.add(textArea); frame.setVisible(true); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String name = field_name.getText(); String sex = field_sex.getText(); String id = field_id.getText(); String math = field_math.getText(); String eng = field_eng.getText(); double avg=(Double.parseDouble(math)+Double.parseDouble(eng))/2; textArea.append(name+" "+sex+" "+id+" "+math+" "+eng+" 平均分"+avg); } }); } public static void main(String[] args) { try { SwingUtilities.invokeAndWait(new Runnable() { @Override public void run() { new Main().init(); } }); } catch (InvocationTargetException | InterruptedException e) { e.printStackTrace(); } } }