zoukankan      html  css  js  c++  java
  • 综合运用开关按钮JToggleButton、复选框JCheckBox、单选框JRadioButton和按钮ButtonGroup,设计如下界面

    package ButtonGroupDemo;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;
    class ButtonGroupDemo{
    private JFrame f=new JFrame("按钮综合应用");
    private JPanel pc=new JPanel(new GridLayout(1,3));
    private JPanel p1=new JPanel(new GridLayout(4,1,0,20));
    private JPanel p2=new JPanel(new GridLayout(3,1,0,30));
    private JPanel p3=new JPanel(new GridLayout(6,1,0,5));
    private JPanel pb=new JPanel(new GridLayout(1,2,10,5));
    private JLabel display=new JLabel();
    Font ft=new Font("Serif",Font.BOLD,18);
    ButtonGroup bg1=new ButtonGroup();
    ButtonGroup bg2=new ButtonGroup();
    JRadioButton cb1=new JRadioButton("学生");
    JRadioButton cb2=new JRadioButton("教师");
    JRadioButton cb3=new JRadioButton("管理人员");
    JRadioButton cb4=new JRadioButton("男");
    JRadioButton cb5=new JRadioButton("女");
    JCheckBox cb6=new JCheckBox("旅游");
    JCheckBox cb7=new JCheckBox("看书");
    JCheckBox cb8=new JCheckBox("上网");
    JCheckBox cb9=new JCheckBox("运动");
    JCheckBox cb10=new JCheckBox("交友");
    JToggleButton jtb1=new JToggleButton("屏蔽右边的按钮",true);
    JToggleButton jtb2=new JToggleButton("显示你选择的结果",true);
    private String st="学生";
    private String sx="男";
    private String hobby="旅游";
    public static void main(String args[]){
    ButtonGroupDemo that=new ButtonGroupDemo();
    that.go();
    }
    void go(){
    f.getContentPane().setLayout(new BorderLayout(0,10));
    f.getContentPane().add("North",pc);
    pc.add(p1);
    pc.add(p2);
    pc.add(p3);
    pc.add(new Label("身份"));p1.add(cb1);
    bg1.add(cb1);
    cb1.setSelected(true);
    p1.add(cb2);
    bg1.add(cb2);
    p1.add(cb3);bg1.add(cb3);
    p2.add(new Label("性别"));
    p2.add(cb4);bg2.add(cb4);
    cb4.setSelected(true);
    p2.add(cb5);bg2.add(cb5);
    p3.add(new Label("爱好"));
    p3.add(cb6);
    cb6.setSelected(true);
    p3.add(cb7);p3.add(cb8);
    p3.add(cb9);p3.add(cb10);
    f.getContentPane().add("Center",pb);
    pb.add(jtb1);
    pb.add(jtb2);
    jtb1.setFont(ft);
    jtb2.setFont(ft);
    f.getContentPane().add("South",display);
    display.setFont(ft);
    display.setText("您的身份是"+st+","+sx+"性,爱好有"+hobby);
    jtb1.addActionListener(new ButtonH(1));
    jtb2.addActionListener(new ButtonH(2));
    f.addWindowListener(new WindowHandler());
    f.setSize(420,300);
    f.setResizable(true);f.setVisible(true);
    }//go方法结束
    class ButtonH implements ActionListener{
    int sel;
    ButtonH(int select){
    sel=select;
    }
    public void actionPerformed(ActionEvent e){
    if(sel==1)
    if(jtb1.isSelected())
    {jtb1.setText("屏蔽右边的按钮");jtb2.setEnabled(true);}
    else
    {jtb1.setText("启用右边的按钮");jtb2.setEnabled(false);}
    if(sel==2)
    {
    Enumeration er;
    er=bg1.getElements();
    for(;er.hasMoreElements();){
    JRadioButton jrb=(JRadioButton)er.nextElement();
    if(jrb.isSelected())
    {st=jrb.getText();break;}
    }
    er=bg2.getElements();
    for(;er.hasMoreElements();)
    {
    JRadioButton jrb=(JRadioButton)er.nextElement();
    if(jrb.isSelected())
    {sx=jrb.getText();break;}
    }
    hobby="";
    if(cb6.isSelected())hobby=cb6.getText();
    if(cb7.isSelected())hobby=hobby+cb7.getText();
    if(cb8.isSelected())hobby=hobby+cb8.getText();
    if(cb9.isSelected())hobby=hobby+cb9.getText();
    if(cb10.isSelected())hobby=hobby+cb10.getText();
    display.setText("您的身份证是"+st+","+"性,您爱好"+hobby);
    jtb2.setSelected(false);
    }//结束if(sel==2)
    }
    }
    class WindowHandler extends WindowAdapter{
    public void windowClosing(WindowEvent e){
    System.exit(1);
    }
    }
    }

  • 相关阅读:
    poj 1789 每个字符串不同的字母数代表两个结点间的权值 (MST)
    poj 1251 poj 1258 hdu 1863 poj 1287 poj 2421 hdu 1233 最小生成树模板题
    poj 1631 最多能有多少条不交叉的线 最大非降子序列 (LIS)
    hdu 5256 最少修改多少个数 能使原数列严格递增 (LIS)
    hdu 1025 上面n个点与下面n个点对应连线 求最多能连有多少条不相交的线 (LIS)
    Gym 100512F Funny Game (博弈+数论)
    UVa 12714 Two Points Revisited (水题,计算几何)
    UVa 12717 Fiasco (BFS模拟)
    UVa 12718 Dromicpalin Substrings (暴力)
    UVa 12716 && UVaLive 6657 GCD XOR (数论)
  • 原文地址:https://www.cnblogs.com/liao-pxsoftware15/p/7529194.html
Copyright © 2011-2022 走看看