JavaGUI------------常用的组件(单选、复选框、下拉列表)

目录

1.单选按钮

2.复选组件框

3.下拉列表


1.单选按钮

类:JRadioButton ButtonGroup

说明:JRadioButton 是一个单选按钮,需要将单选按钮加入到按钮组中

构造方法:

  • new JRadioButton();
  • new JRadioButton(ICon icon);//指定图标
  • new JRadioButton(Icon icon,boolean selscted);//指定图标+是否选中
  • new JRadioButton(String text);//指定文字(标黄表示常用)
  • new JRadioButton(String text,Icon icon);//指定文字+图标
  • new JRadioButton(String text,Icon icon,boolean selected);//指定文字+图标+是否选中

示例:

import javax.swing.*;
import java.awt.*;

public class JradioButton {
    public static void main(String[] args){
        JFrame jf=new JFrame("JRadioButton");
        jf.setLayout(new FlowLayout());
        jf.setBounds(400,300,400,300);

        JRadioButton jrb1=new JRadioButton("男");
        JRadioButton jrb2=new JRadioButton("女");
        ButtonGroup group=new ButtonGroup();
        group.add(jrb1);
        group.add(jrb2);

        jf.add(jrb1);
        jf.add(jrb2);

        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

 

2.复选组件框

类:JChenckBox

构造方法:

  • new JCheckBox();
  • new JCheckBox(Icon icon,boolean checked);//指定图标+是否被选中
  • new JCheckBox(String text,boolean checked);//指定文字+是否被选中

示例: 

import javax.swing.*;
import java.awt.*;

public class JcheckBox {
    public static void main(String[] args) {
        JFrame jf = new JFrame("JRadioButton");
        jf.setLayout(new FlowLayout());
        jf.setBounds(400, 300, 400, 300);

        JCheckBox box = new JCheckBox("睡觉", true);
        JCheckBox box1 = new JCheckBox("吃饭", false);
        JCheckBox box2 = new JCheckBox("跳舞", true);
        JCheckBox box3 = new JCheckBox("玩游戏", false);
        jf.add(box);
        jf.add(box1);
        jf.add(box2);
        jf.add(box3);

        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

3.下拉列表

类:JComboBox

构造方法:

  • new JComboBox();
  • new JComboBox(ComboBoxModel dataModel);//使用listModel‘建立一个下拉列表
  • new JComboBox(Object[] arrayData);//数组
  • new JComboBox(Vector vector);//Vector类的对象可以看作是一个可变大小的数组

方法:

addItem 添加下拉内容

示例: 

import javax.swing.*;
import java.awt.*;

public class JCombobox {
    public static void main(String[] args) {
        JFrame jf = new JFrame("JRadioButton");
        jf.setLayout(new FlowLayout());
        jf.setBounds(400, 300, 400, 300);

        JComboBox box=new JComboBox();
        box.addItem("高中");
        box.addItem("大学");
        box.addItem("研究生");
        box.addItem("博士");
        jf.add(box);

        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
整理了Java GUI编程基础知识! public class AllFrame { /** * 这里的代码涉及到 GUI 编程基本面板、基本组件、事件(ActionListener、ChangeListener、MouseListener) */ public AllFrame() { // new 一个主窗体 也可以这样写 new MainFrame(); MainFrame mf = new MainFrame(); } public static void main(String[] args) { new AllFrame(); } // 主窗体 继承 窗体基本格式 类 Jframe 以下弹出窗口均继承 Jframe class MainFrame extends Jframe implements ActionListener { JMenuBar menubar = new JMenuBar();;// 菜单条 JMenu jmenu[] = new JMenu[5];// panelMenu,usedMenu1,usedMenu2,layoutMenu,helpMenu; // 菜单项 JButton closebtn;// 关闭窗体 // 具体的选项 JMenuItem panelitem[] = new JMenuItem[4]; JMenuItem useditem1[] = new JMenuItem[6]; JMenuItem useditem2[] = new JMenuItem[6]; JMenuItem layoutitem[] = new JMenuItem[3]; JMenuItem helpitem = new JMenuItem("关于"); // 菜单中名称 String menu[] = { "面板使用", "常用组件一", "常用组件二", "布局设置", "帮助" }; String pane[] = { "普通面板", "滚动面板", "分割面板", "选项面板" }; String uesd1[] = { "标签", "按钮", "文本框", "文本域", "单选按钮" ,"微调"}; String uesd2[] = { "滑块","复选框", "下拉菜单", "列表", "表格", "树状" }; String layout[] = { "流式布局", "边界布局", "网格布局" }; // Jpanelitem,Jscrollpaneitem,Splitpaneitem,JTabbedpanelitem, // Jlabelitem,Jbuttonitem,JTextfielditem,JTextareaitem,JRadiobuttonitem, // JCheckboxitem,JCombomoxitem,Jlistitem,Jtableitem,Jtreeitem, // Flowlayoutitem,Borderlayoutitem,Girdlayoutitem, // Helpitem}; public MainFrame() { this.setTitle("JAVA图型界面学习(GUI)"); // 加载菜单条到窗体中 this.setJMenuBar(menubar); // 重新设置大小 this.setSize(380, 290); // 简单的居中方法 在窗体基本格式类 Jframe 中有详细的居中方法 this.setLocationRelativeTo(null); // 设置关闭窗口 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 调用方法 menu(); // 设置容器 Container con = this.getContentPane(); // 定义一个 标签 “JAVA图型界面学习” JLabel jl = new JLabel("JAVA图型界面学习"); // 组件 设置字体大小, 其他组件用法相同 jl.setFont(new Font("隶书", Font.BOLD, 24)); // 具体定位 组件名.setBounds(x, y, width, height) jl.setBounds(30, 30, 280, 50); // 把标签加到 con 容器中 con.add(jl); /

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值