浅学GUI
P2: awt 介绍
package gui.awt;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class PanelTest {
public static void main(String[] args) {
Frame f1 = new Frame("窗口 1");
Panel p1 = new Panel();
f1.setLayout(null);
f1.setBounds(300,300,500,500);
f1.setBackground(new Color(1,1,1));
p1.setBounds(50,50,400,400);
p1.setBackground(new Color(208, 37, 122));
f1.add(p1);
f1.setVisible(true);
// 关闭窗口
f1.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
}
P5: 三种布局管理器
- 流形布局 FlowLayout
- 东西南北中
- 表格布局
P7: 事件监听
package gui.actionlistener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class ActionListen {
public static void main(String[] args) {
Frame f1 = new Frame("start--stop");
Button b1 = new Button("start");
Button b2 = new Button("stop");
b2.setActionCommand("这里可以自定义");
MyMoniter mm = new MyMoniter();
b1.addActionListener(mm);
b2.addActionListener(mm);
f1.add(b1,BorderLayout.EAST);
f1.add(b2,BorderLayout.WEST);
f1.pack();
f1.setVisible(true);
f1.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class MyMoniter implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("msg===> "+e.getActionCommand());
}
}
package gui.actionlistener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestText01 {
public static void main(String[] args) {
MyFrame mf = new MyFrame();
}
}
class MyFrame extends Frame {
public MyFrame(){
TextField tf = new TextField(); //文本框组件
this.add(tf); //组件添加到窗上
MyListener ml = new MyListener(); //创事件建监听器对象
tf.addActionListener(ml); //监听文本框
tf.setEchoChar('*'); //加密
this.setVisible(true);
this.pack();
//窗口监听器,关闭窗口
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
//事件监听器
class MyListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
TextField tf = (TextField) e.getSource();
System.out.println(tf.getText());
tf.setText("");
}
}