Java GUI常用组件介绍

70 篇文章 3 订阅
7 篇文章 1 订阅

注意:

    所有组件创建并设置完成后都必须使用add()方法添加到窗口中,否则无法显示

       jp.add(jButton4);   //将设置好的按钮添加到窗口
        this.add(jp);
  • JLable(标签)  //用来显示文字或图片

            JPanel jp = new JPanel();
    ​
            jp.setLayout(new FlowLayout(FlowLayout.CENTER));
            JLabel accountLabel = new JLabel("账号");
                   accountLabel.setFont(new Font("宋体",Font.BOLD,20));
                  // accountLabel.setIcon(new ImageIcon("微信图片_20211117191028.jpg"));
            jp.add(accountLabel);

    效果图如下:

  • JtextField(文本框) //用来输入文字

     JTextField accountText = new JTextField(20);
            jp.add(accountText);

    效果图如下:

  • JPasswordField(密码框) //用来输入密码

        JLabel passwordLabel = new JLabel("密码");
                   passwordLabel.setFont(new Font("宋体",Font.BOLD,20));
            jp.add(passwordLabel);
            JPasswordField jPasswordField = new JPasswordField(20);
            jp.add(jPasswordField);

    效果图如下:

 

  • JTextArea(多行文本框) /用来输入多行文字

      JTextArea jTextArea = new JTextArea(5,20);
          //滚动面板,可出现滚动条,使多行文本框大小不发生变化
            JScrollPane jScrollPane = new JScrollPane(jTextArea);
            jp.add(jScrollPane);

    效果图如下:

 

  • JMenuBar(菜单栏)      //用来创建一个菜单栏

     JMenuBar jMenuBar = new JMenuBar();
  • JMenu(菜单)      //用来创建一个菜单

     JMenu jMenu1 = new JMenu("文件");
            JMenu jMenu2 = new JMenu("编辑");
            jMenuBar.add(jMenu1);
            jMenuBar.add(jMenu2);
  • JMenuItem(菜单项)      //用来创建菜单项

      JMenuItem jMenuItem = new JMenuItem("新建");
            jMenu1.add(jMenuItem);

    菜单操作效果图如下:

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 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
发出的红包

打赏作者

不会写代码的菜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值