更改按钮大小

时间:2013-10-14 05:53:30

标签: java image swing jbutton preferredsize

我正在尝试更改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);
    }

1 个答案:

答案 0 :(得分:2)

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

BorderLayout将基于多种因素(CENTER组件的首选大小和外部组件的最小大小)来决定最佳“it”认为内容应该如何布局。 ..如果我没记错的话......)

让我们仔细看看......

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?

相关问题