面向对象第10章

第十章

swing介绍

Swing 是一个为Java设计的GUI工具包。Swing是JAVA基础类的一部分。Swing包括了图形用户界面(GUI)器件如:文本框,按钮,分隔窗格和表。

Swing提供许多比AWT更好的屏幕显示元素。它们用纯Java写成,所以同Java本身一样可以跨平台运行,这一点不像AWT。它们是JFC的一部分。它们支持可更换的面板和主题(各种操作系统默认的特有主题),然而不是真的使用原生平台提供的设备,而是仅仅在表面上模仿它们。这意味着你可以在任意平台上使用JAVA支持的任意面板。轻量级组件的缺点则是执行速度较慢,优点就是可以在所有平台上采用统一的行为。

二.布局管理器

1.FlowLayout流式布局:
(1)FlowLayou流式布局管理器默认为居中放置。
(2)设置右对齐

setLayout(new FlowLayout(FlowLayout.RIGHT,20,40));

(3)设置左对齐

setLayout(new FlowLayout(FlowLayout.LEFT,20,40));

2.GridLayout网格布局:

setLayout(new GridLayout(,));

1
3.边框布局:

JButton 对象名=new JButton("东");
BroderLayout 对象名1=new BoderLayout();
this.setLayout(对象名1);
this.add(对象名,BroderLayout.EAST);

三.常用控件:

1.组件
JLabel:标签组件
JTextField:文本框组件
JPasswordField:密码框组件
JButton:按钮组件
JPanel:面板组件
JComboBox:下拉选择框组件
JRadioButton:单选按钮组件
JCheckBox:复选框组件

练习

1 package com.chapter11;

2 import java.awt.*;

3 import javax.swing.*;

4 public class TestJFrame extends JFrame{
5	public TestJFrame(){
6		super("一个新的窗口");
7		this.setBounds(300,200,500,600);
8		ImageIcon icon = new ImageIcon("D:\\捕获.png");
9		this.setIconImage(icon.getImage());
10		this.setResizable(false);
11		this.setVisible(true);
12		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
13	}
14 }
1 package com.chapter11;
2 public class Test {
3 	public static void main(String[] args) {
4 		TestJFrame ts = new TestJFrame();
5 	}
6 }
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);
    }
 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值