I create a HashMap<String, Panel> via a method and return it to use, but one Panel(JPanel) element changes its coordinates to x0 y0 even though I don't, is this a compiler error or my code?
I checked the objects during their creation, but the problem is not in the method that creates
Main class:
private static boolean initialization() {
Window mainWindow = new Window(PROGRAM_NAME, true, -1, -1, WINDOW_MAIN_WIDTH, WINDOW_MAIN_HEIGHT);
mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainWindow.setResize(false);
int[] size = {
100,
100,
100,
100
};
Color[] colors = {
Color.RED,
Color.BLUE,
Color.YELLOW,
Color.ORANGE,
Color.CYAN
};
mainWindow.setPanels(WindowPanels.createPanels(size, colors, mainWindow));
System.out.println(mainWindow.getPanel("down").getLocation());
Label l = new Label("Test", 0, 0);
l.setColor(Color.BLACK);
mainWindow.addCom(l, "left");
l = new Label("Test", 0, 0);
l.setColor(Color.BLACK);
mainWindow.addCom(l, "top");
l = new Label("Test", 0, 0);
l.setColor(Color.BLACK);
mainWindow.addCom(l, "right");
l = new Label("Test", 0, 0);
l.setColor(Color.BLACK);
mainWindow.addCom(l, "down");
l = new Label("Test", 0, 0);
l.setColor(Color.BLACK);
mainWindow.addCom(l, "head");
System.out.println(mainWindow.getPanel("down").getLocation());
windows.add(mainWindow);
return true;
}
Class with method for create:
public class WindowPanels {
public static HashMap<String, Panel> createPanels(int[] size, Color[] color, Window window){
HashMap<String, Panel> panels = new HashMap<String, Panel>();
int winWidth = window.getWidth(), winHeight = window.getHeight();
int x = 0, y = 0, width = winWidth, height = winHeight;
if (size[0] > 0) {
x = 0;
y = size[1];
width = size[0];
height = winHeight - size[1] - size[3];
panels.put("left", new Panel(color[0], x, y, width, height));
} else size[0] = 0;
if (size[1] > 0) {
x = 0;
y = 0;
width = winWidth;
height = size[1];
panels.put("top", new Panel(color[1], x, y, width, height));
} else size[1] = 0;
if (size[2] > 0) {
x = winWidth - size[2];
y = size[1];
width = size[2];
height = winHeight - size[1] - size[3];
panels.put("right", new Panel(color[2], x, y, width, height));
} else size[2] = 0;
if (size[3] > 0) {
x = 0;
y = winHeight - size[3];
width = winWidth;
height = size[3];
panels.put("down", new Panel(color[3], x, y, width, height));
} else size[3] = 0;
x = size[0];
y = size[1];
width = winWidth - size[0] - size[2];
height = winHeight - size[1] - size[3];
panels.put("head", new Panel(color[4], x, y, width, height));
return panels;
}
}
From class Panel (extends JPanel)
public Panel(Color colorBackground, LayoutManager layout, int x, int y, int width, int height) {
super(layout);
setBackground(colorBackground);
setBounds(x, y, width, height);
}
even if I do this:
System.out.println(mainWindow.getPanel("down").getLocation());
System.out.println(mainWindow.getPanel("down").getLocation());
it will return different values
CodePudding user response:
You badly need to use a LayoutManager. It is a class designed to keep your components at the correct size and position, even if windows or content need a resize.
Check this nice Oracle tutorial on Layout Managers.