I am programming an application that works with swing components, I notice one thing on which I would explain I have these classes:
this is the enum on which i create the gui dimension
public enum GuiDimension { WIDTH(700), HEIGHT(400); private final int value; private GuiDimension(int value) { this.value = value; } public int getValue(){ return value; } }
this class that launches the application
private GamePanel gamePanel = new GamePanel(); public static void main(String[] args) { new MainFrame(); } public MainFrame() { initGameFrame(); } private void initGameFrame() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); add(gamePanel); setResizable(false); setUndecorated(true); pack(); setVisible(true); setLocationRelativeTo(null); } }
and this class that sets the panel size
public class GamePanel extends JPanel { public GamePanel() { setPreferredSize(new Dimension(GuiDimension.WIDTH.getValue(),GuiDimension.HEIGHT.getValue()));
What I noticed is that the enumerations are not integers, but objects, but when I return
they return integers that can be used for other purposes after they are accepted.
now if i insert this in :
SetSize (new Dimension (GuiDimension.WIDTH.getValue (), GuiDimension.HEIGHT.getValue ()));
or
SetSize (GuiDimension.WIDTH.getValue (), GuiDimension.HEIGHT.getValue ());
instead, which I inserted as an example
setPreferredSize(new Dimension(GuiDimension.WIDTH.getValue(),GuiDimension.HEIGHT.getValue()));
the frame is displayed with the wrong size, and I do not understand why. If GuiDimension.WIDTH.getValue () and GuiDimension.WIDTH.getValue ()) are true for setPreferredSize (...) ,
why is it not the same for setSize (int,int) and for setSize(Dimension) ?
when testing this simple code you can see it.
source share