首页 > 编程语言 > 详细

java 图形界面

时间:2016-05-10 18:43:12      阅读:182      评论:0      收藏:0      [点我收藏+]

1、创建一个窗口框架

 

/**
 * java 用户界面框架
 * 2016/5/10
 */
package org.windows;

import javax.swing.*;
public class MyJFrame {

    public static void main(String[] args) {
        JFrame frame = new JFrame("MyJFrame");        //创建一个有标题的框架
        frame.setSize(400,300);        //设置宽度
        frame.setLocationRelativeTo(null);        //窗口居中显示
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    //窗口关闭时结束程序
        frame.setVisible(true);                    //这句之后窗口才会显示

    }

}

2、向窗口中添加一个按钮

/**
 * java 用户界面框架
 * 2016/5/10
 */
package org.windows;

import javax.swing.*;
public class MyJFrame {

    public static void main(String[] args) {
        JFrame frame = new JFrame("MyJFrame");        //创建一个有标题的框架
        
        JButton jbtOK = new JButton("OK");        //新建一个按钮
        frame.add(jbtOK);                        //将按钮添加到窗口中
        
        frame.setSize(400,300);        //设置宽度
        frame.setLocationRelativeTo(null);        //窗口居中显示
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    //窗口关闭时结束程序
        frame.setVisible(true);                    //这句之后窗口才会显示

    }

}

 

 3、布局管理器

  1) FlowLayout  :按照组件的添加顺序从左到右排列在容器中,当放满一行就开始新的一行

    向一个窗口添加3个标签和文本域

    

 1 /**
 2  * java 布局管理器 FlowLayout
 3  * 2016/5/10
 4  **/
 5 
 6 package org.windows;
 7 
 8 import javax.swing.*;
 9 
10 import java.awt.FlowLayout;
11 
12 public class MyFlowLayout extends JFrame{
13     public MyFlowLayout(){
14         setLayout(new FlowLayout(FlowLayout.RIGHT,10,20));        //设置右靠齐
15 
16         add(new JLabel("First Name"));            //添加标签
17         add(new JTextField(8));                    //添加文本域,长度为8
18         add(new JLabel("MI"));
19         add(new JTextField(1));
20         add(new JLabel("Last Name"));
21         add(new JTextField(8));
22     }
23     public static void main(String[] args) {
24         MyFlowLayout frame = new MyFlowLayout();
25         frame.setTitle("MyFlowLayout");
26         frame.setSize(250,200);
27         frame.setLocationRelativeTo(null);
28         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
29         frame.setVisible(true);
30         
31     }
32 
33 }

 

 2)GridLayout  :以 网格的形式管理组件,组件按照它们添加的顺序从左到右排列。

          向一个窗口添加3个标签和文本域

  

 1 /**
 2  * java b布局管理器 GridLayout
 3  * 2016/5/10
 4  **/
 5 package org.windows;
 6 
 7 import javax.swing.*;
 8 import java.awt.GridLayout;
 9 public class MyGridLayout extends JFrame{
10     public MyGridLayout(){
11         setLayout(new GridLayout(3, 2, 25, 25));        //设置: 行数(rows):3  列数(columns):2  水平间隔(hgap):5  垂直间隔(vgap):5 
12 
13         add(new JLabel("First Name"));        //标签
14         add(new JTextField(8));                //文本域
15         add(new JLabel("MI"));
16         add(new JTextField(1));
17         add(new JLabel("Last Name"));
18         add(new JTextField(8));
19     }
20 
21     public static void main(String[] args) {
22         MyGridLayout  frame = new MyGridLayout();
23         frame.setSize(200,125);
24         frame.setLocationRelativeTo(null);
25         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
26         frame.setVisible(true);
27     }
28 }

 

 3)BorderLayout: BorderLayout管理器将容器分为5个区域,东区,南区,西区,,北区,中央。

  

 1 /**
 2  * java 布局管理器 BorderLayout
 3  * 2016/5/10
 4  **/
 5 package org.windows;
 6 
 7 import java.awt.BorderLayout;
 8 import javax.swing.*;
 9 
