0

I'm trying to change the size of the button in my JFrame. Every response I see to this question says to use button.setPreferredSize(new Dimension(x, y)) or button.setSize(new Dimension(x, y)), but neither of these work. The button title shows up in the right spot, but the button takes up the whole screen. I also tried adding the button to the frame instead of the pane. When I do that, the button is the correct size, but it is outside of the image. How to I position and size this button to get it appear over the image?

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);
    }
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Radi0actvChickn
  • 47
  • 1
  • 10

1 Answers1

2

You have a series of compound issues. Basically, you set the layout manager of the PaintPane to BorderLayout, then try and add a bunch of components to it.

The BorderLayout will make decisions about how best "it" thinks content should be laid out, based on a number of factors (preferred size of the CENTER component and minimum size for the outer components...if I remember correctly...)

Let's take a closer look...

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();
    //...
}

Each time you add a new component to the PaintPane, you are, effectively, removing what ever was there before (it's still on the container it just won't be displayed). This is because BorderLayout will only ever allow a single component to appear at each of it's 5 layout locations. The default position is always CENTER

This means the temp is the last component added, the frame will use it's preferred size as a basis for calculating the size of the frame (plus any other components that might be displayable on the frame)

A better solution might be to use a different layout manager...

You should also take a look at Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • When I take away the JButton code, everything else shows up in the right places, perfectly fine. The frame.pack() after each pane.add() seemed to take care of that. I'll try a different layout manager though – Radi0actvChickn Oct 14 '13 at 06:18
  • That didn't seem to do anything different – Radi0actvChickn Oct 14 '13 at 06:24
  • There's something weird about your layout arrangement, you should only have a single component on the screen, overlaid on top of your `panel` – MadProgrammer Oct 14 '13 at 06:25
  • I only started working with JPanel, JFrame, JLabel, etc. about 24 hours ago, so I've just been mixing things together until it works. – Radi0actvChickn Oct 14 '13 at 06:30
  • *"I've just been mixing things together"* Throw in a cauldron and a few incantations and you have 'magic'. Unfortunately magic and coding don't mix. Hit the tutorials, rather than a bunch of inane questions on how to fudge the size (we should not be guessing them) of GUI elements. – Andrew Thompson Oct 14 '13 at 07:10
  • Thanks for taking that so literally and for the "helpful" comment..... I posted this to see if there was just a few lines that were messed up, but it seems like a bit more than that. So yeah, I'm reading tutorials. – Radi0actvChickn Oct 14 '13 at 07:36