GUI
Frame
在这里插入代码片package GUI;
import java. awt. *;
public class TestFrame {
public static void main ( String[ ] args) {
Frame frame = new Frame ( "第一个窗口" ) ;
frame. setVisible ( true ) ;
frame. setSize ( 400 , 400 ) ;
frame. setBackground ( Color. BLACK) ;
frame. setLocation ( 500 , 500 ) ;
frame. setResizable ( false ) ;
}
}
Panel
package GUI;
import java. awt. *;
import java. awt. event. WindowAdapter;
import java. awt. event. WindowEvent;
public class TestPanel {
public static void main ( String[ ] args) {
Frame MyFrame = new Frame ( ) ;
Panel panel = new Panel ( ) ;
MyFrame. setLayout ( null) ;
MyFrame. setBounds ( 300 , 300 , 500 , 500 ) ;
MyFrame. setBackground ( new Color ( 1 , 1 , 1 ) ) ;
panel. setBounds ( 50 , 50 , 300 , 300 ) ;
panel. setBackground ( new Color ( 2 , 25 , 255 ) ) ;
MyFrame. add ( panel) ;
MyFrame. setVisible ( true ) ;
MyFrame. addWindowListener ( new WindowAdapter ( ) {
@Override
public void windowClosing ( WindowEvent e) {
System. exit ( 0 ) ;
}
} ) ;
}
}
东西南北中
Button north = new Button ( "north" ) ;
Button south = new Button ( "south" ) ;
Button west = new Button ( "west" ) ;
Button east = new Button ( "east" ) ;
Button center = new Button ( "center" ) ;
frame. add ( east, BorderLayout. EAST) ;
frame. add ( north, BorderLayout. NORTH) ;
frame. add ( south, BorderLayout. SOUTH) ;
frame. add ( east, BorderLayout. EAST) ;
frame. add ( west, BorderLayout. WEST) ;
frame. add ( center, BorderLayout. CENTER) ;
流式布局
Button button1 = new Button ( "button1" ) ;
Button button2 = new Button ( "button2" ) ;
Button button3 = new Button ( "button3" ) ;
frame. setLayout ( new FlowLayout ( ( FlowLayout. CENTER) ) ) ;
frame. add ( button1) ;
frame. add ( button2) ;
frame. add ( button3) ;
网格布局
Button btn1 = new Button ( "btn1" ) ;
Button btn2 = new Button ( "btn2" ) ;
Button btn3 = new Button ( "btn3" ) ;
Button btn4 = new Button ( "btn4" ) ;
Button btn5 = new Button ( "btn5" ) ;
Button btn6 = new Button ( "btn6" ) ;
frame. setLayout ( new GridLayout ( 3 , 2 ) ) ;
frame. add ( btn1) ;
frame. add ( btn2) ;
frame. add ( btn3) ;
frame. add ( btn4) ;
frame. add ( btn5) ;
frame. add ( btn6) ;
事件监听
public class Text01 {
public static void main ( String[ ] args) {
Frame frame = new Frame ( ) ;
Button button = new Button ( "btn1" ) ;
Button button1= new Button ( "btn2" ) ;
button1. setActionCommand ( "button1" ) ;
MyActionListener myActionListener = new MyActionListener ( ) ;
button. addActionListener ( myActionListener) ;
button1. addActionListener ( myActionListener) ;
frame. add ( button, BorderLayout. NORTH) ;
frame. add ( button1, BorderLayout. SOUTH) ;
windowClose ( frame) ;
frame. setVisible ( true ) ;
frame. pack ( ) ;
}
private static void windowClose ( Frame frame) {
frame. addWindowListener ( new WindowAdapter ( ) {
@Override
public void windowClosing ( WindowEvent e) {
System. exit ( 0 ) ;
}
} ) ;
}
}
class MyActionListener implements ActionListener {
@Override
public void actionPerformed ( ActionEvent e) {
System. out. println ( "加油" + e. getActionCommand ( ) ) ;
}
}
文本框监听
public class Text02 {
public static void main ( String[ ] args) {
MyFrame01 frame = new MyFrame01 ( ) ;
windowClose ( frame) ;
}
private static void windowClose ( Frame frame) {
frame. addWindowListener ( new WindowAdapter ( ) {
@Override
public void windowClosing ( WindowEvent e) {
System. exit ( 0 ) ;
}
} ) ;
}
}
class MyFrame01 extends Frame {
public MyFrame01 ( ) {
TextField textField = new TextField ( ) ;
add ( textField) ;
MyActionListener myActionListener = new MyActionListener ( ) ;
textField. addActionListener ( myActionListener) ;
textField. setEchoChar ( '*' ) ;
setVisible ( true ) ;
pack ( ) ;
}
private static void windowClose ( Frame frame) {
frame. addWindowListener ( new WindowAdapter ( ) {
@Override
public void windowClosing ( WindowEvent e) {
System. exit ( 0 ) ;
}
} ) ;
}
}
class MyActionListener implements ActionListener {
@Override
public void actionPerformed ( ActionEvent e) {
TextField field = ( TextField) e. getSource ( ) ;
System. out. println ( field. getText ( ) ) ;
field. setText ( "" ) ;
}
}