10 public class MyBorderLayout extends JFrame {
11     public MyBorderLayout(){
12         setLayout(new BorderLayout(5,10));
13 
14         add(new JButton("East"),BorderLayout.EAST);
15         add(new JButton("South"),BorderLayout.SOUTH);
16         add(new JButton("West"),BorderLayout.WEST);
17         add(new JButton("North"),BorderLayout.NORTH);
18         add(new JButton("Center"),BorderLayout.CENTER);
19     }
20     public static void main(String[] args) {
21         MyBorderLayout frame = new MyBorderLayout();
22 
23         frame.setTitle("MyBorderLayout");
24         frame.setSize(300,200);
25         frame.setLocationRelativeTo(null);
26         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
27         frame.setVisible(true);
28 
29     }
30 
31 }

 4)综合布局实验

 1 /**
 2  * java  布局管理器,小试牛刀
 3  * 2016/5/10
 4  **/
 5 package org.windows;
 6 
 7 import javax.swing.*;
 8 import java.awt.*;
 9 public class TestPanels extends JFrame {
10     public TestPanels(){
11         JPanel p1 = new JPanel();
12         p1.setLayout(new GridLayout(4,3));          //网格布局管理器
13         for(int i = 1; i <= 9; i++){
14             p1.add(new JButton(""+i));
15         }
16         p1.add(new JButton(""+0));
17         p1.add(new JButton("Start"));
18         p1.add(new JButton("Stop"));
19         
20         JPanel p2 = new JPanel(new BorderLayout());
21         p2.add(new JTextField("Time to be displany here!!"),BorderLayout.NORTH);            //5个区域的布局管理器
22         p2.add(p1,BorderLayout.CENTER);
23 
24         add(p2,BorderLayout.EAST);
25         add(new JButton("Food to be placed here"),BorderLayout.CENTER);
26         
27 
28     }
29     public static void main(String[] args) {
30         TestPanels frame = new TestPanels();
31         frame.setTitle("TestPanels");
32         frame.setSize(400,250);
33         frame.setLocationRelativeTo(null);
34         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
35         frame.setVisible(true);
36     }
37 }

运行效果:

技术分享

 4、Swing GUI  属性的使用

 1 /**
 2  * java Swing GUI 组件的公共特性
 3  * 2016/5/10
 4  **/ 
 5 package org.windows;
 6 
 7 import javax.swing.*;
 8 import javax.swing.border.*;
 9 import java.awt.*;
10 
11 public class TestSwingAttribute extends JFrame{
12     public TestSwingAttribute(){
13         JPanel p1 = new JPanel(new FlowLayout(FlowLayout.LEFT,2,2));
14         JButton jbtLeft = new JButton("Left");
15         JButton jbtCenter = new JButton("Center");
16         JButton jbtRight = new JButton("Right");
17         jbtLeft.setBackground(Color.WHITE);     //设置背景色为白色
18         jbtCenter.setForeground(Color.GREEN);       //设置前景色、
19         jbtRight.setToolTipText("This is the Right button");        //鼠标放在按钮上会显示提示文本
20         p1.add(jbtLeft);
21         p1.add(jbtCenter);
22         p1.add(jbtRight);
23         p1.setBorder(new TitledBorder("Three Buttons"));        //设置标题边界
24 
25         Font largeFont  = new Font("TimesRoman",Font.BOLD,20);        //字体属性
26         Border lineBorder = new  LineBorder(Color.BLACK,2);            //设置线边界
27 
28         JPanel p2 = new JPanel(new GridLayout(1, 2, 5, 5));
29         JLabel jlblRed = new JLabel("Red");
30         JLabel jlblOrange = new JLabel("Orange");
31         jlblRed.setForeground(Color.RED);        //设置背景色
32         jlblOrange.setForeground(Color.ORANGE);
33         jlblRed.setFont(largeFont);            //设置字体
34         jlblOrange.setFont(largeFont);
35         jlblRed.setBorder(lineBorder);        //设置线边界
36         jlblOrange.setBorder(lineBorder);
37         p2.add(jlblRed);
38         p2.add(jlblOrange);
39         p2.setBorder(new TitledBorder("Two Labels"));        //设置标题边界
40         setLayout(new GridLayout(2, 1, 5, 5));
41         add(p1);
42         add(p2);
43         
44     }
45 
46     public static void main(String[] args) {
47         JFrame frame = new TestSwingAttribute();
48         frame.setTitle("TestSwingAttribute");
49         frame.setSize(300,150);
50         frame.setLocationRelativeTo(null);
51         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
52         frame.setVisible(true);
53 
54     }
55 
56 }

 

 运行效果:

技术分享技术分享技术分享

 

java 图形界面

原文:http://www.cnblogs.com/snail-lb/p/5478178.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!