首页 > 其他分享 >GUI--JFrame学习01(基本控件)

GUI--JFrame学习01(基本控件)

时间:2023-11-21 23:55:25浏览次数:27  
标签:控件 setBounds JFrame -- add new JLabel root public

[Java进阶] Swing两万字大总结一(超详细教程,这不得收藏一波)_swing 教程_程序喵正在路上的博客-CSDN博客

1.创建第一个JFrame窗体

package learn;

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

public class MyFrame01 extends JFrame {

    public void CreateJFrame(String title){
        //实例化
        JFrame frame = new JFrame(title);
        //获取容器
        Container container = frame.getContentPane();
        container.setBackground(Color.CYAN);

        JLabel jLabel = new JLabel("这是一个JFrame窗体");
        //使标签上的文字居中
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);
        //将标签添加到容器中
        container.add(jLabel);

        //窗体可视
        frame.setVisible(true);
        //设置窗体的位置以及大小
        frame.setBounds(400,300,400,300);
        //设置关闭方式
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new MyFrame01().CreateJFrame("第一个JFrame窗体");
    }
}

 

 2.JDialog窗体

 

package learn;

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

/**
 * MyFrame+MyDialog
 */
public class MyFrame02 extends JFrame {
    public MyFrame02(){
        //创建一个容器
        Container container = getContentPane();
        container.setLayout(null);
        JLabel label = new JLabel("第二个JFrame窗体");
        label.setHorizontalAlignment(SwingConstants.CENTER);
        container.add(label);
        JButton button = new JButton("点击出现对话框");
        button.setBounds(10,10,150,20);
        //按钮添加点击事件
        button.addActionListener(e->{
            new MyDialog(MyFrame02.this).setVisible(true);
        });
        container.add(button);
        container.setBackground(Color.GRAY);
        setSize(400,300);
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String[] args) {
        new MyFrame02();
    }
}

class MyDialog extends JDialog {

    public MyDialog(MyFrame02 frame02){
        //实例化JDialog类对象,指定父窗体、标题、类型
        super(frame02, "JDialog窗体", true);

        Container contentPane = getContentPane();
        contentPane.add(new JLabel("第一个对话框"));
        setBounds(120,120,150,100);
    }
}

 

 3.JLabel标签

 

package learn;

import javax.swing.*;

/**
 * JLabel
 */
public class MyFrame03 extends JFrame {
    JLabel label;
    public MyFrame03(){
        setTitle("添加标签");
        label=new JLabel("第一个JLabel",JLabel.CENTER);
        label.setIcon(new ImageIcon("D:\\javaidea\\work01\\other\\guicha7\\src\\learn\\imgs\\img1.png"));
        label.setHorizontalTextPosition(JLabel.CENTER);
        label.setVerticalTextPosition(JLabel.BOTTOM);
        //设置标签不可用
        label.setEnabled(false);
        //当标签不可用的时候显示的图像
        //注释这一行后显示的图像是不可用即是灰色的
//        label.setDisabledIcon(new ImageIcon("D:\\javaidea\\work01\\other\\guicha7\\src\\learn\\imgs\\img2.png"));
        add(label);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setBounds(400,300,800,650);
        setVisible(true);
    }

    public static void main(String[] args) {
        new MyFrame03();
    }
}

4.普通按钮、单选按钮、复选框

package learn;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.io.File;
import java.io.IOException;

/**
 * JButton,JRadioButton,JCheckBox
 */
public class MyFrame04 extends JFrame {

