2014-02-25 39 views
1

我是相當新的Java和我有一個問題,試圖把一個以上的按鈕, 此刻我'在面板中添加兩個按鈕,但我不知道如何分離它們的x位置,即它們都是直接添加到對方。Java的GUI:我如何有一個使用GridBagLayout的行上的多個按鈕

我需要創建一個新的佈局,還是可以使用我現在擁有的解決方案找到解決方案?

public class Question1 { 
    public static void main (String[] args){ 
     MyFrame f = new MyFrame("Simple Submit Cancel Form"); 
     f.init(); 
    } 
} 

class MyFrame extends JFrame{ 
    MyFrame(String title){ 
    super(title); 
    } 

    private JPanel mainPanel; 
    private GridBagConstraints cText = new GridBagConstraints(); 
    private GridBagConstraints cButton = new GridBagConstraints(); 
    private GridBagLayout gbLayout = new GridBagLayout(); 

    void init(){ 
     mainPanel = new JPanel(); 
     mainPanel.setLayout(gbLayout); 
     mainPanel.setBorder(BorderFactory.createEmptyBorder(10,20,10,20)); 
     this.setContentPane(mainPanel); 

     cText.anchor = GridBagConstraints.WEST; 
     cText.weightx = 0.0; 
     cText.gridx = 0; 
     cText.gridy = 0; 

     JTextField tf = new JTextField(20); 
     gbLayout.setConstraints(tf,cText); 
     mainPanel.add(tf); 

     cButton.gridwidth = 4; 
     cButton.weightx = 0.0; 
     cButton.gridx = 0; 
     cButton.gridy = 1; 

     JButton b = new JButton("Submit"); 
     gbLayout.setConstraints(b,cButton); 
     mainPanel.add(b); 
     b = new JButton("Cancel"); 
     gbLayout.setConstraints(b,cButton); 
     mainPanel.add(b); 



     this.pack(); 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ; 
     this.setVisible(true); 
    } 

回答

3

只是遞增的gridx值:

JButton b = new JButton("Submit"); 
    // gbLayout.setConstraints(b,cButton); 
    mainPanel.add(b, cButton); 
    b = new JButton("Cancel"); 
    cButton.gridx++; 
    // gbLayout.setConstraints(b,cButton); 
    mainPanel.add(b, cButton); 

另外你需要使用用容器加入您的組件到網格包佈局時創建的約束。

例如,

import java.awt.*; 
import javax.swing.*; 

public class Question1 { 
    public static void main(String[] args) { 
     MyFrame f = new MyFrame("Simple Submit Cancel Form"); 
     f.init(); 
    } 
} 

class MyFrame extends JFrame { 
    private static final int GAP = 2; 

    MyFrame(String title) { 
     super(title); 
    } 

    private JPanel mainPanel; 
    private GridBagLayout gbLayout = new GridBagLayout(); 

    void init() { 
     mainPanel = new JPanel(); 
     mainPanel.setLayout(gbLayout); 
     mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 20, 10, 20)); 
     this.setContentPane(mainPanel); 

     GridBagConstraints gbc = new GridBagConstraints(); 
     gbc.anchor = GridBagConstraints.WEST; 
     gbc.fill = GridBagConstraints.HORIZONTAL; 
     gbc.insets = new Insets(GAP, GAP, GAP, GAP); 
     gbc.gridwidth = 2; 
     gbc.weightx = 1.0; 
     gbc.weighty = 1.0; 
     gbc.gridx = 0; 
     gbc.gridy = 0; 

     JTextField tf = new JTextField(20); 

     mainPanel.add(tf, gbc); 

     gbc.gridwidth = 1; 
     gbc.gridx = 0; 
     gbc.gridy = 1; 

     JButton b = new JButton("Submit"); 
     mainPanel.add(b, gbc); 
     b = new JButton("Cancel"); 
     gbc.gridx++; 
     mainPanel.add(b, gbc); 

     this.pack(); 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     this.setVisible(true); 
    } 
} 
+0

感謝您的快速響應,我想知道如果按鈕可以在同一行(即相同的y值)? – user3352349

+0

@ user3352349:哎呀,你說得對。這是需要增加的x值。 –

+0

@ user3352349:在添加組件時也使用約束。閱讀Google將幫助您找到的教程。 –

1

試試下面的代碼:

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

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

public class Question1 { 
    public static void main(String[] args) { 
     MyFrame f = new MyFrame("Simple Submit Cancel Form"); 
     f.init(); 
    } 
} 

class MyFrame extends JFrame { 
    MyFrame(String title) { 
     super(title); 
    } 

    private JPanel mainPanel; 
    private GridBagConstraints cText = new GridBagConstraints(); 
    private GridBagConstraints cButton = new GridBagConstraints(); 
    private GridBagLayout gbLayout = new GridBagLayout(); 

    void init() { 
     mainPanel = new JPanel(); 
     mainPanel.setLayout(gbLayout); 
     mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 20, 10, 20)); 
     this.setContentPane(mainPanel); 

     cText.anchor = GridBagConstraints.WEST; 
     cText.weightx = 0.0; 
     cText.gridx = 0; 
     cText.gridy = 0; 

     JTextField tf = new JTextField(20); 
     gbLayout.setConstraints(tf, cText); 
     mainPanel.add(tf); 

     cButton.gridwidth = 4; 
     cButton.weightx = 0.0; 
     cButton.gridx = 0; 
     cButton.gridy = 1; 

     JPanel demoPanel = new JPanel(); 

     JButton b = new JButton("Submit"); 
     gbLayout.setConstraints(demoPanel, cButton); 
     demoPanel.add(b); 
     b = new JButton("Cancel"); 
     // gbLayout.setConstraints(b,cButton); 
     demoPanel.add(b); 

     mainPanel.add(demoPanel); 

     this.pack(); 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     this.setVisible(true); 
    } 
} 

我剛纔把兩個按鈕,一個JPanel裏面,把JPanel中的GridBagLayout面板裏面!

+0

非常感謝! – user3352349

+0

接受它作爲答案,如果它幫助你:) – ItachiUchiha

+0

不是一個完美的解決方案...但暫時解決您的問題。仍然不推薦。 –

相關問題