2012-07-18 221 views
2

请看看下面的代码使所有按钮的大小相同

import javax.swing.*; 
import java.awt.event.*; 
import java.awt.*; 

public class GUI extends JFrame 
{ 
    private JButton open, process; 
    private JLabel center; 
    private JScrollPane scroll; 
    private Box box; 
    private IplImage image; 

    public FirstGUI() 
    { 
     open = new JButton("Open Image"); 
     open.setPreferredSize(new Dimension(70,20)); 
     open.setMaximumSize(new Dimension(100,20)); 

     open.addActionListener(new OpenImageAction()); 
     process = new JButton("Process"); 
     process.setPreferredSize(new Dimension(100,20)); 
     process.setMinimumSize(new Dimension(100,20)); 
     process.addActionListener(new ProcessAction()); 
     System.out.println("Open Size: "+open.getSize()+"  Process size: "+process.getSize()); 


     box = new Box(BoxLayout.Y_AXIS); 
     box.add(open); 
     box.add(process); 

     center = new JLabel(); 
     scroll = new JScrollPane(center); 

     getContentPane().add(box,"West"); 
     getContentPane().add(scroll,"Center"); 

     this.setSize(300,300); 
     this.pack(); 
     this.validate(); 
     this.setVisible(true); 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    } 

    public static void main(String[]args) 
    { 
     try 
     { 
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 

      new GUI(); 
     } 
     catch(java.lang.Exception e) 
     { 
      JOptionPane.showMessageDialog(null,"GUI Error"); 
     } 
    } 

我想在相同尺寸的所有按钮。在这里,第一个比第二个宽。我需要相同的宽度和高度。正如你所看到的,我已经使用了setPrefferedSize(),setMaximumSize(),setMinimumSize()等所有可用的方法,但它仍然无法正常工作。请帮忙!

+0

如果帧大小调整,您会发生什么?这些按钮是否应该在西部面板中调整大小,还是应该保持相同的大小并保持不变:顶部,中部,底部? – 2012-07-18 07:23:37

+0

按钮保持在相同的大小,无论是否rezied。当框架没有时,按钮不需要调整大小。 那就是我需要的。但是,嘿,我喜欢学习,调整大小的部分:) – 2012-07-18 07:26:14

回答

9

以下是使用GridLayout实现此目的的一种方法。我还介绍了一个额外的JPanel,以便在调整JFrame的大小时不会拉伸按钮,并且我选择了GridBagLayout,以便它将垂直居中按钮面板。肯定有其他方法可以解决您的问题。

你应该尽量避免的一件事是强制首选/最大/最小尺寸。委托给L & F和LayoutMananager。

如果您在JFrame称之为pack(),是没用的,以前设置其大小,pack()会改变反正。尝试拨打setVisible(true);作为GUI初始化的最后一行。

如果您想正确理解布局,定位,尺寸等是如何在Swing中工作的,我强烈建议您阅读the tutorial on LayoutManager's

import java.awt.BorderLayout; 
import java.awt.GridBagLayout; 
import java.awt.GridLayout; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JOptionPane; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.SwingUtilities; 
import javax.swing.UIManager; 

public class GUI extends JFrame { 
    private JButton open, process; 
    private JLabel center; 
    private JScrollPane scroll; 
    private JPanel box; 

    public GUI() { 
     open = new JButton("Open Image"); 
     // open.addActionListener(new OpenImageAction()); 
     process = new JButton("Process"); 
     // process.addActionListener(new ProcessAction()); 

     box = new JPanel(new GridLayout(2, 1)); 
     box.add(open); 
     box.add(process); 
     JPanel west = new JPanel(new GridBagLayout()); 
     west.add(box); 

     center = new JLabel("Some center label"); 
     scroll = new JScrollPane(center); 

     getContentPane().add(west, BorderLayout.WEST); 
     getContentPane().add(scroll); 

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

    public static void main(String[] args) { 
     try { 
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
     } catch (java.lang.Exception e) { 
      JOptionPane.showMessageDialog(null, "GUI Error"); 
     } 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       new GUI(); 
      } 
     }); 
    } 
} 
+0

+1的布局经理的意见。 – asgs 2012-07-18 07:36:45

+0

@Yohan:请注意,单词_size_既不出现在GUI中,也出现在['ButtonTest'](http://stackoverflow.com/a/3420431/230513)中。另请参阅本文[问与答](http://stackoverflow.com/q/7229226/230513)。 – trashgod 2012-07-18 19:26:29

+0

非常感谢您的回答。非常感谢:) – 2012-07-22 16:51:22

-2

也许是因为尺寸与setPreferredSize()不一样?

open.setPreferredSize(new Dimension(70,20)); 

    process.setPreferredSize(new Dimension(100,20)); 
-1
  1. 设置相同尺寸的两个按钮。
  2. 设置两个按钮的最大和最小尺寸。现在只有第一个和第二个最小值。 它会工作。
+4

*“它会工作......”* ..在你的电脑上,然后打破下一个。 – 2012-07-18 08:13:02

