java - 更改按钮大小

标签 java image swing jbutton preferredsize

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

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);
    }

最佳答案

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

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

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

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?

关于java - 更改按钮大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19354202/

相关文章:

java - JTextPane 语法突出显示

java - 用于检查 Selenium WebDriver 中项目列表的循环

c# - 图像处理比锁定位更快

java - 在Java中设置进度条

java - 在 JSwing 中一次显示两个列表的组合

java - 需要帮助定位实验室的 Java GUI 组件

java - 是否有一个 DecimalFormat 的行为类似于 Double.toString?

java - 代理服务器的 Axis2 NTLM 身份验证

image - 如何在浏览器选项卡中放置图像/ Logo

html - 隐藏图像背景