2014-02-27 50 views
0

所以我想創造出一系列單選按鈕,檢查顯示的框,如下所示時,沒有出現:組件添加到面板

  Radio Button 
Check Box 
      Radio Button 
Check Box 
      Radio Button 

不過,我仍然在爲Java學習過程我想知道是否有人可以解決這個問題。此時按鈕和方框顯示在正確的位置,但第一個單選按鈕(「Courier」)由於某種原因未被顯示。如果你也許可以描述一下原因和一個可能的解決方案。

感謝

更新的代碼:

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

import javax.swing.BorderFactory; 
import javax.swing.JCheckBox; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JRadioButton; 

public class Question2 { 
    public static void main(String[] args) { 
     MyFrame f = new MyFrame("Font Chooser"); 
     f.init(); 
    } 
} 

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

    private JPanel mainPanel; 
    private GridBagConstraints gbc = 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); 

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

     JCheckBox cb = new JCheckBox("Bold"); 
     gbLayout.setConstraints(cb, gbc); 
     mainPanel.add(cb); 
     gbc.gridy = 3; 
     gbLayout.setConstraints(cb, gbc); 
     cb = new JCheckBox("Italic"); 
     mainPanel.add(cb); 

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

     JRadioButton rb = new JRadioButton("Times"); 
     gbLayout.setConstraints(rb, gbc); 
     mainPanel.add(rb, gbc); 
     gbc.gridy = 2; 
     rb = new JRadioButton("Helvatica"); 
     mainPanel.add(rb, gbc); 
     rb = new JRadioButton("Courier"); 
     gbc.gridy = 4; 
     mainPanel.add(rb, gbc); 


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

回答

3

3個問題

  • Y座標不會重新分配給不同的值導致最後2個單選按鈕,在同一地點存在
  • GridBagConstraints不用於左側組件
  • setConstraints被錯誤地用來設置限制

由此得到的代碼:

public class GoodGridBagApp { 
    public static void main(String[] args) { 

     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       JFrame frame = new JFrame("Font Chooser"); 
       frame.add(getMainPanel()); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.pack(); 
       frame.setVisible(true); 
      } 

      private JPanel getMainPanel() { 

       JPanel mainPanel = new JPanel(); 
       GridBagConstraints gbc = new GridBagConstraints(); 
       mainPanel.setLayout(new GridBagLayout()); 
       mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 20, 10, 20)); 

       gbc.gridx = 1; 
       gbc.gridy = 2; 

       JCheckBox cb = new JCheckBox("Bold"); 
       mainPanel.add(cb, gbc); 
       gbc.gridy = 4; 
       cb = new JCheckBox("Italic"); 
       mainPanel.add(cb, gbc); 

       gbc.gridx = 2; 
       gbc.gridy = 1; 

       JRadioButton rb = new JRadioButton("Times"); 
       mainPanel.add(rb, gbc); 
       gbc.gridy = 3; 
       rb = new JRadioButton("Helvatica"); 
       mainPanel.add(rb, gbc); 
       rb = new JRadioButton("Courier"); 
       gbc.gridy = 5; 
       mainPanel.add(rb, gbc); 

       return mainPanel; 
      } 
     }); 
    } 
} 

閱讀:How to Use GridBagLayout

+0

謝謝你提供的,愚蠢的錯誤。新問題是「Times」單選按鈕現在與「Italic」複選框處於同一級別。我檢查了他們的y值,他們都是不同的。你能看到其他原因嗎? – user3352349

+0

上面的代碼顯示他們在不同的Y位置,所以這不能從您目前發佈的內容中回答:) – Reimeus

+0

我所有的代碼都在上面。 – user3352349