【问题标题】:How can I align RadioButtons in a JSplitPane?如何在 JSplitPane 中对齐 RadioButtons?
【发布时间】:2012-11-09 19:40:20
【问题描述】:

我正在尝试在此处对齐我的程序中的 RadioButtons。有人可以帮助我吗?我不确定该怎么做。我在这里和其他地方搜索过,似乎找不到适用的例子。这就是我相信我的代码的相关部分的内容。如果需要更多,我会编辑。

final class SplitPanel extends JFrame {
    private FlowLayout flowLayout = new FlowLayout();
    private GridLayout gridLayout = new GridLayout(4, 1);
    private DiagLayout diagLayout = new DiagLayout();

    private JRadioButton jrbFlowLayout = new JRadioButton("Horizontal");
    private JRadioButton jrbGridLayout = new JRadioButton("Verticle");
    private JRadioButton jrbDiagLayout = new JRadioButton("Diagonal");

    private JButton jbt1 = new JButton("Button 1");
    private JButton jbt2 = new JButton("Button 2");
    private JButton jbt3 = new JButton("Button 3");
    private JButton jbt4 = new JButton("Button 4");

    private JSplitPane jSplitPane;
    private JPanel jPanel1, jPanel2;

    public SplitPanel() {
        this.setTitle("Split Panel with Diagonalization");
        this.setSize(600, 400);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        createPanel1();
        createPanel2();

        jSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
                jPanel1, jPanel2);
        jSplitPane.setOneTouchExpandable(true);
        jSplitPane.setContinuousLayout(true);
        jSplitPane.setDividerLocation(150);

        getContentPane().add(jSplitPane);
    }

    public void createPanel1() {
        jPanel1 = new JPanel();
        jPanel1.setBorder(new TitledBorder("Select a Layout Manger"));
        jPanel1.add(jrbFlowLayout);
        jPanel1.add(jrbGridLayout);
        jPanel1.add(jrbDiagLayout);

        ButtonGroup buttonGroup1 = new ButtonGroup();
        buttonGroup1.add(jrbFlowLayout);
        buttonGroup1.add(jrbGridLayout);
        buttonGroup1.add(jrbDiagLayout);

    }

    public void createPanel2() {
        jPanel2 = new JPanel();
        jPanel2.setLayout(diagLayout);
        jPanel2.add(jbt1);
        jPanel2.add(jbt2);
        jPanel2.add(jbt3);
        jPanel2.add(jbt4);

        jrbFlowLayout.addActionListener (new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jPanel2.setLayout(flowLayout);
                jPanel2.validate();
            }
        });

        jrbGridLayout.addActionListener (new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jPanel2.setLayout(gridLayout);
                jPanel2.validate();
            }
        });

        jrbDiagLayout.addActionListener (new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jPanel2.setLayout(diagLayout);
                jPanel2.validate();
            }
        });

    }

感谢您的帮助!

【问题讨论】:

    标签: java swing layout-manager jradiobutton jsplitpane


    【解决方案1】:

    为您的 jPanel1 尝试不同的布局管理器

    public void createPanel1() {
        jPanel1 = new JPanel();
        //jPanel1.setLayout(new GridLayout(0, 1);
        jPanel1.setLayout(new BoxLayout(jPanel1, BoxLayout.Y_AXIS));
        jPanel1.setBorder(new TitledBorder("Select a Layout Manger"));
        jPanel1.add(jrbFlowLayout);
        jPanel1.add(jrbGridLayout);
        jPanel1.add(jrbDiagLayout);
    
        ButtonGroup buttonGroup1 = new ButtonGroup();
        buttonGroup1.add(jrbFlowLayout);
        buttonGroup1.add(jrbGridLayout);
        buttonGroup1.add(jrbDiagLayout);
    
    }
    

    GridLayout...

    BoxLayout...

    我几乎建议GridBagLayout,但在这种情况下,这可能有点矫枉过正......

    【讨论】:

      【解决方案2】:

      JPanel 的默认布局是 FlowLayout。 http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html

      您可能希望将面板布局设置为 GridLayout 甚至是 BoxLayout。

      createPanel1(){
      // create panel.
      jPanel1.setLayout(new GridLayout(0,1));
      

      【讨论】:

      • 甜蜜!非常感谢。比我想象的要简单得多!
      猜你喜欢
      • 2019-09-02
      • 2019-01-02
      • 2012-08-29
      • 2010-12-31
      • 1970-01-01
      • 2021-05-22
      • 2020-10-15
      • 2016-05-06
      • 2012-03-04
      相关资源
      最近更新 更多