2012-02-27 16 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)も参照してください。 –

答えて

3

ここSwing tutorial about GridLayoutからの引用です:

AのGridLayoutオブジェクトは、セルのグリッドにコンポーネントを配置します。各 コンポーネントは、そのセル内のすべての使用可能な領域を取り、各セル はまったく同じサイズです。

あなたの本がそのような重要なことを伝えていない場合、それは悪い本です。代わりにSwingチュートリアルを使用することをお勧めします。私はあなたの先生があなたにそれを伝えることができなかったことに驚いています。

この動作が目的の動作でない場合は、別のレイアウトマネージャを選択してください。 Swingチュートリアルにはall standard onesのガイドがあります。

+0

ありがとうございます。うん、私が使っている本はJavaプログラミングの手引きで、行と列について話しています。私が取り組んでいる問題は、GridLayoutを使用すると言っていますが、どのような外観にする必要があるかの写真は、ボタンとセルの間にマージンがあるように見えます。また、私の先生ではなく先生の助手だったので、私がそれを持ち上げた理由は、私が試していたことを皆に聞かせて、最初に何の研究もせずにここに質問を投げ捨てるのではないということでした。とにかく、ありがとうと私の質問はあまりにも不満だった申し訳ありません:( – Punkrockie

+0

マーキングを設定することは、コンストラクタのhgap引数とvgap引数を使用して問題はありませんが、すべてのセルは同じhgapとvgapを共有します。 –

関連する問題