Java GUI,尝试定位单选按钮和复选框

时间:2014-02-27 17:39:30

标签: java user-interface checkbox radio-button gridbaglayout

所以我正在尝试创建一系列单选按钮和复选框,显示如下:

          Radio Button
Check Box
          Radio Button
Check Box
          Radio Button

但是,我还在学习java的过程中,我想知道是否有人能解决这个问题。目前按钮和方框显示在正确的位置,但是由于某种原因没有显示第一个单选按钮(“时间”)。如果您可以描述原因和可能的解决方案,那将是伟大的。

由于

更新代码:

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

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

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;
        gbLayout.setConstraints(rb, gbc);
        rb = new JRadioButton("Helvatica");
        mainPanel.add(rb, gbc);
        gbc.gridy = 4;
        gbLayout.setConstraints(rb, gbc);
        rb = new JRadioButton("Courier");
        mainPanel.add(rb, gbc);


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

2 个答案:

答案 0 :(得分:1)

这就是问题所在,你说每个高度都是3,但实际上每个单元都是1。

cRadioButton.gridheight = 3; // change this to 1

这是完整的源代码,我确实从另一个答案中做了一些建议的更改,因为在某些时候你会想要做一些不同的事情(每种类型的按钮都有不同的动作监听器实现)。

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

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

public class MyFrame1 extends JFrame {
    MyFrame1(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 italic = new JCheckBox("Italic");
        gbLayout.setConstraints(italic, gbc);
        mainPanel.add(italic);

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

        gbc.gridx = 1;
        gbc.gridy = 0;
        JRadioButton times = new JRadioButton("Times");
        gbLayout.setConstraints(times, gbc);
        mainPanel.add(times, gbc);

        gbc.gridy = 2;
        JRadioButton helv = new JRadioButton("Helvatica");
        gbLayout.setConstraints(helv, gbc);
        mainPanel.add(helv, gbc);

        gbc.gridy = 4;
        JRadioButton courier = new JRadioButton("Courier");
        gbLayout.setConstraints(courier, gbc);
        mainPanel.add(courier, gbc);


        this.pack();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }
    public static void main(String[] args) {
        MyFrame1 f = new MyFrame1("Font Chooser");
        f.init();
    }
}

答案 1 :(得分:0)

似乎你不断重新分配同一个对象,这可能会导致你的重叠。而不是

JRadioButton rb = new JRadioButton("Times");
//...
newPanel.add(rb);
rb = new JRadioButton("Helvatica");
//...
newPanel.add(rb);
//and so on

尝试类似

的内容
JRadioButton times = new JRadioButton("Times");
JRadioButton helva = new JRadioButton("Helvatica");
//...

newPanel.add(times);
newPanel.add(helva);