java Swing圖形化介面

來源:互聯網
上載者:User

標籤:

 

 

 

  學過java的人應該對java的圖形化介面很是反感,特別是接觸java不久的人。如果想和其他語言那樣用滑鼠拖拽,可以使用wondosbulider外掛程式。但是用起來也不是那麼方便。當然對於不樂意寫代碼的人來說真是太幸福了。

  然而用代碼去實現java的圖形化介面並非那麼麻煩。總結起來就是~

1.定義你需要使用的面板,按鈕,文本,標籤。

2.對這些執行個體化。  然後把按鈕,文本,標籤按照你想要的方法放到面板上。

3.把面板放在介面上。  如果介面簡單的話,面板也是可以不使用的,面板可以方便管理一些按鈕。

 

 

如何去簡單的添加一些按鈕那?

定義面板按鈕~執行個體化面板按鈕~將按鈕添加到面板上。

 

package Swing;import java.awt.FlowLayout;import java.awt.Frame;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;public class FrameDemo extends JFrame {    private JPanel jp,jp1;    //聲明面板,面板功能:可添加按鈕文本等,方便管理介面。    private  JButton b1;   //聲明 按鈕    private JButton b2,b3;        public FrameDemo  ()    {        super("測試視窗");        jp = new JPanel();          b1 = new JButton("按鈕1");        b2 = new JButton("按鈕2");//執行個體化 jp b1 b2         jp.add(b1);           jp.add(b2);                    this.add(jp);   //按鈕添加到面板,面板添加到介面。        this.setSize(300,200);   //設定 視窗寬度300,高度200        this.setLocation(100,100);  //設定視窗左上方座標,視窗開啟時的位置。以左上定點為0.0        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    }    public static void main(String[] args) {         FrameDemo frame = new FrameDemo();         frame.setVisible(true);                  }}

    下面介紹幾個布局,讓你的介面更加美觀

流布局~組建從左至右依次安排到面板上。也是預設布局

 

package Swing;import java.awt.FlowLayout;import java.awt.Frame;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;public class FrameDemo extends JFrame {    private JPanel jp;    //聲明面板,面板功能:可添加按鈕文本等,方便管理介面。    private  JButton b1;   //聲明 按鈕    private JButton b2,b3,b4;        public FrameDemo  ()    {        super("測試視窗");        jp = new JPanel(); //  先對面板執行個體化,再對面板進行布局。      (靠左對齊,水平間距10,垂直間距15)可更改。        FlowLayout layout = new FlowLayout(FlowLayout.LEFT,10,25);        jp.setLayout(layout);                 b1 = new JButton("按鈕1");        b2 = new JButton("按鈕2");        b3 = new JButton("按鈕3");        b4 = new JButton("按鈕4");        jp.add(b1);           jp.add(b2);        jp.add(b3);        jp.add(b4);        this.add(jp);   //按鈕添加到面板,面板添加到介面        this.setSize(300,200);   //設定 視窗寬度300,高度200        this.setLocation(100,100);  //設定視窗左上方座標,視窗開啟時的位置。        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    }    public static void main(String[] args) {         FrameDemo frame = new FrameDemo();         frame.setVisible(true);                  }}

執行結果

邊界布局~有五個位置,東南西北中~預設為中

package Swing;import java.awt.BorderLayout;import java.awt.FlowLayout;import java.awt.Frame;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;public class FrameDemo extends JFrame {    private JPanel jp;    //聲明面板,面板功能:可添加按鈕文本等,方便管理介面。    private  JButton b1;   //聲明 按鈕    private JButton b2,b3,b4;        public FrameDemo  ()    {        super("測試視窗");        jp = new JPanel(); //  先對面板執行個體化,再對面板進行布局。      (靠左對齊,水平間距10,垂直間距15)可更改。        jp.setLayout(new BorderLayout());                 b1 = new JButton("按鈕1");        b2 = new JButton("按鈕2");//執行個體化 jp b1 b2         b3 = new JButton("按鈕3");        b4 = new JButton("按鈕4");        jp.add(b1,BorderLayout.WEST);           jp.add(b2,BorderLayout.WEST);    //按鈕2將按鈕1覆蓋, 未設定按鈕大小,按鈕會的長度會是面板的長度。        jp.add(b3,BorderLayout.NORTH);        jp.add(b4,BorderLayout.SOUTH);        this.add(jp);   //按鈕添加到面板,面板添加到介面        this.setSize(500,500);   //設定 視窗寬度500,高度500        this.setLocation(100,100);  //設定視窗左上方座標,視窗開啟時的位置。        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    }    public static void main(String[] args) {         FrameDemo frame = new FrameDemo();         frame.setVisible(true);                  }}

注意,按鈕1被按鈕2覆蓋

網格布局以及卡片布局都是一樣的設定步驟,唯一改變的是布局方式改變。只要依次添加組件,注意個組件的位置就可以做好自己要求的介面。

我最喜歡的布局~null空布局。根據自己的意願在面板上隨意添加,但是容易造成布局混亂。我將文字框,標籤,和按鈕放在null布局中,標籤和文字框的放置方法和按鈕相同。

 

 

package Swing;import java.awt.FlowLayout;import java.awt.Frame;import javax.swing.JButton;import javax.swing.JFormattedTextField;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JPasswordField;import javax.swing.JTextField;public class FrameDemo extends JFrame {    private JPanel jp;    //聲明面板,面板功能:可添加按鈕文本等,方便管理介面。    private  JButton b1;   //聲明 按鈕    private JButton b2,b3,b4;        private  JLabel lname ,lpwd;    private JTextField txtname;    private JPasswordField txtpwd;     public FrameDemo  ()    {        super("測試視窗");        jp = new JPanel();             jp.setLayout(null);                 b1 = new JButton("登陸");    //對按鈕建立添加        b2 = new JButton("取消");        b1.setBounds(60, 90, 60, 25);        b2.setBounds(125, 90, 60, 25);          jp.add(b1);           jp.add(b2);        lname = new JLabel("使用者名稱");    // 對標籤建立添加        lpwd = new JLabel("密碼");        txtname = new JTextField(20);        txtpwd = new JPasswordField(20);        txtpwd.setEchoChar(‘*‘);                lname.setBounds(30,30,60,25);        txtname.setBounds(95, 30, 120, 25);        lpwd.setBounds(30,60,60,25);        txtpwd.setBounds(95,60,120,25);                jp.add(lname);        jp.add(txtname);        jp.add(lpwd);        jp.add(txtpwd);                this.add(jp);   //按鈕添加到面板,面板添加到界‘        this.setSize(250,170);           this.setLocation(300,300);  //設定視窗左上方座標,視窗開啟時的位置。        this.setResizable(false);//視窗不可改變大小        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    }    public static void main(String[] args) {         FrameDemo frame = new FrameDemo();         frame.setVisible(true);                  }}

 

 

是否會自己按裝一些組件了那?複選框的添加步驟也是一樣的,但是要注意多設定些面板,用以管理各個複選框的標籤等。

 

java Swing圖形化介面

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.