2017-04-04 94 views
0

我有一個關於GridBagLayout的問題。我試圖製作一個簡單的計算器,並且我使用GridBagLayout將所有按鈕放在按鈕面板中,儘管它將所有按鈕都放在中間,而不會改變按鈕的大小,這是合乎邏輯的。我可以在GridLayout(它調整按鈕的大小和JPanel的大小)和GridBagLayout(所以我可以把它們放在寬度和順序我想要)之間獲得一些東西嗎?將GridBagLayout調整爲JPanel大小

與佈局的代碼如下:

... BottomPanel(){

this.setLayout(new GridBagLayout()); 
    GridBagConstraints gbc = new GridBagConstraints(); 

    backspace = new JButton("<--"); 
    clean = new JButton("C"); 
    plusminus = new JButton("+/-"); 
    squareroot = new JButton("\u221A"); 
    divide = new JButton("/"); 
    percent = new JButton("%"); 
    multiply = new JButton("*"); 
    fraction = new JButton("1/x"); 
    minus = new JButton("-"); 
    plus = new JButton("+"); 
    dot = new JButton("."); 
    equals = new JButton("="); 
    zero = new JButton("0"); 
    one = new JButton("1"); 
    two = new JButton("2"); 
    three = new JButton("3"); 
    four = new JButton("4"); 
    five = new JButton("5"); 
    six = new JButton("6"); 
    seven = new JButton("7"); 
    eight = new JButton("8"); 
    nine = new JButton("9"); 


    gbc.gridx = 0; 
    gbc.gridy = 0; 
    gbc.gridwidth = 1; 
    gbc.gridheight = 1; 
    this.add(backspace, gbc); 

    gbc.gridx = 1; 
    gbc.gridy = 0; 
    gbc.gridwidth = 2; 
    gbc.gridheight = 1; 
    gbc.fill = GridBagConstraints.HORIZONTAL; 
    this.add(clean, gbc); 

    gbc.gridx = 3; 
    gbc.gridy = 0; 
    gbc.gridwidth = 1; 
    gbc.gridheight = 1; 
    this.add(plusminus, gbc); 

    gbc.gridx = 4; 
    gbc.gridy = 0; 
    gbc.gridwidth = 1; 
    gbc.gridheight = 1; 
    this.add(squareroot, gbc); 

    gbc.gridx = 0; 
    gbc.gridy = 1; 
    gbc.gridwidth = 1; 
    gbc.gridheight = 1; 
    this.add(seven, gbc); 

    gbc.gridx = 1; 
    gbc.gridy = 1; 
    gbc.gridwidth = 1; 
    gbc.gridheight = 1; 
    this.add(eight, gbc);  

    gbc.gridx = 2; 
    gbc.gridy = 1; 
    gbc.gridwidth = 1; 
    gbc.gridheight = 1; 
    this.add(nine, gbc); 

    gbc.gridx = 3; 
    gbc.gridy = 1; 
    gbc.gridwidth = 1; 
    gbc.gridheight = 1; 
    this.add(divide, gbc); 

    gbc.gridx = 4; 
    gbc.gridy = 1; 
    gbc.gridwidth = 1; 
    gbc.gridheight = 1; 
    this.add(percent, gbc); 

    gbc.gridx = 0; 
    gbc.gridy = 2; 
    gbc.gridwidth = 1; 
    gbc.gridheight = 1; 
    this.add(four, gbc); 

    gbc.gridx = 1; 
    gbc.gridy = 2; 
    gbc.gridwidth = 1; 
    gbc.gridheight = 1; 
    this.add(five, gbc); 

    gbc.gridx = 2; 
    gbc.gridy = 2; 
    gbc.gridwidth = 1; 
    gbc.gridheight = 1; 
    this.add(six, gbc); 

    gbc.gridx = 3; 
    gbc.gridy = 2; 
    gbc.gridwidth = 1; 
    gbc.gridheight = 1; 
    this.add(multiply, gbc); 

    gbc.gridx = 4; 
    gbc.gridy = 2; 
    gbc.gridwidth = 1; 
    gbc.gridheight = 1; 
    this.add(fraction, gbc); 

    gbc.gridx = 0; 
    gbc.gridy = 3; 
    gbc.gridwidth = 1; 
    gbc.gridheight = 1; 
    this.add(one, gbc); 

    gbc.gridx = 1; 
    gbc.gridy = 3; 
    gbc.gridwidth = 1; 
    gbc.gridheight = 1; 
    this.add(two, gbc); 

    gbc.gridx = 2; 
    gbc.gridy = 3; 
    gbc.gridwidth = 1; 
    gbc.gridheight = 1; 
    this.add(three, gbc); 

    gbc.gridx = 3; 
    gbc.gridy = 3; 
    gbc.gridwidth = 1; 
    gbc.gridheight = 1; 
    this.add(minus, gbc); 

    gbc.gridx = 4; 
    gbc.gridy = 3; 
    gbc.gridwidth = 1; 
    gbc.gridheight = 2; 
    gbc.fill = GridBagConstraints.VERTICAL; 
    this.add(equals, gbc); 

    gbc.gridx = 0; 
    gbc.gridy = 4; 
    gbc.gridwidth = 2; 
    gbc.gridheight = 1; 
    gbc.fill = GridBagConstraints.HORIZONTAL; 
    this.add(zero, gbc); 

    gbc.gridx = 2; 
    gbc.gridy = 4; 
    gbc.gridwidth = 1; 
    gbc.gridheight = 1; 
    this.add(dot, gbc); 

    gbc.gridx = 3; 
    gbc.gridy = 4; 
    gbc.gridwidth = 1; 
    gbc.gridheight = 1; 
    this.add(plus, gbc); 




} 

,其結果是:

Calculator

謝謝!

+0

也許jgoodies formlayout適合您的需求 – XtremeBaumer

+0

將非零權重分配給您的網格袋約束有幫助嗎? –

回答

0

這裏至少在正確的方向邁出的一步:在你創建GridBagConstraints對象後,設置其權重嚴格大於0:

gbc.weightx = 0.1; 
    gbc.weighty = 0.1; 

這並不重要,其價值你挑,只要大於0.0,不大於1.0。這會導致你的按鈕大部分在水平方向伸展填寫可用空間和甚至垂直間距流傳:

Small panel

Panel dragged large

你可能已經知道,如果你想要的按鈕時長面板確實如此,你可以使用gbc.fill = GridBagConstraints.BOTH;

+0

非常感謝!有誰知道如何「強制」按鈕同時垂直拉伸? 非常感謝! – Dominika

+0

'gbc.fill = GridBagConstraints.BOTH;'或'gbc.fill = GridBagConstraints.VERTICAL;'。 @Dominika –

+0

非常感謝!它現在完美運行!你非常有幫助!謝謝! – Dominika