现在的位置: 首页 > 综合 > 正文

《Java7程序设计》第15章 Swing基础知识

2018年04月10日 ⁄ 综合 ⁄ 共 2874字 ⁄ 字号 评论关闭



1.
抽象窗口工具包AWT 2.SwingJFC Java基本类的一部分)

UI组件:顶层容器JFrameJDialog,以及可以添加到容器中的组件。

布局管理器:如何在容器中布置组件。

事件处理。

 

Swing消除了AWT对原生对等函数的依赖。AWT是调用操作系统的原生GUI函数来完成。

AWT组件:java.awt Component

import java.awt.Button;

import java.awt.Checkbox;

import java.awt.FlowLayout;

import java.awt.Frame;

import java.awt.Label;

import java.awt.TextField;

 

public class AWTFrameTest extends Frame{

public static void main(String[] args){

AWTFrameTest frame = new AWTFrameTest();

frame.setTitle("李苏璇");

frame.setSize(300,100);

 

frame.setLayout(new FlowLayout());

 

Label label = new Label("姓名");

frame.add(label);

TextField textField = new TextField();

frame.add(textField);

Button button = new Button("注册");

frame.add(button);

Checkbox checkbox = new Checkbox();

frame.add(checkbox);

 

frame.setVisible(true);

 

}

}

 

除了AWT类外,Swing还包括一些类:

java.awt.Color

Color color = Color.GREEN;

Color myColor = new Color(246,27,27);

component.setForeGround(Color.TELLOW)

java.awt.Font

public Font(java.lang.String name ,int style ,int size)

int style = Font.BOLD | Font.ITALIC;

Font font = new Font("Garamond",style,11);

java.awt.Point

java.awt.Dimension

java.awt.Rectangle

java,awt.Graphics

java.awt.Toolkit

 

javax.swing JFrame JDialog JApplet 都扩展了java.awt.Container

JFrame 框架容器 顶层容器可以出现在屏幕上不必放在另一个容器中。

新的调用JFrame的规范:

frame.pack();调整到子组件适合的尺寸和布局 frame.setVisible(true);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

public static void main(String[] args){

    SwingUtilities.invokeLater(new Runnable(){

        public void run(){

            constructGUI();

        }

    });

}

 

public JTextField()

public void setText(java.lang.String text)

public java.lang.String getText()

public void JPasswordField()

public char[] getPassword()

 

JOptionPane显示提示,或者获取输入

JDialog.setDefaultLookAndFeelDecorated(true);

JOptionPane.showMessage(null,"This is the main","Title",JOptionPane.INFORMATION_MESSAGE)

 

十六章:Swing高级知识【布局管理和事件处理】

java.awt.LayoutManager

jPanel panel = new JPanel(layoutManager);

jFrame.getContentPane().setLayoutManager(layoutManager);

jFrame.setLayoutManager(layoutManager);

button.setPreferredSize(new Demension(300,300));

 

BorderLayout 分为五个区域:东西南北中间

FlowLayout 排列成一条横线

BoxLayout 从上到下

GridLayout 在单元格中排列组件 frame.setLayout(new GridLayout(3,2));

 

事件处理:Java事件模型 关键:编写事件监听器 java.awt.event

不用实现java.awt.event.MouseListener接口,而是扩展MouseAdapter.

 

AWT事件APIjava.awt.event.ActionEvent JButton被按压 JCheckbox被选中

java.awt.event.ActionListener接口

java.awt.event.MouseEvent

java.awt.event.MouseListener

java.awt.event.KeyEvent KeyEvent.VK_A

java.awt.event.KeyListener接口

动作监听器:执行事件发生后处理的代码 调用的代码是:button.addActionListener(new MyActionListener());

class MyActionListener implements ActionListener {

    public void actionPerformed(ActionEvent e) {

        JButton source = (JButton) e.getSource();

        String buttonText = source.getText();

        JOptionPane.showMessageDialog(null,

                "You clicked " + buttonText);

    }

}

 

额外:

JButton comfirmButton = new JButton( "确定" );

comfirmButton.addActionListener(new RegisterButtonHandler());

add(comfirmButton);

 

//处理确定按钮

private class RegisterButtonHandler implements ActionListener

{

public void actionPerformed( ActionEvent e )

 

 

抱歉!评论已关闭.