2013-07-05 44 views
0

我有2個按鈕,一個名爲btnShort,另一個名爲btnLong。我希望它們具有相同的寬度,那麼最好的選擇是什麼?我如何建立2個相同寬度的JButton實例

enter image description here

import java.awt.Dimension; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 


public class TestCode2 { 

public static void main(String[] args) { 

    JFrame window = new JFrame("Test2"); 
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    window.setSize(400, 200); 

    // --------------------------------------------------------------------- 

    GridBagLayout layout = new GridBagLayout(); 
    GridBagConstraints constraints = new GridBagConstraints(); 

    JPanel container = new JPanel(layout); 
    window.add(container); 

    constraints.gridy = 0; 

    JButton btnShort = new JButton("Short"); 
    layout.setConstraints(btnShort, constraints); 
    container.add(btnShort); 

    constraints.gridy = 1; 

    JButton btnLong = new JButton("That's a long button"); 
    layout.setConstraints(btnLong, constraints); 
    container.add(btnLong); 

    //This one won't work because button dimension is not known yet... 
    //btnShort.setPreferredSize(new Dimension(btnLong.getWidth(), btnLong.getHeight())); 

    // --------------------------------------------------------------------- 

    window.setVisible(true); 
} 

} 

回答

1

。利用適當的佈局管理器,像GridBagLayout,它的約束...

enter image description here

import java.awt.BorderLayout; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class Buttons { 

    public static void main(String[] args) { 
     new Buttons(); 
    } 

    public Buttons() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException ex) { 
       } catch (InstantiationException ex) { 
       } catch (IllegalAccessException ex) { 
       } catch (UnsupportedLookAndFeelException ex) { 
       } 

       JFrame frame = new JFrame("Test"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(new TestPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 

     }); 
    } 

    public class TestPane extends JPanel { 

     public TestPane() { 
      setLayout(new GridBagLayout()); 
      GridBagConstraints gbc = new GridBagConstraints(); 
      gbc.gridwidth = GridBagConstraints.REMAINDER; 
      gbc.fill = GridBagConstraints.HORIZONTAL; 

      add(new JButton("I'm a very long button"), gbc); 
      add(new JButton("I'm not"), gbc); 

     } 

     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension(200, 200); 
     } 
    }  
} 
+0

好吧,那很複雜。我沒有看到調用新的'Run()'方法的原因 - 它沒有工作,但我不明白爲什麼按鈕在這種情況下看起來不同! –

+0

invokeLater的原因在[Inital Threads](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html)中有解釋。如果提供weightX約束,則這將更改列的所有組件的大小以佔用指定的空間量。此示例僅使用填充HORIZONTAL約束來請求列中的所有組件填充列 – MadProgrammer

+0

對不起,但我仍然不明白爲什麼**您使用了'invokeLater()'?正如我所說,沒有任何工作正常,但有不同的方面(我不明白的另一件事) –

3

設置GridBagConstraints填充屬性GridBagConstraints.HORIZONTAL這樣既JButton組件佔據寬度相等的容器

constraints.fill = GridBagConstraints.HORIZONTAL; 
+0

+1得到了在第一 – MadProgrammer

+0

謝謝你的這個答案,但它不是非常令人滿意的最長按鈕將它的尺寸也變了 - 我的目標是隻改變一個很短的寬度。 –

+0

在這種情況下,您將不得不額外覆蓋getPreferredSize長按鈕以獲得所需的大小。請參閱更新 – Reimeus

相關問題