0

I am trying to create a game about war. I am starting off with just creating a single window. I am using swing to create windows. However, when the user tries to resize the JFrame, the elements don't move with the frame. I am resizing the buttons that I use, as well as my JPanel. My code can be seen below:

Main.java:

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

import Numbers.Numbers;

public class Main implements ActionListener {

  static JFrame frame;

  static JPanel titlePane;
  static JButton playButton;
  static JButton explanationButton;

  static JPanel explanationPane;
  static JButton nextButton;
  static JLabel explanationLabel;
  static JButton backButton;

  static int frameSize = 500;

  public Main(boolean run) {

    if (run) {

      frame = new JFrame("War");

      titlePane = new JPanel(null);
      playButton = new JButton("PLAY");
      explanationButton = new JButton("HOW TO PLAY");

      explanationPane = new JPanel(null);
      nextButton = new JButton("NEXT");
      backButton = new JButton("BACK");

      titlePane.setSize(frameSize, frameSize);
      titlePane.add(playButton);
      playButton.setBounds(Numbers.doubleToInt(frameSize/2.5), Numbers.doubleToInt(frameSize/2.5), Numbers.doubleToInt(frameSize/5), Numbers.doubleToInt(frameSize/(100/3)));
      playButton.setBackground(Color.GRAY);
      playButton.setForeground(Color.WHITE);
      playButton.setVisible(true);
      explanationButton.setBounds(Numbers.doubleToInt(frameSize/2.5), Numbers.doubleToInt(frameSize/(25/11)), Numbers.doubleToInt(frameSize/5), Numbers.doubleToInt(frameSize/(100/3)));
      explanationButton.setBackground(Color.GRAY);
      explanationButton.setForeground(Color.WHITE);
      explanationButton.setVisible(true);

      explanationPane.setSize(frameSize, frameSize);
      explanationPane.add(nextButton);
      nextButton.setBounds(225, 50, 50, 15);
      nextButton.setBackground(Color.GRAY);
      nextButton.setForeground(Color.WHITE);
      nextButton.setVisible(true);
      backButton.setBounds(225, 450, 50, 15);
      backButton.setBackground(Color.GRAY);
      backButton.setForeground(Color.WHITE);
      backButton.setVisible(true);

      frame.setSize(frameSize, frameSize);
      frame.add(titlePane);
      frame.add(explanationPane);
      titlePane.setVisible(false);
      explanationPane.setVisible(false);
      frame.setVisible(true);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

  }

  public void switchPane(String pane) {

    switch (pane) {

      case "title":
        titlePane.setVisible(true);
        explanationPane.setVisible(false);
        break;

      case "explanation":
        titlePane.setVisible(false);
        explanationPane.setVisible(true);
        break;

    }

  }

  @Override
  public void actionPerformed(ActionEvent event) {

  }

  public static void main(String[] args) {

    Main war = new Main(true);
    war.switchPane("title");

    while (true) {

      if (frame.getX() != frameSize) {

        frameSize = frame.getX();

      }

      else if (frame.getY() != frameSize) {

        frameSize = frame.getY();

      }

      frame.setSize(frameSize, frameSize);
      titlePane.setSize(frameSize, frameSize);
      explanationPane.setSize(frameSize, frameSize);
      playButton.setBounds(Numbers.doubleToInt(frameSize/2.5), Numbers.doubleToInt(frameSize/2.5), Numbers.doubleToInt(frameSize/5), Numbers.doubleToInt(frameSize/(100/3)));
      explanationButton.setBounds(Numbers.doubleToInt(frameSize/2.5), Numbers.doubleToInt(frameSize/(25/11)), Numbers.doubleToInt(frameSize/5), Numbers.doubleToInt(frameSize/(100/3)));

    }

  }

}

Numbers.java:

package Numbers;

public class Numbers {
  public static int doubleToInt(double toConvert) {
    int toReturn = (int) toConvert;
    return toReturn;
  }
}

Any help would be appreciated! :)

Please note I am not close to finishing so please ignore the empty method.

Darcy Sutton
  • 111
  • 6
  • 2
    Your components are not resizing / moving because you are using `null`-layout with fixed sizes and bounds. This is actually kind of considered [bad practice](https://stackoverflow.com/questions/6592468/why-is-it-frowned-upon-to-use-a-null-layout-in-swing) and I also would advise you against it. Instead, use one (or more) appropriate [layout manager(s)](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html). The linked tutorial also contains all of the java swing basics, which I highly suggest that you check those out. – maloomeister Oct 12 '21 at 10:23
  • I am completely aware that I am doing this but I need to use absolute positioning. If there is any other way to perform absolute positioning then please inform me of it. :) – Darcy Sutton Oct 13 '21 at 08:04
  • What do you mean, you _need to use absolute positioning_? Why? _ If there is any other way to perform absolute positioning..._ - well, no, you do it in your code and you see the side effects of it. But you asked how to remove those effects and I told you in my comment above. – maloomeister Oct 13 '21 at 08:06
  • I want to be able to place elements exactly where I want them, so I have to use absolute positioning unless there is another way to do this. – Darcy Sutton Oct 13 '21 at 08:09
  • You can also place your components exactly where you want them, if you work with layout managers? That's what they are used for. The components will behave exactly as you specify them to behave. – maloomeister Oct 13 '21 at 08:27
  • Ok thank you for you help. – Darcy Sutton Oct 13 '21 at 08:28

0 Answers0