Swing和抽象窗口工具包
在java中,使用两组类来开发程序的用户界面:Swing早期的称为抽象窗口工具包的一组类。这些类让你能够创建图形用户界面以及接收用户输入。
Swing提供了创建可运行的GUI所需的所有类,不管用户在哪种操作系统中运行Java程序。
使用组件
组织图形用户界面时,需要使用两类对象:组件和容器。组件是用户界面中的独立元素,如按钮或滑块;容器是用于容纳其他组件的组件。
创建界面的第一步是创建能够容纳组件的容器。在应用程序中,该容器通常是窗口或框架。
框架:包含用户运行软件时希望看到的所有常见的窗口特性,如关闭按钮、最大化按钮和最小化按钮。
这些容器分别是使用Swing包中的JWindow和JFrame类创建的。为了在Java程序中引用Swing包而且无需使用完整的包和类名,可使用下面的语句:
import javax.swing.*;
1 import javax.swing.*;2
3 public class SalutonFrame extendsJFrame {4 publicSalutonFrame() {5 super("Saluton mondo!");6 setLookAndFeel();7 setSize(350, 100);8 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);9 setVisible(true);10 }11
12 private voidsetLookAndFeel() {13 try{14 UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");15 } catch(Exception exc) {16
17 }18 }19
20 public static voidmain(String[] arguments) {21 SalutonFrame frame = newSalutonFrame();22 }23 }
如上述代码所示:
创建框架时,必须在框架的构造函数中执行几种操作:
1.调用超类JFrame的构造函数;如:super();super("Main Frame");
2.调置框架的标题;setTitle("Main Frame");
3.设置框架的大小;setSize(350, 125);也可以先用组件填充它,然后调用不带参数的pack()方法,
4.设置框架的外观;Java包含了一个增强的外观,名为Nimbus,当在类中使用时,必须先启用。通过调用主Swing包中UIManager类的setLookAndFeel()方法可以设置外观;该方法接受一个参数:外观类的完整名称。
5.定义用户关闭框架时应执行的操作;
6.显示框架:使用true作为参数调用setVisible()方法
按钮
JButton okButton = new JButton("OK");
创建JButton等组件后,应调用add()方法将其加入到容器中:add(okButton);
在容器中添加组件时,不需要指明组件在容器中显增的位置,组件的布局由被称为布局管理器的对象决定。最简单的布局管理器是FlowLayout类,它位于java.awt包中。
FlowLayout flo = new FlowLayout();
创建布局管理器后,调用容器的setLayout()方法将其管理器同容器关联起来
setLayout(flo);
1 import javax.swing.*;2 import java.awt.*;3
4 public class Playback extendsJFrame {5 publicPlayback() {6 super("Playback");7 setLookAndFeel();8 setSize(225, 80);9 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);10 FlowLayout flo = newFlowLayout();11 setLayout(flo);12 JButton play = new JButton("Play");13 JButton stop = new JButton("Stop");14 JButton pause = new JButton("Pause");15 add(play);16 add(stop);17 add(pause);18 setVisible(true);19 }20
21 private voidsetLookAndFeel() {22 try{23 UIManager.setLookAndFeel("com.sun.java.swing.plaf.NimbusLookAndFeel");24 } catch(Exception exc) {25
26 }27 }28 public static voidmain(String[] arguments) {29 Playback frame = newPlayback();30 }31
32 }
按钮代码示例
标签和文本框
1 FlowLayout flo = newFlowLayout();2 setLayout(flo);3 JLabel pageLabel = new JLabel("Web page address: ", JLabel.RIGHT);4 JTextField pageAddress = new JTextField(20);5 add(pageLabel);6 add(pageAddress);
标签和文本框示例
JTextField.getText()获取文本;JTextField.setText()设置文本。
复选框
1 FlowLayout flo = newFlowLayout();2 setLayout(flo);3 JCheckBox jumoSize = new JCheckBox("Jumbo Size", true);4 add(jumoSize);
复选框示例
组合框
JComboBox组件是一个弹出式选择列表,也可以设置成能够接收文本输入。
1 FlowLayout flo = newFlowLayout();2 setLayout(flo);3 JComboBox profession = newJComboBox();4 profession.addItem("Butcher");5 profession.addItem("Backer");6 profession.addItem("Candlestick maker");7 profession.addItem("Fletcher");8 profession.addItem("Fighter");9 profession.addItem("Technical writer");10 add(profession);
复选框代码示例
要让JComboBox组件能够接收文本输入,必须使用true作为参数调用其setEditable()方法,如profession.setEditable(true); 必须在将组件加入容器中之前调用该方法。
文本区域
1 FlowLayout flo = newFlowLayout();2 setLayout(flo);3 JTextArea comments = new JTextArea(8, 40);4 add(comments);
文本区域代码示例
JTextArea组件允许用户输入多行文本,可以指定该组件的宽度和高度。
当用户输入的文本超出一行时,可以调用文本区域的两个方法来指定组件的处理方式。调用带有true参数的setLineWrap(boolean)方法可以让文本换行。
当用户输入的文件到达区域的底部时,它会自动增大区域,但是在区域的右边缘或下边缘并没有滚动条。要想用更好的方式来实现文本区域组件,必须将其放置到名为滚动面板的容器中。
在Swing中,可以将组件添加到一个滚动面板中来实现滚动。滚动面板是由JScrollPan类表示的一个容器。
1 FlowLayout flo = newFlowLayout();2 setLayout(flo);3 JTextArea comments = new JTextArea("I am ok\n see you...", 8, 40);4 comments.setLineWrap(true);5 comments.setWrapStyleWord(true);6 JScrollPane scroll = newJScrollPane(comments, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,7 JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);8 add(scroll);
文本区域代码示例
面板
面板,是使用Swing中的JPanel类创建的。JPanel对象是可在图形用户界面中使用的最简单的一种容器,用于将显示区域划分成不同的组件组。将显示区域分成几部分后,可以在每个部分使用不同的布局管理器。
1 FlowLayout flo = newFlowLayout();2 setLayout(flo);3 JPanel topRow = newJPanel();4 topRow.setLayout(flo);5 add(topRow);
面板示例代码
通过调用面板的add()方法可以将组件添加到面板中。还可以通过调用setLayout()方法,直接给面板指派一个布局管理器。
当需要在界面中包含绘画区域时(如显示图像文件中的图像),也可以使用面板。
JPanel的另一种用途是,用于创建可加入到其他类的组件。
创建自己的组件
1 importjava.io.IOException;2 import java.nio.file.*;3 import javax.swing.*;4
5 public class FreeSpacePanel extendsJPanel {6 JLabel spaceLabel = new JLabel("Disk space: ");7 JLabel space = newJLabel();8
9 publicFreeSpacePanel() {10 super();11 add(spaceLabel);12 add(space);13 try{14 setValue();15 } catch(IOException ioe) {16 space.setText("Error");17 }18 }19
20 private final void setValue() throwsIOException {21 //get the current file storage pool
22 Path current = Paths.get("");23 FileStore store =Files.getFileStore(current);24 //find the free storage space
25 long totalSpace =store.getTotalSpace();26 long freeSpace =store.getUnallocatedSpace();27 //get this as a persentage (with two digits)
28 double percent = (double)freeSpace / (double)totalSpace * 100;29 percent = (int)(percent * 100) / (double) 100;30 //set the label's text
31 space.setText(freeSpace + " free out of " + totalSpace + " ("
32 + percent + "%)");33 }34 }
FreeSpacePanel
1 import java.awt.*;2 import javax.swing.*;3
4 public class FreeSpaceFrame extendsJFrame{5 publicFreeSpaceFrame() {6 super("Disk Free Space");7 setLookAndFeel();8 setSize(500, 100);9 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);10 FlowLayout flo = newFlowLayout();11 setLayout(flo);12 FreeSpacePanel freePanel = newFreeSpacePanel();13 add(freePanel);14 setVisible(true);15 }16 private voidsetLookAndFeel() {17 try{18 UIManager.setLookAndFeel("com.sun.java.swing.plaf.NimbusLookAndFeel");19 } catch(Exception exc) {20
21 }22 }23 public static voidmain(String[] arguments) {24 FreeSpaceFrame frame = newFreeSpaceFrame();25 }26 }
FreeSpaceFrame
上述应用程序可以显示可用的磁盘空间、总的磁盘空间,以及两者的百分比。