    JPanel root;
    JButton msgBtn,imgBtn;
    JRadioButton radioButton1,radioButton2,radioButton3,radioButton4;
    JCheckBox checkBox1,checkBox2,checkBox3,checkBox4;
    public MyFrame04() throws IOException {
        //定义面板容器
        root = new JPanel();
        setContentPane(root);
        //设置面板是绝对布局
        setLayout(null);


        //普通按钮以及添加图片的按钮
        msgBtn = new JButton("普通按钮");
        msgBtn.setBounds(54,68,100,40);
        root.add(msgBtn);
        ImageIcon icon = new ImageIcon(ImageIO.read(new File("D:\\javaidea\\work01\\other\\guicha7\\src\\learn\\imgs\\img1.png")));
        imgBtn = new JButton(icon);
        imgBtn.setBounds(196,40,120,120);
        root.add(imgBtn);


        //单选按钮
        radioButton1 = new JRadioButton("学习");
        radioButton2 = new JRadioButton("玩游戏");
        radioButton3 = new JRadioButton("运动");
        radioButton4 = new JRadioButton("听音乐");
        radioButton1.setBounds(45,200,73,23);
        radioButton2.setBounds(134,200,73,23);
        radioButton3.setBounds(45,250,73,23);
        radioButton4.setBounds(134,250,73,23);
        root.add(radioButton1);
        root.add(radioButton2);
        root.add(radioButton3);
        root.add(radioButton4);
        //定义单选按钮组控件
        ButtonGroup group = new ButtonGroup();
        group.add(radioButton1);
        group.add(radioButton2);
        group.add(radioButton3);
        group.add(radioButton4);

        //复选框
        checkBox1 = new JCheckBox("C");
        checkBox2 = new JCheckBox("C++");
        checkBox3 = new JCheckBox("Java");
        checkBox4 = new JCheckBox("Python");
        checkBox1.setBounds(45,300,73,23);
        checkBox2.setBounds(134,300,73,23);
        checkBox3.setBounds(45,350,73,23);
        checkBox4.setBounds(134,350,73,23);
        root.add(checkBox1);
        root.add(checkBox2);
        root.add(checkBox3);
        root.add(checkBox4);

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setBounds(400,300,400,450);
        setVisible(true);
    }

    public static void main(String[] args) throws IOException {
        new MyFrame04();
    }
}

 5.文本框、密码框

package learn;

import javax.swing.*;

/**
 * JTextField、JPasswordField
 */
public class MyFrame05 extends JFrame {
    JPanel root;
    JLabel unameLabel,upwdLabel;
    JTextField unameText;
    JPasswordField upwd;
    JButton loginBtn,exitBtn;

    public MyFrame05(){
        //定义面板容器
        root =new JPanel();
        setContentPane(root);
        setLayout(null);//设置绝对布局

        unameLabel = new JLabel("用户名: ");
        unameLabel.setBounds(52,33,54,15);
        root.add(unameLabel);
        unameText = new JTextField(12);
        unameText.setBounds(116,30,139,21);
        root.add(unameText);
        upwdLabel = new JLabel("密 码: ");
        upwdLabel.setBounds(52,74,54,15);
        root.add(upwdLabel);

        //密码框
        upwd = new JPasswordField(12);
        upwd.setBounds(116,71,139,21);
        //设置回显字符
        upwd.setEchoChar('●');
        root.add(upwd);

        loginBtn = new JButton("登录");
        loginBtn.setBounds(64,116,69,23);
        root.add(loginBtn);
        exitBtn = new JButton("退出");
        exitBtn.setBounds(174,116,69,23);
        root.add(exitBtn);


        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setBounds(400,300,340,256);
        setVisible(true);

    }

    public static void main(String[] args) {
        new MyFrame05();
    }

}

 6.JTextArea、JComboBox

package learn;

import javax.swing.*;

/**
 * JTextArea、JComboBox
 */
public class MyFrame06 extends JFrame {
    JPanel root;
    JTextArea textArea;
    JComboBox comboBox;
    JLabel msgLabel;
    public MyFrame06(String title){
        //定义面板容器
        super(title);
        root = new JPanel();
        setContentPane(root);
        setLayout(null);

        //TextArea
        textArea = new JTextArea();
        //设置文本域自动换行
        textArea.setLineWrap(true);
        textArea.setBounds(20,10,290,200);
        root.add(textArea);

        //ComboBox
        msgLabel = new JLabel("请选择部门: ");
        msgLabel.setBounds(31,220,130,15);
        root.add(msgLabel);
        String[]  options={
                "生产部","销售部","财务部","采购部","市场部","宣传部"
        };
        comboBox = new JComboBox(options);
        comboBox.setBounds(130,220,100,21);
        root.add(comboBox);

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setBounds(400,300,400,400);
        setVisible(true);

    }

    public static void main(String[] args) {
        new MyFrame06("JTextArea & JComboBox");
    }

}

 

 

