2021-06-22

Posted 二十的十七

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2021-06-22相关的知识,希望对你有一定的参考价值。

第10章 Swing编程初级应用

10.1 Swing中如何创建第一个窗体

1.注意事项
这里虽然关闭了窗体,不代表结束了整个程序,所以还要从控制台去结束,需要加入设置代码,一般会在软件的主界面加入。
2.窗体子类的创建
不同的窗体具有不同的功能和特性,所以一般使用时会创建窗体类继承JFrame类,从而扩展它。
3.注意事项
代码中的文件路经,注意反斜杠\\需要加入2个,因为一个具有特殊含义,需要转义一下。
4.知识要点
SWING中JFRAME的使用需要注意:
(1)JFrame类构造方法创建的窗体是不可见的,需要开发人员通过硬编码的方式,设置窗体的可见性。
(2)JFrame类构造方法创建的窗体默认的大小为0×0像素,也就是没有大小,默认的坐标是(0,0)就是屏幕的左上角。

应用1:

package com.frame;



import javax.swing.ImageIcon;
import javax.swing.JFrame;

public class MianFrame extends JFrame
public MianFrame() 
	this.setTitle("主窗体");
	this.setSize(500, 400);
	this.setLocation(200, 300);
	this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	ImageIcon icon=new ImageIcon("C:\\\\Users\\\\123\\\\Pictures\\\\Camera Roll\\\\2018022816064949211.JPG");
	this.setIconImage(icon.getImage());
	this.setResizable(false);
	


package com.frame;

public class Test1 

	public static void main(String[] args) 
		// TODO Auto-generated method stub
        MianFrame m1=new MianFrame();
        m1.setVisible(true);
	



10.2 布局管理器的使用

1.知识要点
SWING布局管理器的使用:
常用的布局管理器:FlowLayout流式布局管理器、GridLayout网格布局管理器、BorderLayout边框布局管理器。
2.注意事项
不同的布局方式,界面中的控件会按照不同的方式摆放。

应用2:

package com.frame;

import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;

public class GridDemoFrame extends JFrame
public GridDemoFrame() 
	this.setTitle("网格布局");
	this.setBounds(200, 300, 500, 400);
	this.setResizable(false);
	this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//	this.setLayout(new GridLayout(3, 3));
	GridLayout grid=new GridLayout(3, 3);
	grid.setHgap(5);
	grid.setVgap(10);
	this.setLayout(grid);
//	this.setLayout(new GridLayout(5, 6, 5, 10));
	
	JButton btn1=new JButton("1");
	JButton btn2=new JButton("2");
	JButton btn3=new JButton("3");
	JButton btn4=new JButton("4");
	JButton btn5=new JButton("5");
	JButton btn6=new JButton("6");
	JButton btn7=new JButton("7");
	JButton btn8=new JButton("8");
	JButton btn9=new JButton("9");
	this.add(btn1);
	this.add(btn2);
	this.add(btn3);
	this.add(btn4);
	this.add(btn5);
	this.add(btn6);
	this.add(btn7);
	this.add(btn8);
	this.add(btn9);




10.3 swing常用组件的使用

1.知识要点
SWING常用组件主要包含:
(1)文本——JLabel
(2)按钮——JButton
(3)文本框——JTextField
(4)密码框——JPasswordField
(5)面板——JPanel
(6)下拉框——JComboBox
(7)单选按钮——JRadioButton
(8)复选框——JCheckBox
2.注意事项
把布局方式设置为null,所有添加到界面上的控件必须设置坐标,否则显示不正常。
3.注意事项
Panel代表一个容器,它里面的按钮如果要水平排列,则需要设置Panel的布局方式。

应用3:


```java
package com.frame;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class RegisterFrame extends JFrame
	JPanel panel1,panel2;
	JLabel label1,label2,label3;
	JTextField jf1,jf2,jf3;
	JButton btn1,btn2;
public RegisterFrame() 
	this.setTitle("注册");
	this.setBounds(200, 200, 400, 200);
	this.setResizable(false);
	this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	this.setLayout(new BorderLayout());
	panel1=new JPanel();
	panel2=new JPanel();
	this.add(panel1,BorderLayout.CENTER);
	panel1.setLayout(new GridLayout(3, 2, 20, 30));
	label1=new JLabel("姓名");
	label1.setHorizontalAlignment(JLabel.CENTER);
	jf1=new JTextField(15);
	label2=new JLabel("性别");
	label2.setHorizontalAlignment(JLabel.CENTER);
	jf2=new JTextField(15);
	label3=new JLabel("年龄");
	label3.setHorizontalAlignment(JLabel.CENTER);
	jf3=new JTextField(15);
	panel1.add(label1);
	panel1.add(jf1);
	panel1.add(label2);
	panel1.add(jf2);
	panel1.add(label3);
	panel1.add(jf3);
	this.add(panel2,BorderLayout.SOUTH);
	panel2.setLayout(new FlowLayout(FlowLayout.CENTER));
	btn1=new JButton("确定");
	btn2=new JButton("取消");
	panel2.add(btn1);
	panel2.add(btn2);
	
	



package com.frame;

public class Test3 

	public static void main(String[] args) 
		// TODO Auto-generated method stub
       RegisterFrame r1=new RegisterFrame();
       r1.setVisible(true);
	




10.4 下拉列表,单复选按钮的使用
注意事项
将数据放入combox下拉框中,combox放入Panel中,最后将Panel放入界面中。

应用4:

package com.frame;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class MyFrame1 extends JFrame
	JLabel label1,label2;
	JTextField jf1;
	JPasswordField jf2;
	JButton btnLogin,btnClose;
public MyFrame1() 
	this.setTitle("控件的使用");
	this.setBounds(200, 200, 500, 400);
	this.setResizable(false);
	this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	this.setLayout(null);
	label1=new JLabel("用户名");
	label1.setBounds(50, 50, 80, 25);
	this.add(label1);
	jf1=new JTextField(20);
	jf1.setBounds(100, 50, 200, 25);
	this.add(jf1);
	label2=new JLabel("密码");
	label2.setBounds(50, 80, 80, 25);
	this.add(label2);
	jf2=new JPasswordField(20);
	jf2.setBounds(100, 80, 200, 25);
	this.add(jf2);
	btnLogin=new JButton("登录");
	btnLogin.setBounds(110, 120, 60, 20);
	this.add(btnLogin);
	btnClose=new JButton("关闭");
	btnClose.setBounds(190, 120, 60, 20);
	this.add(btnClose);
	


package com.frame;

public class Test4 

	public static void main(String[] args) 
		// TODO Auto-generated method stub
        MyFrame1 m1=new MyFrame1();
        m1.setVisible(true);
	




以上是关于2021-06-22的主要内容,如果未能解决你的问题,请参考以下文章

2021-06-22

JavaSE常用类日期时间02 2021.06.22-23

2021-06-22 总结

2021-06-22

2021-06-22

2021-06-22