图形用户界面。
package graph;
/** * 边界布局案例 * * */
import java.awt.*;
import javax.swing.*;public class demo2 extends JFrame{JButton button1,button2,button3,button4,button5 = null;public static void main(String[] args) {// TODO Auto-generated method stubdemo2 demo = new demo2();}public demo2() {//创建组件button1 = new JButton("东部");button2 = new JButton("南部");button3 = new JButton("西部");button4 = new JButton("北部");button5 = new JButton("中部");//添加组件this.add(button5,BorderLayout.CENTER);this.add(button1,BorderLayout.EAST);this.add(button2,BorderLayout.SOUTH);this.add(button3,BorderLayout.WEST);this.add(button4,BorderLayout.NORTH);//设置窗体this.setTitle("边界布局案例");this.setSize(300,300);this.setLocation(500,300);this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);this.setVisible(true);}}
package graph;/*流式布局演示*/
import java.awt.*;
import javax.swing.*;public class demo3 extends JFrame{JButton button1,button2,button3,button4,button5,button6 = null;public static void main(String[] args) {// TODO Auto-generated method stubdemo3 demo =new demo3();}public demo3() {//创建组件button1 = new JButton("1");button2 = new JButton("2");button3 = new JButton("3");button4 = new JButton("4");button5 = new JButton("5");button6 = new JButton("6");this.setLayout(new FlowLayout(FlowLayout.LEFT));this.add(button1);this.add(button2);this.add(button3);this.add(button4);this.add(button5);this.add(button6);//设置窗体this.setTitle("边界布局案例");this.setSize(300,300);this.setLocation(500,300);//禁止用户改变窗体大小 this.setResizable(false);this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);this.setVisible(true);}}
package graph;/*网格布局*/import java.awt.*;
import javax.swing.*;public class demo4 extends JFrame{int siza = 9;JButton[] button =new JButton[siza];public static void main(String[] args) {// TODO Auto-generated method stubdemo4 demo = new demo4();}public demo4(){for (int i=0;i<siza;i++) {try {//非简单的数据类型,new 、。button[i] = new JButton(String.valueOf(i));} catch (Exception e) {// TODO: handle exceptione.getStackTrace();}}//设置网格布局this.setLayout(new GridLayout(3, 3,10,10));for (int i=0;i<siza;i++) {try {this.add(button[i]);} catch (Exception e) {// TODO: handle exception}}//设置窗体this.setTitle("网格布局案例");this.setSize(300,300);this.setLocation(500,300);//禁止用户改变窗体大小 this.setResizable(false);this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);this.setVisible(true);}
}
package graph;
/** * * jpanel* */import java.awt.*;
import javax.swing.*;public class demo5 extends JFrame{public static void main(String[] args) {// TODO Auto-generated method stubdemo5 demo = new demo5();}public demo5() {//创建jpanel 容器类 ,可以放置按钮 JPanel jp1 = new JPanel();JPanel jp2 = new JPanel();//创建buttonJButton button1 = new JButton("banana");JButton button2 = new JButton("apple");JButton button3 = new JButton("orange");JButton button4 = new JButton("juice");JButton button5 = new JButton("bread");JButton button6 = new JButton("rice");//向容器添加构件jp1.add(button1);jp1.add(button2);jp2.add(button4);jp2.add(button5);jp2.add(button6);this.add(jp1,BorderLayout.NORTH);this.add(jp2,BorderLayout.SOUTH);this .add(button3, BorderLayout.CENTER);this.setTitle("JPanel");this.setSize(300, 300);this.setLocation(300, 300);//关闭时自动关闭java虚拟机this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);this.setVisible(true);}
}