Java Swing程序设计学习总结

181 篇文章 3 订阅

Swing程序设计

重点类:

  1. Component:组件类
  2. Container:容器类
  3. JComponent:组件父类

常用组件:
在这里插入图片描述
例子:

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

public class winF {
    public static void JF_use_H(JFrame jf){
        //设置窗体的位置和大小
        jf.setBounds(150,150,500,500);
        //获取主容器
        Container container = jf.getContentPane();
        //设置文本标签
        JLabel jl = new JLabel("Enter System");
        jl.setHorizontalAlignment(SwingConstants.CENTER);
        container.add(jl);
        //设置窗口关闭方式
        //DO_NOTHING_ON_CLOSE:关闭窗体不触发任何操作即关不上
        // HIDE_ON_CLOSE:关闭窗体时仅隐藏不释放资源
        //EXIT_ON_CLOSE:彻底关闭
        // DISPOSE_ON_CLOSE:关闭窗体时,释放资源,窗体消失但程序不关闭
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //设置窗口可视化
        jf.setVisible(true);
    }

    public static void main(String[] args) {
        //设置一个叫Enter System的不可见窗体
        JFrame jf = new JFrame("Enter System");
        winF.JF_use_H(jf);
        JDialog jd = new JDialog(jf,"jdialog");
        jd.setBounds(200,200,400,200);
        jd.setVisible(true);
        //JDialog的退出没有EXIT_ON_CLOSE
        jd.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }
}

程序展示:
在这里插入图片描述

代码优化:

import javax.swing.*;
import java.util.Scanner;

public class JF_self {
    public void JF_S_Form(JFrame jf){
        Scanner sc = new Scanner(System.in);
        System.out.print("please enter the Form width:");
        int Formwidth = sc.nextInt();
        System.out.print("please enter the Form height:");
        int Formheight = sc.nextInt();
        System.out.print("plaese enter the name of the Form\ncalled:");
        String Formname = sc.next();
        jf.setBounds(100,100,Formwidth,Formheight);
        jf.setTitle(Formname);
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jf.setVisible(true);
    }

    public static void main(String[] args) {
        JFrame jf = new JFrame();
        JF_self jsf = new JF_self();
        jsf.JF_S_Form(jf);
    }
}

程序展示:
在这里插入图片描述

JDialog对话框

功能:
从一个窗体中弹出另一个窗体
语法:

JFrame jf = new JFrame("title");
JDialog jd = new JDialog(jf,"jdialog");

JLabel标签

功能:
可以显示文本图标等
构造方法:

  1. public JLabel(String text,Icon icon,int aligment):创建一个带有文本,图标的标签,并且设置它的对齐方式
  2. public JLabel(String text,int aligment)
  3. public JLabel(,Icon icon,int aligment)
  4. public JLabel()

综合编写:

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

public class Jlabel {
    public static void main(String[] args) {
        JFrame jf = new JFrame("Enter");
        //设置为流布局
        jf.setLayout(new FlowLayout(FlowLayout.CENTER));
        jf.setBounds(200,200,400,400);
        //设置标签组件
        JLabel jl = new JLabel("账号",SwingConstants.CENTER);
        jf.add(jl);
        //设置按钮模块
        JButton jb1 = new JButton("登录");
        JButton jb2 = new JButton("注册");
        jf.add(jb1);
        jf.add(jb2);
        //设置按钮为可用
        jb1.setEnabled(true);
        jb2.setEnabled(true);
        //设置按钮边界显示
        jb1.setBorderPainted(true);
        jb2.setBorderPainted(true);
        //设置单选按钮
        JRadioButton jrb1 = new JRadioButton("男");
        JRadioButton jrb2 = new JRadioButton("女");
        //将单选按钮加入按键组中
        ButtonGroup group = new ButtonGroup();
        group.add(jrb1);
        group.add(jrb2);
        jf.add(jrb1);
        jf.add(jrb2);
        //设置复选框
        JCheckBox jcb1 = new JCheckBox("choose1",true);
        JCheckBox jcb2 = new JCheckBox("choose2",false);
        JCheckBox jcb3 = new JCheckBox("choose3",false);
        JCheckBox jcb4 = new JCheckBox("choose4",false);
        jf.add(jcb1);
        jf.add(jcb2);
        jf.add(jcb3);
        jf.add(jcb4);
        //设置下拉框
        JComboBox jcombobox = new JComboBox();
        jcombobox.addItem("---请选择学历---");
        jcombobox.addItem("本科");
        jcombobox.addItem("高中");
        jcombobox.addItem("初中");
        jf.add(jcombobox);
        //设置多级菜单栏
        JMenuBar jmb = new JMenuBar();
        //设置菜单
        JMenu menu = new JMenu("菜单1");
        //设置菜单选项
        JMenuItem jmi1 = new JMenuItem("选项1");
        JMenuItem jmi2 = new JMenuItem("选项2");
        menu.add(jmi1);
        menu.add(jmi2);
        jmb.add(menu);
        jf.add(jmb);
        //设置文本组件
        JTextField jtf = new JTextField("请输入",22);
        jf.add(jtf);
        //文本组件和标签综合使用
        //jf.setLayout(new FlowLayout(FlowLayout.LEFT));
        JLabel jl2= new JLabel("账号:");
        jf.add(jl2);
        JTextField jtf2 = new JTextField("请输入账号",22);
        jf.add(jtf2);
        //密码框组件
        JPasswordField pwd = new JPasswordField("",22);
        //设置密码为星号
        pwd.setEchoChar('*');
        jf.add(pwd);
        //文本域组件
        JTextArea textArea = new JTextArea(10,10);
        //设置文本自动换行
        textArea.setLineWrap(true);
        jf.add(textArea);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

程序展示:
在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值