java基础学习笔记(7)

Swing程序设计

 

概述

AWT:重量级组件,依赖于本地的窗口系统来决定组件的功能;

Swing:轻量级组件,不依赖于任何系统,完全由java编写;

 

Swing

Jvaa.lang.Object>java.awt.Component>java.awt.Container>javax.swing.JComponent

 

 

常用窗体

JFrame窗体

需继承JFrame类;

 

//创建窗体

JFrame jf=new JFrame(title);

Container container=jf.getContentPane();

 

//添加组件

JLabel jl=new JLabel("这是一个窗体");

jl.setHorizontalAlignment(SwingConstants.CENTER);

container.add(jl);

 

//设置窗体大小

jf.setSize(width,height);

//设置窗体可见

jf.setVisible(true);

//设置窗体的关闭事件

jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

DO_NOTHING_ON_CLOSE 什么都不做将窗体关闭,程序还在运行;

DISPOSE_ON_CLOSE 

HIDE_ON_CLOSE 隐藏窗体

 

 

 

JDialog窗体

需继承 JDialog类;

 

---------------------------------------------------------------------------

class MyJDialog extends JDialog { // 创建新类继承JDialog类

 

private static final long serialVersionUID = 1L;

 

public MyJDialog(TS frame) {

// 实例化一个JDialog类对象,指定对话框的父窗体、窗体标题和类型

super(frame, "第一个JDialog窗体"true);

Container container = getContentPane(); // 创建一个容器

container.add(new JLabel("这是一个对话框")); // 在容器中添加标签

setBounds(120, 120, 100, 100); // 设置对话框窗体大小

}

}

 

---------------------------------------------------------------------------JButton bl = new JButton("弹出对话框"); // 定义一个按钮

bl.setBounds(10, 10, 100, 20);

bl.addActionListener(new ActionListener() { // 为按钮添加鼠标单击事件

public void actionPerformed(ActionEvent e) {

// 使MyJDialog窗体可见

new MyJDialog(TS.this).setVisible(true);

}

});

标签组件与图标

标签的使用

JLabel jl=new JLabel("这是一个窗体");//定义标签

jl.setHorizontalAlignment(SwingConstants.CENTER);//设置文字水平居中对齐;

 

图标的使用

 

需继承Icon接口

 

1、创建图标

并实现:

getIconHeight()

getIconWidth()

paintIcon(Component arg0, Graphics arg1, int x, int y) 

3个接口方法

 

class DrawIcon extends JFrame implements Icon { // 实现Icon接口

private int width; // 声明图标的宽

private int height; // 声明图标的长

public int getIconHeight() { // 实现getIconHeight()方法

return this.height;

}

public int getIconWidth() { // 实现getIconWidth()方法

return this.width;

}

public DrawIcon(int width, int height) { // 定义构造方法

this.width = width;

this.height = height;

}

// 实现paintIcon()方法

public void paintIcon(Component arg0, Graphics arg1, int x, int y) {

arg1.fillOval(x, y, width, height); // 绘制一个圆形

}

}

 

2、使用图片图标

public void MyImageIcon() {

Container container = getContentPane();

// 创建一个标签

JLabel jl = new JLabel("这是一个JFrame窗体", JLabel.CENTER);

// 获取图片所在的URL

URL url = TS.class.getResource("imageButton.jpg");

Icon icon = new ImageIcon(url); // 实例化Icon对象

jl.setIcon(icon); // 为标签设置图片

// 设置文字放置在标签中间

jl.setHorizontalAlignment(SwingConstants.CENTER);

jl.setOpaque(true); // 设置标签为不透明状态

container.add(jl); // 将标签添加到容器中

setSize(250, 100); // 设置窗体大小

setVisible(true); // 使窗体可见

// 设置窗体关闭模式

setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

}

 

常用布局管理器

 

布局管理器包括:绝对布局、流布局、边界不觉、网格布局

 

绝对布局

步骤1setLayOut(null);//清除布局

步骤2setBounds(int 上,int 左,int 宽,int )

 

流布局

流布局构造方法:

Public FlowLayout(int alignent,int horizGap,int vertGap)

 

