2012-02-27 71 views
3

我是初學者,正在做功課。我的按鈕正在填充gridLayout中的整個單元格。我已經閱讀了關於GridBagLayout的書,但是我的書沒有提及任何關於它的內容,所以我認爲我的當前代碼存在錯誤。老師一直在試圖幫助我,但我們無法弄清楚,所以我想我會在這裏嘗試。任何幫助,將不勝感激!以下是我有:按鈕正在填充GridLayout中的整個單元格,Java

package riddle; 

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

public class Riddle implements ActionListener { 
    private final String LABEL_TEXT = "Why did the chicken cross the road?"; 
    JFrame frame; 
    JPanel contentPane; 
    JLabel label, label1; 
    JButton button; 
    JButton button1; 
    private static int i; 

    public Riddle() { 
     /* Create and set up the frame */ 
     frame = new JFrame(LABEL_TEXT); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     /* Create a content pane with a GridLayout and empty borders */ 
     contentPane = new JPanel(); 
     contentPane.setLayout(new GridLayout(0, 2, 10, 5)); 


     /* Create and add label that is centered and has empty borders */ 
     label = new JLabel("Why did the chicken cross the road?"); 
     label.setAlignmentX(JButton.LEFT_ALIGNMENT); 
     label.setBorder(BorderFactory.createEmptyBorder(20, 50, 20, 50)); 
     contentPane.add(label); 

     label1 = new JLabel(" "); 
     label1.setAlignmentX(JButton.LEFT_ALIGNMENT); 
     label1.setBorder(BorderFactory.createEmptyBorder(20, 50, 20, 50)); 
     contentPane.add(label1); 

     /* Create and add button that is centered */ 
     button = new JButton("Answer"); 
     button.setAlignmentX(JButton.RIGHT_ALIGNMENT); 
     button.setActionCommand("Show Answer"); 
     button.addActionListener(this); 

     contentPane.add(button); 

     /* Add content pane to frame */ 
     frame.setContentPane(contentPane); 

     /* Size and then display the frame */ 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    /** Handle button click action event 
    * pre: 
    * post: clicked button shows answer 
    */ 
    public void actionPerformed(ActionEvent event) { 
     String eventName = event.getActionCommand(); 

     if (eventName.equals("Show Answer")) { 
      label1.setText("To get to the other side. "); 
      button.setText("Answer"); 
      button.setActionCommand("Answer"); 
     } 
    } 

    /** 
    * Create and show the GUI 
    */ 
    private static void runGUI() { 
     JFrame.setDefaultLookAndFeelDecorated(true); 

     Riddle greeting = new Riddle(); 
    } 



    public static void main(String[] args) { 
     javax.swing.SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       runGUI(); 
      } 
     }); 
    } 
} 
+1

另請參閱[這個答案](http://stackoverflow.com/a/7181197/418556)。 – 2012-02-27 19:13:52

回答

3

下面是來自Swing tutorial about GridLayout報價:

一個網格佈局對象放置組件單元網格。每個 組件都佔用其單元中的所有可用空間,並且每個單元格 的大小完全相同。

如果你的書沒有告訴這麼重要的東西,那是一本不好的書,我會建議使用Swing教程。我很驚訝你的老師還沒有告訴你。

如果此行爲不是您想要的行爲,請選擇另一個佈局管理器。 Swing教程有關於all standard ones的指南。

+0

謝謝。是的,我正在使用的是Java編程指南,只是談論行和列。我正在處理的問題甚至說使用GridLayout,但它需要的樣子,使它看起來像它有一個按鈕和單元格之間的餘量。另外,爲了澄清,這不是我的老師,而是老師的助理,我甚至提出這個問題的原因是讓你知道我在嘗試,而不是在沒有做任何研究的情況下在這裏拋出我的問題。無論如何,非常感謝,我的問題非常遺憾:( – Punkrockie 2012-02-27 18:42:35

+0

使用構造函數的hgap和vgap參數來設置marging不是問題,但所有單元格都有相同的hgap和vgap。 – 2012-02-27 19:13:50