标签:控件,setBounds,JFrame,--,add,new,JLabel,root,public
From: https://www.cnblogs.com/hmy22466/p/17842611.html

相关文章

  • 向量夹角、法向量
    夹角1)夹角一般用180度内的角   2)如果夹角用于计算sin正弦值(叉乘公式有sin计算),那还要区分向量的先后顺序,即:夹角是顺时针还是逆时针。因为像sin(60)和sin(-60)结果是不一样的。a到b的夹角:逆时针180度内b到a的夹角:顺时针180度内 法向量1)左法向量,逆时针90度向量......
  • SD-Host控制器设计架构
    SDHost功能列表系统挂接在SoC中的有控制寄存器和状态寄存器内建DMA-数据需要从外部将数据读取到buffer中,然后再对于SD卡进行写操作;对于读操作,首先先将SD卡读取到控制器中buffer中,再从buffer中将数据存储到SoC中的存储单元中,数据搬移需要使用DMADMA也是挂接在AHB总线上......
  • 三、Linux基本使用和常用命令
    Linux基本使用和常用命令1、登录Linux操作系统方式1.图形化界面基于xwindowSystem显示框架开发由KDE(类似于苹果系统)、GNOME.v.3.0提供图形化桌面环境2.虚拟控制台文本方式(Ctrl+Alt+F3) 3.Web网页登录前提是需要打开网页控制台,由cock.pit程序提供‘’‘......
  • 每日随笔——享元模式
    [实验任务一]:围棋设计一个围棋软件,在系统中只存在一个白棋对象和一个黑棋对象,但是它们可以在棋盘的不同位置显示多次。实验要求:1.提交类图;2.提交源代码;3.注意编程规范;4.要求用简单工厂模式和单例模式实现享元工厂类的设计。  源码:AddresspackageSC13;classaddre......
  • 蛤蟆先生去看心理医生-阅读笔记 All In One
    蛤蟆先生去看心理医生-阅读笔记AllInOne心理学作者:[英]罗伯特·戴博德出版社:天津人民出版社出品方:果麦文化原作名:CounsellingForToads:APsychologicalAdventure译者:陈赢出版年:2020-8-1页数:208定价:38.00元装帧:平装ISBN:9787201161693de......
  • Power BI - 5分钟学习连接数据源
    最近应业务部门请求,给同事做一次入门培训;在整理资料之余,把一些内容以每天5分钟的形式分享给大家那么首先我们解释WhatisPowerBI?简单来说就是可以从各种数据源中提取数据,并对数据进行整理分析,然后生成精美的图表,并且可以在电脑端和移动端与他人共享的一个神器。第1天-Power......
  • 聪明办法学Python-2023-task01
    task00因为完全按照视频教学傻瓜式操作即可完全学会,这里不做赘述视频链接:【安装】手把手带你配置AI环境_哔哩哔哩_bilibilitask01参考视频链接[Chap1启航]聪明办法学Python第二版_哔哩哔哩_bilibili注释Comment分类:单行注释,使用#开头多行注释,使用'''或"""......
  • python_datetime日期时间
    #!/usr/bin/python3#-*-coding:UTF-8-*-importdatetimeimporttime#时间戳ticks=time.time()print(ticks)#结构体时间{tm_year...}localtime=time.localtime(ticks)print(localtime)#格式化时间strftime=time.asctime(localtime)print(strftime)#获取当前日......
  • 关于 Mac 在 cocos creator 中如何使用 FGUI
    先在官方网下载对应的Mac的FGUI,网站如下:https://www.fairygui.com/download之后就一键式的打开就好,在这个界面上 选择自己对应的项目类型,之后创建即可,打开的面板如下: 最后在选择发布就好了,这里发布有两个文件 如果你想重新在打开这个布局的话,就可以在这个后缀名......
  • 聪明班法学python task1
    Python课程简介Python是一种非常流行的编程语言,是人工智能的主流语言。特点:代码少,比c简单安装Installation安装清单(默认配置即可):Miniconda1.需要激活环境2.更换镜像源【可加快国内资源下载速度】​ Pip换源​ Conda换源VisualStudioCodeGit启航GETTINGSTARTED第......