2013-10-14 323 views
0

我试图改变我的JFrame中按钮的大小。我看到这个问题的每一个回应都说使用button.setPreferredSize(new Dimension(x,y))或button.setSize(new Dimension(x,y)),但这些都不起作用。按钮标题显示在正确的位置,但该按钮占据整个屏幕。我也尝试将按钮添加到框架而不是窗格。当我这样做时,按钮是正确的大小,但它是在图像之外。如何定位和调整此按钮的大小以使其显示在图像上?更改按钮大小

public ImageTest(String title, String imageSource, int minimum, int middle, int maximum, int curr, boolean x) { // Extends JFrame... 
     frame = new JFrame(); 
     frame.setTitle(title); 
     frame.setLayout(new GridBagLayout()); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     maxim = maximum; 
     minim = minimum; 
     mercSize(minim, maxim, curr); //change size of mercury in thermometer 

     pane = new PaintPane(new ImageIcon(imageSource).getImage()); 
     pane.setLayout(new BorderLayout()); 
     frame.add(pane); 

     b1 = new JButton("Increase Temp"); 
     b1.setPreferredSize(new Dimension(100, 100)); //Button does not work yet 
     b1.setBorder(BorderFactory.createEmptyBorder(560,250,0,0)); 
     b1.setLocation(95, 45); 
     pane.add(b1); 
     frame.pack(); 

     min = new JLabel("" + minimum); 
     min.setFont(min.getFont().deriveFont(Font.BOLD, 28)); 
     if(x == true) min.setForeground(Color.BLACK); //Changes the font color to match the image 
     else min.setForeground(Color.WHITE); 
     min.setVerticalAlignment(JLabel.TOP); 
     min.setVerticalTextPosition(JLabel.TOP); 
     min.setBorder(BorderFactory.createEmptyBorder(445, 150, 0, 0)); //Positions the text 
     pane.add(min); 
     frame.pack(); 

     mid = new JLabel("" + middle); 
     mid.setFont(mid.getFont().deriveFont(Font.BOLD, 28)); 
     if(x == true) mid.setForeground(Color.BLACK); //Changes the font color to match the image 
     else mid.setForeground(Color.WHITE); 
     mid.setVerticalAlignment(JLabel.TOP); 
     mid.setVerticalTextPosition(JLabel.TOP); 
     mid.setBorder(BorderFactory.createEmptyBorder(240, 150, 0, 0)); //Positions the text 
     pane.add(mid); 
     frame.pack(); 

     max = new JLabel("" + maximum); 
     max.setFont(max.getFont().deriveFont(Font.BOLD, 28)); 
     if(x == true) max.setForeground(Color.BLACK); //Changes the font color to match the image 
     else max.setForeground(Color.WHITE); 
     max.setVerticalAlignment(JLabel.TOP); 
     max.setVerticalTextPosition(JLabel.TOP); 
     max.setBorder(BorderFactory.createEmptyBorder(35, 150, 0, 0)); //Positions the text 
     pane.add(max); 
     frame.pack(); 

     temp = new JLabel("Current Temperature: " + curr); 
     temp.setFont(temp.getFont().deriveFont(Font.BOLD, 28)); 
     if(x == true) temp.setForeground(Color.BLACK); //Changes the font color to match the image 
     else temp.setForeground(Color.WHITE); 
     temp.setVerticalAlignment(JLabel.TOP); 
     temp.setVerticalTextPosition(JLabel.TOP); 
     temp.setBorder(BorderFactory.createEmptyBorder(560, 120, 0, 0)); //Positions the text 
     pane.add(temp); 
     frame.pack(); 

     error = new JLabel(); 

     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 
+0

的JLabel,JButton的....已(在API实现)水平和垂直对齐 – mKorbel

回答

2

您有一系列复合问题。基本上,您将PaintPane的布局管理器设置为BorderLayout,然后尝试向其添加一组组件。

BorderLayout将根据多种因素(如组件的首选大小和外部组件的最小大小,如果我没有记错的话,将如何最好地“认为”内容应该布置的决定做出决定。 ..)

让我们来仔细看看...

public ImageTest(String title, String imageSource, int minimum, int middle, int maximum, int curr, boolean x) { // Extends JFrame... 
    //... 
    pane = new PaintPane(new ImageIcon(imageSource).getImage()); 
    pane.setLayout(new BorderLayout()); 
    // Add button to center position 
    pane.add(b1); 
    // Add min to center position, overwriting b1 
    pane.add(min); 
    // Add mid to center position, overwriting 
    pane.add(mid); 
    // Add max to center position, overwriting mid 
    pane.add(max); 
    // Add temp to center position, overwriting max 
    pane.add(temp); 
    frame.pack(); 
    //... 
} 

每次添加新的组件到PaintPane,你是,有效地去除什么都在那里之前(它仍然在容器上它只是不会显示)。这是因为BorderLayout将永远只允许单个组件出现在它的5个布局位置中的每一个。默认位置始终为CENTER

这意味着temp是最后添加的组件,帧将使用它的首选大小作为计算帧大小的基础(以及可能在帧上可显示的任何其他组件)

更好的解决办法可能是使用不同的布局管理器...

你也应该看看Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?

+0

当我拿走了JButton代码,其他所有东西都显示在正确的位置,非常好。每个pane.add()后面的frame.pack()似乎都照顾到了这一点。我会尝试一个不同的布局管理器,虽然 – Radi0actvChickn

+0

试着在'frame.setVisible(true)'之前或之后添加一个'pane.revalidate' ... – MadProgrammer

+0

这似乎没有做任何不同 – Radi0actvChickn