Java圖形化介面設計——布局管理器之GridBagLayout

來源:互聯網
上載者:User

標籤:not   rri   etl   init   bag   imp   override   height   enter   

GridBagLayout 不會隨著視窗的變化標籤隨之發生變化,可固定。

----------------------------------------------------------
 
import java.awt.Button;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
 
import javax.swing.JFrame;
import javax.swing.JPanel;
 
public class GridBagEx2 extends  JPanel
{
    private static final long serialVersionUID = -5214441555967215113L;
 
    protected void makebutton(String name, GridBagLayout gridbag,
            GridBagConstraints c)
    {
        Button button = new Button(name);
        gridbag.setConstraints(button, c);
        add(button);
    }
 
    public void init()
    {
        GridBagLayout gridbag = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
 
        setFont(new Font("SansSerif", Font.PLAIN, 14));
        setLayout(gridbag);
 
        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1.0;
        makebutton("Button1", gridbag, c);
        makebutton("Button2", gridbag, c);
        makebutton("Button3", gridbag, c);
 
        c.gridwidth = GridBagConstraints.REMAINDER; //end row
        makebutton("Button4", gridbag, c);
 
        c.weightx = 0.0; //reset to the default
        makebutton("Button5", gridbag, c); //another row
 
        c.gridwidth = GridBagConstraints.RELATIVE; //next-to-last in row
        makebutton("Button6", gridbag, c);
 
        c.gridwidth = GridBagConstraints.REMAINDER; //end row
        makebutton("Button7", gridbag, c);
 
        c.gridwidth = 1; //reset to the default
        c.gridheight = 2;
        c.weighty = 1.0;
        makebutton("Button8", gridbag, c);
 
        c.weighty = 0.0; //reset to the default
        c.gridwidth = GridBagConstraints.REMAINDER; //end row
        c.gridheight = 1; //reset to the default
        makebutton("Button9", gridbag, c);
        makebutton("Button10", gridbag, c);
 
        setSize(300, 100);
    }
 
    public static void main(String args[])
    {
        JFrame f = new JFrame("GridBag Layout Example");
        f.setLocation(400, 200);
        GridBagEx2 ex1 = new GridBagEx2();
 
        ex1.init();
 
        f.add("Center", ex1);
        f.pack();
        f.setSize(f.getPreferredSize());
        f.setVisible(true);
        f.addWindowListener(new WindowAdapter()
        {
 
            @Override
            public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            }
             
        });
    }
 
}

Java圖形化介面設計——布局管理器之GridBagLayout

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.