2014-02-27 81 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