java第八次上机
1.求和并清除
1 package smy.window; 2 import javax.swing.*; 3 import java.awt.*; 4 import java.awt.event.ActionEvent; 5 import java.awt.event.ActionListener; 6 7 public class plus implements ActionListener{ 8 JFrame f; 9 JPanel p; 10 JLabel l1,l2,l3,l4; 11 JButton b1,b2; 12 JTextField[] t; 13 public plus(){ 14 f = new JFrame("求和"); 15 p = new JPanel(); 16 b1 = new JButton("求和"); 17 b2 = new JButton("清除"); 18 l1 = new JLabel("加数1"); 19 l2 = new JLabel("加数2"); 20 l3 = new JLabel(" "); 21 l4 = new JLabel(" "); 22 t = new JTextField[3]; 23 for(int i=0;i<t.length;i++){ 24 t[i] = new JTextField(5); 25 } 26 27 f.setVisible(true); 28 f.setLocationRelativeTo(null); 29 f.setSize(400,220); 30 f.add(p); 31 p.setLayout(new GridLayout(3,3)); 32 p.add(l1); 33 p.add(t[0]); 34 p.add(l3); 35 p.add(l2); 36 p.add(t[1]); 37 p.add(l4); 38 p.add(b1); 39 p.add(t[2]); 40 p.add(b2); 41 42 b1.addActionListener(this); 43 b2.addActionListener(this); 44 } 45 public static void main(String[] args) { 46 new plus(); 47 } 48 49 public void actionPerformed(ActionEvent e) { 50 if(e.getSource()==b1){ 51 int m,n; 52 m=Integer.parseInt(t[0].getText()); 53 n=Integer.parseInt(t[1].getText()); 54 t[2].setText(m+n+"");} 55 else if(e.getSource()==b2){ 56 for(int i=0;i<t.length;i++){ 57 t[i].setText(null);} 58 } 59 } 60 61 }
运行界面
2.信息录入
package bbb; import javax.swing.*; import java.awt.*; public class imformation { JFrame f; JPanel p; JLabel [] l; String[] title = {"姓名: ", "性别: ", "爱好: ", "籍贯: ", " ", " ", " ", " ", " ", " "}; JButton b; JTextField t; JRadioButton [] r; //单选按钮 String[] title1 = {"男", "女"}; JCheckBox [] c; //复选框 3个 String[] title2 = {"体育", "音乐", "美术"}; JComboBox cb; JTextArea a; public imformation(){ f = new JFrame("信息录入"); p = new JPanel(); t = new JTextField(9); b = new JButton("确定"); a = new JTextArea(); l = new JLabel[10]; cb = new JComboBox(); for(int i=0;i<l.length;i++){ l[i] = new JLabel(title[i]); } r = new JRadioButton[2]; for(int j=0;j<r.length;j++){ r[j] = new JRadioButton(title1[j]); } c = new JCheckBox[3]; for(int k=0;k<c.length;k++){ c[k] = new JCheckBox(title2[k]); } f.setVisible(true); f.setLocationRelativeTo(null); f.setSize(450,450); f.add(p,BorderLayout.NORTH); p.setLayout(new GridLayout(5,3)); p.add(l[0]); p.add(t); p.add(l[4]); p.add(l[5]); p.add(l[1]); p.add(r[0]); p.add(r[1]); p.add(l[6]); p.add(l[2]); p.add(c[0]); p.add(c[1]); p.add(c[2]); p.add(l[3]); p.add(cb); p.add(l[7]); p.add(l[8]); //p.add(l[9]); p.add(b); f.add(a,BorderLayout.CENTER); } public static void main(String[] args) { new imformation(); } }