GUI(Graphical User Interfaces)
- 提供程序的外观和感觉
- 利用GUI图形用户界面,输入输出结果
专门的类库,两个
java.awt(AWT--abstract window toolkit)
javax.swing
包中的主要类或接口之间的继承关系:
object->
LayoutManager
Component->
Label
Button
Canvas
Checkbox
Container->
Pannel->
Applet
Window->
Dialog
Frame
Component类
repaint();
paint();
update();
getFont();
setFont();
setBackground();
SetForeground();
setSize();
setLocation();
imageUpdate();
setEnable();
isVisible();
isValid();
...
Container类
add();//添加组件到容器
remove();//删除某个组件
LayoutManager(布局管理器)
FlowLayout
BorderLayout
GridLayout
CardLayout
BoxLayout
GridBagLayout
Swing介绍
- 不依赖本地代码,全平台,显示外观课调。
- AWT有的它都有前面加J,如:JLabel,JMenu..
Swing组件的分类
- 顶层容器JFrame,JApplet,JDialog,JWindow. 4个
- 普通容器JPanel,JScrollPane,JSplitPane,JToolBar
- 特殊容器,在GUI上起特殊作用的中间层JInternalFrame,JLayeredPanel,JRootPane.
- 基本控件,实现人机交互的组件,JButton,JComboBox,JList,JMent,JSlider,JTextField
- 向用户显示不可编辑信息的组件JLabel,JProgressBar,ToolTip
向用户显示能被编辑的格式化信息的组件JColorChooser,JFileChooser,JTable,JTextArea
Swing基本规则
Swing组件不能直接添加到顶层容器中(JFrame,JApplet,JDialog,JWindow. )
如添加到JFrame
//方法一:
frame.getContentPane().add(childComponent);
//方法二:(建立中间容器JPanel或者JDesktopPane,添加到容器中,用setContentPane()方法把该容器设置为JFrame的内容面板)
JPanel contantPane=new JPanel();
.....//把其他组件添加到JPanel中;
栗子:
package java8danyuan;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LabelandTextTest extends JFrame {
private JTextField textField1,textField2,textField3;
private JTextArea textArea1,textArea2;
private JPasswordField passwordField;
private JButton copyButton;
//set up GUI
public LabelandTextTest(){
super("关于JTextArea/JPasswordField/JTextArea的使用");
Container container=getContentPane();//设置容器
container.setLayout(new FlowLayout());//设置为流式布局
JLabel label1=new JLabel("用户名:");//新建“用户名”标签
container.add(label1);//加入container
textField1=new JTextField(10);//新建文本区1对象
container.add(textField1);//输入区域对象加入container
container.add(new JLabel("口令:"));//新建“口令”标签
passwordField=new JPasswordField(10);//新建密码输入框对象
container.add(passwordField);//密码输入框对象加入container
Icon bug= new ImageIcon("C:\\Users\\Administrator\\eclipse\\java-neon\\eclipse\\eclipse.exe");
JLabel label2=new JLabel("照片",bug,SwingConstants.LEFT);//图标菜单对象
label2.setHorizontalTextPosition(SwingConstants.CENTER);
label2.setVerticalTextPosition(SwingConstants.BOTTOM);
container.add(label2);
textField3=new JTextField("显示用户名或口令",30);//建立文本区对象3
textField3.setEditable(false);//设置为不可改
container.add(textField3);//文本区对象加入container
TextFieldHandler handler=new TextFieldHandler();
textField1.addActionListener(handler);//建立监听
passwordField.addActionListener(handler);//建立监听
Box box=Box.createHorizontalBox();
String string="This is a demo string to\n"+"illustrate textarea programming \n";
textArea1=new JTextArea(string,5,12);
box.add(new JScrollPane(textArea1));
copyButton=new JButton("拷贝");
box.add(copyButton);
copyButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event)
{textArea2.setText(textArea1.getSelectedText());
}
}
);
textArea2=new JTextArea(5,12);
textArea2.setEditable(false);
box.add(new JScrollPane(textArea2));
container.add(box);
setSize(500,400);
setVisible(true);
}
public static void main(String args []){
LabelandTextTest application=new LabelandTextTest();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private class TextFieldHandler implements ActionListener{
public void actionPerformed(ActionEvent event){
String string="";
if (event.getSource()==textField1)
string="textField1:"+event.getActionCommand();
else if (event.getSource()==passwordField){
string="passwordField:"+new String(passwordField.getPassword());
}
textField3.setText(string);
}
}
}