+0

即使你的答案是100%错误,我感谢你的回答... – 2012-07-22 16:56:58

1

您可以创建自己的布局管理器来计算所有按钮的最大尺寸(可能来自首选尺寸值)并基本上指定相同尺寸(通过setSize)。当然,你必须提供位置信息以及,但是这给提供垂直&横向布局选项

UPDATE

这是我使用的实施,我拿不出它的信用优势,你可以看到它是由Santhosh Kumar撰写的。我觉得它非常有用,因为它不仅将所有按钮的大小设置为相同,而且还提供了对齐方式。您可以在这里找到原始文章http://www.jroller.com/santhosh/entry/how_do_you_layout_command

/** 
* @author Santhosh Kumar - [email protected] 
*/ 
public class ButtonLayout implements LayoutManager, SwingConstants { 

    protected static Logger logger = Logger.getLogger(ButtonLayout.class); 

    private int gap; 
    private int alignment; 

    public ButtonLayout() { 

     setAlignment(RIGHT); 
     setGap(2); 

    } 

    public ButtonLayout(int alignment, int gap) { 
     setGap(gap); 
     setAlignment(alignment); 
    } 

    public ButtonLayout(int gap) { 
     this(RIGHT, gap); 
    } 

    public int getAlignment() { 
     return alignment; 
    } 

    public void setAlignment(int alignment) { 
     this.alignment = alignment; 
    } 

    public int getGap() { 
     return gap; 
    } 

    public void setGap(int gap) { 
     this.gap = gap; 
    } 

    private Dimension[] dimensions(Component children[]) { 
     int maxWidth = 0; 
     int maxHeight = 0; 
     int visibleCount = 0; 
     Dimension componentPreferredSize; 

     for (int i = 0, c = children.length; i < c; i++) { 

      if (children[i].isVisible()) { 

       componentPreferredSize = children[i].getPreferredSize(); 
       maxWidth = Math.max(maxWidth, componentPreferredSize.width); 
       maxHeight = Math.max(maxHeight, componentPreferredSize.height); 
       visibleCount++; 

      } 

     } 

     int usedWidth = 0; 
     int usedHeight = 0; 

     switch (alignment) { 

      case LEFT: 
      case RIGHT: 
       usedWidth = maxWidth * visibleCount + gap * (visibleCount - 1); 
       usedHeight = maxHeight; 
       break; 

      case TOP: 
      case BOTTOM: 
       usedWidth = maxWidth; 
       usedHeight = maxHeight * visibleCount + gap * (visibleCount - 1); 
       break; 

     } 

     return new Dimension[]{ 
          new Dimension(maxWidth, maxHeight), 
          new Dimension(usedWidth, usedHeight),}; 
    } 

    public void layoutContainer(Container container) { 

     Insets insets = container.getInsets(); 
     int width = container.getWidth() - (insets.left + insets.right); 
     int height = container.getHeight() - (insets.top + insets.bottom); 

     Component[] children = container.getComponents(); 
     Dimension dim[] = dimensions(children); 

     int maxWidth = dim[0].width; 
     int maxHeight = dim[0].height; 
     int usedWidth = dim[1].width; 
     int usedHeight = dim[1].height; 

     for (int i = 0, c = children.length; i < c; i++) { 

      if (children[i].isVisible()) { 

       switch (alignment) { 
        case LEFT: 
         children[i].setBounds(
             insets.left + (maxWidth + gap) * i, 
             insets.top, 
             maxWidth, 
             maxHeight); 
         break; 
        case TOP: 
         children[i].setBounds(
             insets.left + ((width - maxWidth)/2), 
             insets.top + (maxHeight + gap) * i, 
             maxWidth, 
             maxHeight); 
         break; 
        case RIGHT: 
         children[i].setBounds(
             width - insets.right - usedWidth + (maxWidth + gap) * i, 
             insets.top, 
             maxWidth, 
             maxHeight); 
         break; 
        case BOTTOM: 
         children[i].setBounds(
             insets.left + (maxWidth + gap) * i, 
             height - insets.bottom - usedHeight + (maxHeight + gap) * i, 
//          insets.top, 
             maxWidth, 
             maxHeight); 
         break; 
       } 

      } 

     } 

    } 

    public Dimension minimumLayoutSize(Container c) { 
     return preferredLayoutSize(c); 
    } 

    public Dimension preferredLayoutSize(Container container) { 

     Insets insets = container.getInsets(); 

     Component[] children = container.getComponents(); 
     Dimension dim[] = dimensions(children); 

     int usedWidth = dim[1].width; 
     int usedHeight = dim[1].height; 

     return new Dimension(
         insets.left + usedWidth + insets.right, 
         insets.top + usedHeight + insets.bottom); 
    } 

    public void addLayoutComponent(String string, Component comp) { 
    } 

    public void removeLayoutComponent(Component c) { 
    } 

} 
+0

感谢您的回复。非常感激 :) – 2012-07-22 16:53:22

相关问题