Java 更改按钮大小

Java 更改按钮大小,java,image,swing,jbutton,preferredsize,Java,Image,Swing,Jbutton,Preferredsize,我正在尝试更改JFrame中按钮的大小。我看到的每个对这个问题的回答都说使用button.setPreferredSize(新维度(x,y))或button.setSize(新维度(x,y)),但这两种方法都不起作用。按钮标题显示在正确的位置,但按钮占据整个屏幕。我还尝试将按钮添加到框架而不是窗格中。当我这样做时,按钮的大小是正确的,但它在图像之外。如何定位和调整此按钮的大小以使其显示在图像上 public ImageTest(String title, String imageSource,

我正在尝试更改JFrame中按钮的大小。我看到的每个对这个问题的回答都说使用button.setPreferredSize(新维度(x,y))或button.setSize(新维度(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);
    }

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

BorderLayout
将根据多个因素(
中心组件的首选尺寸和外部组件的最小尺寸…如果我没记错的话…)决定“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
是最后添加的组件,框架将使用其首选大小作为计算框架大小的基础(加上框架上可能显示的任何其他组件)

更好的解决方案可能是使用不同的布局管理器


您还应该看看

JLabel、JButton。。。。(在API中实现)水平和垂直对齐当我去掉JButton代码时,其他所有内容都显示在正确的位置,非常好。每个pane.add()后面的frame.pack()似乎解决了这个问题。我将尝试另一种布局管理器,虽然它似乎没有做任何不同的事情。您的布局安排有点奇怪,您应该只有一个组件在屏幕上,覆盖在
面板的顶部。大约24小时前,我才开始与JPanel、JFrame、JLabel等合作,所以我一直在把东西混合在一起,直到它起作用。“我只是把东西混合在一起”扔进一个大锅和一些咒语,你就有了“魔法”。不幸的是,魔法和编码不能混合使用。点击教程,而不是一堆关于如何伪造GUI元素大小(我们不应该猜测它们)的空洞问题。