🏴☠️ 狂补GUI编程(一)🏴☠️
🪅frame窗口:
public class gui_study01 {
public static void main ( String [ ] args) {
Frame frame= new Frame ( "我的java第一个图形界面窗口\uD83E\uDE82" ) ;
frame. setVisible ( true ) ;
frame. setBounds ( 300 , 300 , 500 , 500 ) ;
frame. setBackground ( Color . PINK) ;
MyFrame a1= new MyFrame ( 0 , 0 , 500 , 500 ) ;
MyFrame a2= new MyFrame ( 508 , 0 , 500 , 500 ) ;
MyFrame a3= new MyFrame ( 1016 , 0 , 500 , 500 ) ;
}
}
🪅MyFrame自定义面板
public class MyFrame extends Frame {
static int id= 0 ;
public MyFrame ( int x, int y, int w, int h) {
super ( "MyFrame\uD83E\uDE85" + ( ++ id) ) ;
setVisible ( true ) ;
setBounds ( x, y, w, h) ;
setBackground ( Color . PINK) ;
setResizable ( false ) ;
addWindowListener ( new WindowAdapter ( ) {
@Override
public void windowClosing ( WindowEvent e) {
System . exit ( 0 ) ;
}
} ) ;
}
}
🪅panel面板:
🪂解决了关闭事件(适配器)
public class TestPanel {
public static void main ( String [ ] args) {
MyFrame frame= new MyFrame ( 300 , 300 , 500 , 500 ) ;
Panel panel= new Panel ( ) ;
frame. setLayout ( null ) ;
panel. setBounds ( 100 , 100 , 300 , 300 ) ;
panel. setBackground ( new Color ( 109 , 208 , 162 ) ) ;
frame. add ( panel) ;
frame. addWindowListener ( new WindowAdapter ( ) {
@Override
public void windowClosing ( WindowEvent e) {
System . exit ( 0 ) ;
}
} ) ;
}
}
🪅Layout布局
🪂流式布局
public class TestFlowLayout {
public static void main ( String [ ] args) {
MyFrame frame = new MyFrame ( 200 , 200 , 500 , 500 ) ;
Button button1 = new Button ( "\uD83E\uDE821" ) ;
Button button2 = new Button ( "\uD83E\uDDF82" ) ;
Button button3 = new Button ( "\uD83C\uDF873" ) ;
frame. setLayout ( new FlowLayout ( ) ) ;
frame. add ( button1) ;
frame. add ( button2) ;
frame. add ( button3) ;
}
}
🪂东西南北东-布局
public class TestBorderLayout {
public static void main ( String [ ] args) {
MyFrame frame = new MyFrame ( 200 , 200 , 500 , 500 ) ;
Button east = new Button ( "East" ) ;
Button west = new Button ( "West" ) ;
Button south = new Button ( "South" ) ;
Button north = new Button ( "North" ) ;
Button center = new Button ( "Center" ) ;
frame. add ( east, BorderLayout . EAST) ;
frame. add ( west, BorderLayout . WEST) ;
frame. add ( south, BorderLayout . SOUTH) ;
frame. add ( north, BorderLayout . NORTH) ;
frame. add ( center, BorderLayout . CENTER) ;
}
}
🪂表格布局
public class TestGridLayout {
public static void main ( String [ ] args) {
MyFrame frame = new MyFrame ( 200 , 200 , 500 , 500 ) ;
Button button1 = new Button ( "button1" ) ;
Button button2 = new Button ( "button2" ) ;
Button button3 = new Button ( "button3" ) ;
Button button4 = new Button ( "button4" ) ;
Button button5 = new Button ( "button5" ) ;
Button button6 = new Button ( "button6" ) ;
frame. setLayout ( new GridLayout ( 2 , 3 ) ) ;
frame. add ( button1) ;
frame. add ( button2) ;
frame. add ( button3) ;
frame. add ( button4) ;
frame. add ( button5) ;
frame. add ( button6) ;
}
}
🏅实操作业:
🎇写出下面图形设计:
public class ExDemo01 {
public static void main ( String [ ] args) {
MyFrame frame = new MyFrame ( 200 , 200 , 500 , 500 ) ;
Button button = new Button ( "1" ) ;
Button button2 = new Button ( "2" ) ;
Button button3 = new Button ( "3" ) ;
Button button4 = new Button ( "4" ) ;
Button button5 = new Button ( "5" ) ;
Button button6 = new Button ( "6" ) ;
Button a = new Button ( "a" ) ;
Button b = new Button ( "b" ) ;
Button c = new Button ( "c" ) ;
Button d = new Button ( "d" ) ;
frame. setLayout ( new GridLayout ( 2 , 1 ) ) ;
Panel panel_1 = new Panel ( ) ;
Panel panel_2 = new Panel ( ) ;
Panel panel_3 = new Panel ( ) ;
Panel panel_4 = new Panel ( ) ;
frame. add ( panel_1) ;
frame. add ( panel_2) ;
panel_1. setLayout ( new GridLayout ( 1 , 3 ) ) ;
panel_2. setLayout ( new GridLayout ( 1 , 3 ) ) ;
panel_1. add ( a) ;
panel_1. add ( panel_3) ;
panel_1. add ( b) ;
panel_2. add ( button) ;
panel_2. add ( panel_4) ;
panel_2. add ( button2) ;
panel_3. setLayout ( new GridLayout ( 2 , 1 ) ) ;
panel_4. setLayout ( new GridLayout ( 2 , 2 ) ) ;
panel_3. add ( c) ;
panel_3. add ( d) ;
panel_4. add ( button3) ;
panel_4. add ( button4) ;
panel_4. add ( button5) ;
panel_4. add ( button6) ;
}
}