我的一个工具栏用JtoolBar,垂直方向。
上面排列的多个按钮JToggleButton总是随上面文字的长短而宽度不一。怎么解决,我希望宽度是一样的

解决方案 »

  1.   

    把字符的对齐方式设置为 center 试下,呵呵!
      

  2.   

    JToolBar上面设一下GridLayout就可以了,代码如下^o^:package com.ddpie.frame;import java.awt.FlowLayout;
    import java.awt.GridLayout;import javax.swing.JFrame;
    import javax.swing.JToggleButton;
    import javax.swing.JToolBar;public class TestJToolBar {
      public static void main(String[] args){
        JFrame frame = new JFrame();
        frame.setSize(300,200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new FlowLayout());
        JToolBar toolBar = new JToolBar(JToolBar.VERTICAL);
        frame.getContentPane().add(toolBar);
        toolBar.setLayout(new GridLayout(3,1));//这里是关键
        JToggleButton button1 = new JToggleButton("Clicked");
        JToggleButton button2 = new JToggleButton("Clicked123");
        JToggleButton button3 = new JToggleButton("Clicked1234567");
        toolBar.add(button1);
        toolBar.add(button2);
        toolBar.add(button3);
        frame.setVisible(true);
      }
    }