Reputation: 21
This is the code.
public class Main {
public static int Health = 100;
public static int ArmourPt = 0;
public static int Gold = 250;
public static JLabel Goldlbl = new JLabel("Gold:");
public static JLabel Healthlbl = new JLabel("Health:");
public static JLabel ArmourPtLbl = new JLabel("Armour Points");
public static String GoldString = (Integer.toString(Gold));
public static JLabel GoldDis = new JLabel(GoldString);
public static String HealthString = (Integer.toString(Health));
public static JLabel HealthDis = new JLabel(HealthString);
public static JFrame MainWindow = new JFrame();
public static int CalcDamage(int Damage, int Armour) {
int ReturnDamage = Damage - Armour + Damage / Damage + Armour / Damage * Damage - Armour + Damage + 25 ;
if(ReturnDamage < 0){
ReturnDamage = 0;
}
return ReturnDamage;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
MainWindow.setSize(1500, 700);
MainWindow.setTitle("Ampventure");;
JPanel MPanel = (JPanel)MainWindow.getContentPane();
MPanel.setLayout(null);
JPanel StatsPanel = new JPanel();
StatsPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 4));
StatsPanel.setSize(350, 450);
StatsPanel.setLocation(0, 0);
StatsPanel.setMaximumSize(new Dimension (350, 450));
StatsPanel.add(Goldlbl);
Goldlbl.setLocation(30, 14);
StatsPanel.add(GoldDis);
GoldDis.setLocation(35, 14);
StatsPanel.add(Healthlbl);
Healthlbl.setLocation(0, 0);
MPanel.add(StatsPanel);
JPanel InvPanel = new JPanel();
InvPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 4));
InvPanel.setSize(350, 212);
InvPanel.setLocation(0, 450);
StatsPanel.setMaximumSize(new Dimension (350, 250));
MPanel.add(InvPanel);
JPanel DisplayPanel = new JPanel();
DisplayPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 4));
DisplayPanel.setSize(1135, 450);
DisplayPanel.setLocation(350, 0);
MPanel.add(DisplayPanel);
JPanel ControlPanel = new JPanel();
ControlPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 4));
ControlPanel.setSize(1135, 212);
ControlPanel.setLocation(350, 450);
MPanel.add(ControlPanel);
MainWindow.setVisible(true);
}
}
What is supposed to happen: A window divided into four sections and on the upper right section two labels named Gold: and Health: displayed with the first spaced above the second one.
What happens at execution: The label Health: is horizontally next to the label Gold:
I have tried changing everything, but nothing works.
Also I realize my program uses a null layout manager, but that is my intention. Not a mistake.
Upvotes: 2
Views: 136
Reputation: 21233
You set the null layout only for MPanel
. Nested panels do not inherit parent's layout. StatsPanel
has FlowLayout
as this is a default layout of JPanel
. Goldlbl
and Healthlbl
are positioned according to the rules of FlowLayout
. See How to Use FlowLayout for more examples.
If the intention is to use absolute positioning then execute StatsPanel.setLayout(null);
. In case of absolute layout you have to call setbounds
method for each of the container's children. See Doing Without a Layout Manager for details.
As a side note, the naming convention of the posted snippet is a bit confusing. See Java Naming Conventions.
Upvotes: 2