banner
NEWS LETTER

GUI图形化(已弃用)

Scroll down

JavaGUI图形化界面笔记

常用窗体

Jframe

新建Jframe

1
2
3
4
5
6
7
       JFrame jf=new JFrame("我的第一个界面");
/*设置窗体大小和出现的位置*/
jf.setBounds(500,500,500,700);
/*设置窗体是否显示*/
jf.setVisible(true);
/*设置窗体是否可调节(默认可调)*/
jf.setResizable(false);

设定窗口关闭方式

1
2
3
4
5
6
7
8
/*隐藏当前窗口,并释放窗体占用的所有资源*/
jf.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
/*隐藏当前窗口*/
jf.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
/*结束窗口所在的应用程序*/
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
/*点击后什么都不做*/
jf.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);

设置背景颜色

1
jf.getContentPane().setBackground(Color.decode("#dddddd"));

Jdialog(弹窗)

1
2
3
JDialog jd=new JDialog(jf ,"弹窗");
jd.setBounds(500,500,300,400);
jd.setVisible(true);

常用的面板

面板也是一种swing容器,他可以作为容器容纳其他组件,但是他也必须放在一个容器内

Jpanel

Jpanel是最基础的面板,继承自java.awt.Container类

1
2
3
4
5
6
JPanel jp=new JPanel(new FlowLayout());
JButton jb=new JButton("登录");
JButton jb2=new JButton("注册");
jp.add(jb);
jp.add(jb2);
jf.add(jp);

JscrollPane(滚动面板)

定义实例:JScrollPane jsp=new JScrollPane(某个继承自JPanel类的实例 pane);
设置初始大小:jsp.setPreferredSize(new Dimension(200,200));

设置滚动条显示和隐藏

1
JScrollPane waiceng1=new JScrollPane(waiceng,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

常用组件

标签组件

类:JLabel

作用:显示文本或者提示信息

构造方法:

new JLabel();

new JLabel(Icon icon); 设置图标

new Jlabel(Icon icon,SwingConstants.CENTER) 设置图标+水平对齐方式

new Jlabel(String str,SwingConstants.CENTER) 设置文字+水平对齐方式

new Jlabel(String str,Icon icon,SwingConstants.CENTER)

设置文字图标+水平对齐方式

1
2
3
jf.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
JLabel jl=new JLabel("我是提示文字",SwingConstants.);
jf.add(jl);

按钮组件

类:JButton

构造方法:

new JButton();

new JButton(Icon icon); 指定图标

new JButton(String txt); 指定文字

new JButton(Icon icon,String txt); 指定文字和图标

其他方法:

setEnabled(boolean n) 按钮是否可用

setBorderPainted(boolean n) 按钮是否带边框

setToolTipText(String st) 鼠标上移提示文字

单选多选按钮

单选

new JRadioButton();

new JRadioButton(Icon icon); 设置图标

new JRadioButton(Icon icon,boolean b) 设置图标+是否选中

new JRadioButton(String str,boolean b) 设置文字+是否选中

new JRadioButton(String str,Icon icon,boolean b)

设置文字图标+是否选中

1
2
3
4
5
6
7
JRadioButton jr=new JRadioButton("男");
JRadioButton jr2=new JRadioButton("女");
ButtonGroup bg=new ButtonGroup();
bg.add(jr);
bg.add(jr2);
jf.add(jr);
jf.add(jr2);

复选

new JCheckBox();

new JCheckBox (Icon icon,boolean b); 指定图标+是否被选中

new JCheckBox (String txt,boolean b); 指定文字+是否被选中

下拉列表

构造方法

new JComboBox();

方法:

addItem:添加下拉内容

1
2
3
4
5
JComboBox jcb=new JComboBox();
jcb.addItem("请选择省份");
jcb.addItem("河北");
jcb.addItem("河南");
jf.add(jcb);

菜单栏

一级菜单:

创建菜单条 JMenuBar

创建菜单JMenu

创建菜单项JMenuItem

总结:菜单项依附菜单、菜单依附菜单条

创建菜单栏

创建菜单

创建菜单项

菜单添加一个菜单项

菜单项添加菜单

1
2
3
4
5
6
7
8
9
10
JMenuBar bar=new JMenuBar();
JMenu menu=new JMenu("菜单1");
JMenuItem item1=new JMenuItem("二级菜单1");
JMenuItem item2=new JMenuItem("二级菜单2");
JMenuItem item3=new JMenuItem("二级菜单3");
menu.add(item1);
menu.add(item2);
menu.add(item3);
bar.add(menu);
jf.add(bar);

文本

类:JTextField

构造函数

new JTextField();

new JTextField(String text); 设置默认文字

new JTextField(int fiel) 设置文本框长度

new JTextField(String str,int b) 默认文字+文本框长度

密码框

类:JPasswordField

构造函数

new JPasswordField();

new JPasswordField(String text); 设置默认文字

new JPasswordField(int fiel) 设置密码框长度

new JPasswordField(String str,int b) 默认文字+密码框长度

文本域

类:JTextArea

构造函数

new JTextArea();

new JTextArea(String text); 设置默认文字

new JTextArea(int fiel,int fiel) 设置文本框长度和宽度

方法:

setLineWrap(); 设置文本框是否换行

常用布局

流式布局

构造方法:

FlowLayout(int aligment); 设置对齐方式(默认居中)

FlowLayout(int aligment,int horizGap,int vertGap); 设置对齐方式+上下偏移

通过setLayout设置布局方式

例如:jf.setLayout(new FlowLayout(FlowLayout.LEFT))

网格式布局

构造方法:

GridLayout(int row,int col); 指定行数和列数

GridLayout(int row,int col,int horizGap,int vertGap); 设置行数列数+上下偏移

Other Articles
cover
Maven操作指南
  • 23/02/28
  • 18:36
  • 2.2k
  • 9
cover
Vue框架
  • 23/02/27
  • 18:46
  • 9k
  • 46