GUI(Graphical User Interface):图形用户接口,用图形的方式,来显示计算机操作的界面,这样更方便更直接。
GLI(Command line Interfacw):命令行用户接口,就是常见的Dos命令行操作,需记忆一些常用命令,操作不雅观。
java为GUI提供的对象都存在java.awt和javax.swing两个包中。
Awt和Swing:
java.awt:Abstract Window ToolKit(抽象窗口工具包),需要调用本地系统方法实现功能。属重量级控件。
javax.swing:在AWT的基础上,建立的一套图形界面系统,其中提供了更多的组件,而且完全由java实现。增强了移植性,属轻量级控件。
Container:为容器,是一个特殊的组件,该组件可以通过add方法添加其他组件进来。
容器中组件的排放方式,就是布局。
常见布局管理器:
FlowLayout(流式布局管理器):从左到右的顺序排列--Panel默认的布局管理器。
BorderLayout(边界布局管理器):东西南北中--Frame默认的布局管理器。
GridLayout(网格布局管理器):规则的矩阵。
CardLayout(卡片布局管理器):选项卡。
GridBagLayout(网络包布局管理器):非规则矩阵。
Awt:
public class FrameDemo {
public static void main(String[] args) {
Frame f=new Frame("my frame");
// f.setSize(500,400);
// f.setLocation(400,200);
f.setBounds(400,200,500,400);
f.setLayout(new FlowLayout());//设置流式布局
Button but=new Button("一个按钮");
f.add(but);//将按钮添加到窗体中
f.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e) {
System.out.println("closing....."+e);
System.exit(0);
}
});
//在按钮上添加一个监听。
but.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("button..run");
System.exit(0);
}
});
f.setVisible(true);
System.out.println("over");
}
}
运行:public class MouseAndKeyDemo {
private Frame f;
private TextField tf;
private Button but;
public MouseAndKeyDemo() {
init();
}
private void init() {
f=new Frame("演示鼠标和键盘监听");
f.setBounds(400, 200, 500, 400);
f.setLayout(new FlowLayout());
tf=new TextField(35);
but=new Button("一个按钮");
f.add(tf);
f.add(but);
myEvent();
f.setVisible(true);
}
public void myEvent() {
//给文本框添加键盘监听
tf.addKeyListener(new KeyAdapter(){
@Override
public void keyPressed(KeyEvent e) {
/*System.out.println("key run..."+KeyEvent.getKeyText(e.getKeyCode())+"::::"+e.getKeyCode());
int code=e.getKeyCode();
if(!(code>=KeyEvent.VK_0 && code<=KeyEvent.VK_9)){
System.out.println("必须是数字");
e.consume();
}*/
if(e.isControlDown()&&e.getKeyCode()==KeyEvent.VK_ENTER){
System.out.println("enter run...");
}
}
});
f.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
but.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
}
});
//在按钮上添加一个鼠标监听
but.addMouseListener(new MouseAdapter(){
private int count =1;
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseClicked(MouseEvent e) {
if(e.getClickCount()==2)
tf.setText("mouse double click..."+count++);
}
});
}
public static void main(String[] args) {
new MouseAndKeyDemo();
}
}
运行:Swing: