Java第四次上机


package daima;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
public class table implements ActionListener {
    int i=1;
    JFrame f;                      
    JPanel p;
    JButton  b;
    JLabel  l1,l2,l3,l4,l5,l6,l7,l8,l9,l10;
    JTextField  t1;
    JTextArea  a;             //文本区
    ButtonGroup group;           //按钮组 (把按钮绑在一起,使其只能选择一个)
    JRadioButton  r1,r2;      //单选按钮
    JCheckBox   ch1,ch2,ch3;     //复选框
    JComboBox cb;                //选择框
    GridLayout gl;             //网格布局
    String  title[]= {"湖南","湖北","浙江","北京","云南","上海","广东"};
    public table() {
        f=new JFrame("信息录入");
        p=new JPanel();
        
        b=new JButton("确定");
        b.addActionListener(this);          //对按钮加监听
        
        l1=new JLabel("姓名:");
        l2=new JLabel();
        l3=new JLabel();
        l4=new JLabel("性别:");
        l5=new JLabel();
        l6=new JLabel("爱好:");
        l7=new JLabel("籍贯:");
        l8=new JLabel();
        l9=new JLabel();
        l10=new JLabel();
        
        t1=new JTextField(5);
        
        a=new JTextArea();
        
        group=new ButtonGroup();
        r1=new JRadioButton("男",true);   //构造方法JRadioButton(String ,boolean)  前者表示按钮名称,或者表示默认按钮是否被选中
        r1.addActionListener(this);
        r2=new JRadioButton("女");        //构造方法JRadioButton(String)
        r2.addActionListener(this);      //对单选按钮加监听
        
        ch1=new JCheckBox("体育");        //构造方法JCheckBox(String)   参数表示按钮的名称
        ch1.addActionListener(this);
        ch2=new JCheckBox("音乐");
        ch2.addActionListener(this);
        ch3=new JCheckBox("美术");
        ch3.addActionListener(this);     //分别对复选按钮加监听
        
        cb=new JComboBox(title);         //选择框    选项为title字符串数组    
        
        
        gl=new GridLayout(5,3);         //网格布局设为5行3列
        p.setLayout(gl);                //将面板默认的流布局改为网格布局
        
        
        p.add(l1);
        p.add(t1);
        p.add(l2);
        p.add(l3);
        
        p.add(l4);
        p.add(r1);
        p.add(r2);
        group.add(r1);              
        group.add(r2);        //p.add() 括号里放的应该是组件,而ButtonGroup不是组件,不能放到容器中。ButtonGroup只是一个作用域
        p.add(l5);
        
        p.add(l6);
        p.add(ch1);
        p.add(ch2);
        p.add(ch3);
        
        p.add(l7);
        p.add(cb);
        p.add(l8);
        p.add(l9);
        
        p.add(l10);
        p.add(b);
        
        f.add(p,BorderLayout.NORTH);    
        f.add(a,BorderLayout.CENTER);
        f.setSize(500,300);            
        f.setVisible(true);         //使窗口可见
        a.setLineWrap(true);       //表示自动换行  既当文字比控件的宽度还长时会自动换行
}
    public static void main(String[]args) {
        new table();
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        String   w=a.getText(),s="",c1="",c2="",c3="";
        if(r1.isSelected()) {
            s=r1.getText();
        }
        if(r2.isSelected()) {        //单选按钮是类 AbstractButton的一个子类,方法isSelected() 返回按钮的状态
             s=r2.getText();
        }
        if(ch1.isSelected()) {
            c1=ch1.getText();
        }
        if(ch2.isSelected()) {
            c2=ch2.getText();
        }
        if(ch3.isSelected()) {
            c3=ch3.getText();
        }
        if(e.getSource()==b) {       //判断是否选择此按钮
            a.setText(w+"第"+i+"位学生的信息为:"+"
"+"姓名是:"+t1.getText()+" 性别是:"+s+" 爱好为:"+c1+c2+c3+" 籍贯为:"+cb.getSelectedItem()+"
");
            i++;     //getSelectedItem()  表示获取选择框的内容
           
                      
        }
            
        }}
        
    

package daima;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class plusf implements ActionListener {
     JFrame f;
     JPanel p;
     JButton b1,b2;
     JLabel l1,l2,l3,l4;
     JTextField  t1,t2,t3;
     GridLayout a;
     public plusf(){
      f=new JFrame("加法器");
      p=new JPanel();
      b1=new JButton("   求和");
      b1.addActionListener(this);
      b2=new JButton("   清除");
      b2.addActionListener(this);
      l1=new JLabel("               加数1");
      l2=new JLabel();
      l3=new JLabel("               加数2");
      l4=new JLabel();
      t1=new JTextField(5);
      t2=new JTextField(5);
      t3=new JTextField(5);
      a=new GridLayout(3,3);
      p.setLayout(a);
      p.add(l1);
      p.add(t1);
      p.add(l2);
      p.add(l3);
      p.add(t2);
      p.add(l4);
      p.add(b1);
      p.add(t3);
      p.add(b2);
      f.add(p);
      f.setSize(500,200);
      f.setVisible(true); 
      p.setBackground(Color.pink);
      b2.setBackground(Color.red);
      b1.setBackground(Color.blue);
     }
    public static void main(String[]args){
      new plusf();
    }
    public void actionPerformed(ActionEvent e) {
  //getSource()方法是区分响应的是哪一个按钮
     if(e.getSource()==b1){
     int n;
     int b=Integer.parseInt(t1.getText());
     int c=Integer.parseInt(t2.getText());
     n=c+b;
     t3.setText(n+"");  //+是字符串连接符
    }
  if(e.getSource()==b2){
   t1.setText("");
   t2.setText("");
   t3.setText("");   
    }
  
 }
     
}

原文地址:https://www.cnblogs.com/lusilin/p/10961024.html