Alignment:3个参数012

0表示左对齐

1表示居中对齐

2表示右对齐

 

Horiagapvertgap表示水平和垂直间距;

 

setLayout(new FlowLayout(2, 10, 10));

for (int i = 0; i < 10; i++) { // 在容器中循环添加10个按钮

c.add(new JButton("button" + i));

}

 

边界布局

BorderLayout()

 

// 定义组件摆放位置的数组

String[] border = { BorderLayout.CENTER, BorderLayout.NORTH,

BorderLayout.SOUTH, BorderLayout.WEST, BorderLayout.EAST };

String[] buttonName = { "center button", "north button",

"south button", "west button", "east button" };

 

 

setLayout(new BorderLayout()); // 设置容器为边界布局管理器

for (int i = 0; i < border.length; i++) {

// 在容器中添加按钮,并设置按钮布局

c.add(border[i], new JButton(buttonName[i]));

}

 

 

网格布局

GridLayout();

 

setLayout(new GridLayout(7, 3, 5, 5));//73列  水平垂直间距5

for (int i = 0; i < 20; i++) {

c.add(new JButton("button" + i)); // 循环添加按钮

}

 

 

常用面板

Jpanel面板

 

JPanel p1 = new JPanel(new GridLayout(1, 3, 10, 10));

JPanel p2 = new JPanel(new GridLayout(1, 2, 10, 10));

JPanel p3 = new JPanel(new GridLayout(1, 2, 10, 10));

JPanel p4 = new JPanel(new GridLayout(2, 1, 10, 10));

p1.add(new JButton("1")); // 在面板中添加按钮

p1.add(new JButton("2"));

p1.add(new JButton("3"));

p2.add(new JButton("4"));

p2.add(new JButton("5"));

p3.add(new JButton("6"));

p3.add(new JButton("7"));

p4.add(new JButton("8"));

p4.add(new JButton("9"));

 

JScrollpanel面板

JTextArea ta = new JTextArea(20, 50); // 创建文本区域组件

JScrollPane sp = new JScrollPane(ta); // 创建JScrollPane面板对象

c.add(sp); // 将该面板添加到该容器中

 

 

按钮组件

提交按钮组件

 

  button.addActionListener(new ActionListener(){

    

     public void actionPerformed(ActionEvent e){

     JOptionPane.showConfirmDialog(null"弹出对话框");

     }

    

    });

 

单选按钮

    JRadioButton jr1=new JRadioButton("9");

JRadioButton jr2=new JRadioButton("10");

JRadioButton jr3=new JRadioButton("11");

JRadioButton jr4=new JRadioButton("12");

ButtonGroup bg=new ButtonGroup();

 

jr1.addActionListener(new ActionListener(){

    

     public void actionPerformed(ActionEvent e){

          }

    

    });

 

复选框组件

 

        JCheckBox jc1=new JCheckBox("13");

JCheckBox jc2=new JCheckBox("14");

JCheckBox jc3=new JCheckBox("15");

JCheckBox jc4=new JCheckBox("16");

 

jc1.addActionListener(new ActionListener(){

    

     public void actionPerformed(ActionEvent e){

          }

    

    });

 

列表组件

下拉框组件

一般封装为下拉框模型

public class Combo extends AbstractListModel<String> implements ComboBoxModel<String> {

private static final long serialVersionUID = 1L;

String selecteditem = null;

String[] test = { "身份证""军人证""学生证""工作证" };

public String getElementAt(int index) {

return test[index];

}

public int getSize() {

return test.length;

}

public void setSelectedItem(Object item) {

selecteditem = (String) item;

}

public Object getSelectedItem() {

return selecteditem;

}

public int getIndex() {

for (int i = 0; i < test.length; i++) {

if (test[i].equals(getSelectedItem()))

return i;

}

return 0;

}

 

}

 

列表框组件

class MyListModel extends AbstractListModel<String> {

private static final long serialVersionUID = 1L;

private String[] contents = { "列表1""列表2""列表3""列表4""列表5""列表6" };

public String getElementAt(int x) {

if (x < contents.length)

return contents[x++];

else

return null;

}

public int getSize() {

return contents.length;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值