JAVA-基础(十) Swing

 在看到applet和Swing的时候,我想起了winform,以及java beans包中各种所谓的组件的时候,一切都那么似曾相识。

Swing是AWT的扩展,它提供了更强大和更灵活的组件集合。 除了我们已经熟悉的组件如按钮、复选框和标签外,Swing还包括许多新的组件,如选项板、 滚动窗口、树、表格。许多一些开发人员已经熟悉的组件,如按钮,在Swing都增加了新功 能。而且,按钮的状态改变时按钮的图标也可以随之改变。 与AWT组件不同,Swing组件实现不包括任何与平台相关的代码。Swing组件是纯Java 代码,因此与平台无关。一般用轻量级(lightweight)这个术语描述这类组件。 在Swing包中的类和接口的数量众多,本章只对其中的一部分简要描述。Swing包是开 发人员希望自己仔细研究的部分。

在使用swing时要引入的包:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

 

一些常用的控件以及对应的事件:

1)内容窗格:

     得到内容空格   Container getContentPane( )      Container contentPane = getContentPane();

     容器的add( )方法在内容窗格中增加一个组件   add(comp)  contentPane.add(jl);

2)JTextComponent类,JTextComponent类是JComponent的子类

3)JButtom类,AbstractButton类扩展JComponent类。 AbstractButton类包含多种方法,用于控制按钮行为,检查复选框和单选按钮

     可以通过下列方法读写与按钮相关的文字: String getText( ) void setText(String s)

    AbstractButton抽象类的子类在按钮被按下时生成行为事件。

    通过如下的方法注册和注 销这些事件的监听器: void addActionListener(ActionListener al) void removeActionListener(ActionListener al)

4)JCheckBox类提供复选框的功能

    当选中或取消复选框时,生成一个项目事件。这个事件由itemStateChanged( )处理。在 itemStateChanged( )内部,getItem( )方法获取产生事件的JCheckBox对象。getText( )方法获 取复选框的文字,并用这个文字设置文本域。

    cb.addItemListener(this); 增加事件

    处理理件:

    public void itemStateChanged(ItemEvent ie)

    { JCheckBox cb = (JCheckBox)ie.getItem(); jtf.setText(cb.getText()); }

5)单选按钮由JRadioButton类支持,JRadioButton也是AbstractButton抽象类的子类。

     单选按钮按下产生的行为事件,由actionPerformed( )处理。getActionCommand( ) 方法 获取与单选按钮相关的文字,并用此文字设置文本域。

      b2.addActionListener(this);  增加事件

     public void actionPerformed(ActionEvent ae) { tf.setText(ae.getActionCommand()); }  处理事件

6)Swing通过JComboBox类支持组合框(combo box,一个文本域和下拉列表的组合), JComboBox类是JComponent的子类。组合框通常显示一个可选条目,但可允许用户在一个 628 第 3 部分 Java 软件开发技术 下拉列表中选择多个不同条目。用户也可以在文本域内键入选择项

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/*
 <applet code="JComboBoxDemo" width=300 height=100>
 </applet>
*/

public class JComboBoxDemo extends JApplet
implements ItemListener {
 JLabel jl;
 ImageIcon france, germany, italy, japan;
 public void init() {
 // Get content pane
 Container contentPane = getContentPane();
 contentPane.setLayout(new FlowLayout());
 // Create a combo box and add it
 // to the panel
 JComboBox jc = new JComboBox();
 jc.addItem("France");
 jc.addItem("Germany");
 jc.addItem("Italy");
 jc.addItem("Japan");
 jc.addItemListener(this);
 contentPane.add(jc);
 // Create label
 jl = new JLabel(new ImageIcon("france.gif"));
 contentPane.add(jl);
 }
 public void itemStateChanged(ItemEvent ie) {
 String s = (String)ie.getItem();
 jl.setIcon(new ImageIcon(s + ".gif")); 
 }
} 

7)选 项 窗 格

选项窗格被封装为JTabbedPane类,JTabbedPane是JComponent的子类。使用默认构造 函数时,选项的定义方法如下所示: void addTab(String str, Component comp) 其中,str是标签的标题,comp是应加入标签的组件。典型情况下,加入的是JPanel 或 其子类。 在小应用程序中使用选项窗格的一般过程如下所示: 1. 创建JTabbedPane对象。 2. 调用addTab( ) 方法在窗格中增加一个标签(这个方法的参数是标签的标题和它包 含的组件)。 3. 重复步骤2,增加标签。 4. 将选项窗格加入小应用程序的内容窗格。

public void init() {
 JTabbedPane jtp = new JTabbedPane();
 jtp.addTab("Cities", new CitiesPanel());
 jtp.addTab("Colors", new ColorsPanel());
 jtp.addTab("Flavors", new FlavorsPanel());
 getContentPane().add(jtp);
 } 

class CitiesPanel extends JPanel {
 public CitiesPanel() {
 JButton b1 = new JButton("New York");
 add(b1);
 JButton b2 = new JButton("London");
 add(b2);
 JButton b3 = new JButton("Hong Kong");
 add(b3);
 JButton b4 = new JButton("Tokyo");
 add(b4);
 }
} 

8)JScrollPane类  流动窗格

  

public class JScrollPaneDemo extends JApplet {
 public void init() {
 // Get content pane
 Container contentPane = getContentPane();
 contentPane.setLayout(new BorderLayout());
 // Add 400 buttons to a panel
 JPanel jp = new JPanel();
 jp.setLayout(new GridLayout(20, 20));
 int b = 0;
 for(int i = 0; i < 20; i++) {
 for(int j = 0; j < 20; j++) {
 jp.add(new JButton("Button " + b));
 ++b;
 }
 }
 // Add panel to a scroll pane
 int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
 int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
 JScrollPane jsp = new JScrollPane(jp, v, h);
 // Add scroll pane to the content pane
 contentPane.add(jsp, BorderLayout.CENTER);
 }
} 

9)Jtree  树形控件

当节点扩展或收缩时, JTree 对象生成事件。 addTreeExpansionListener( ) 和 removeTreeExpansionListener( )方法注册或注销监听这些通知的监听器。

10)Jtable 表型控件

原文地址:https://www.cnblogs.com/freewsf/p/7794643.html