项目不会在JPanel周围移动

时间:2013-12-08 00:39:45

标签: java swing layout awt

这是代码。

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);
    }
}

应该发生什么:一个窗口分为四个部分,在右上部分有两个名为Gold:和Health的标签:第一个显示在第二个上面。

执行时会发生什么:标签Health:水平位于标签Gold旁边:

我尝试过改变一切,但没有任何作用。

此外,我意识到我的程序使用空布局管理器,但这是我的意图。不是一个错误。

1 个答案:

答案 0 :(得分:2)

仅为MPanel设置空布局。嵌套面板不会继承父级布局。 StatsPanelFlowLayout,因为这是JPanel的默认布局。根据{{​​1}}的规则定位GoldlblHealthlbl。有关更多示例,请参阅How to Use FlowLayout

如果打算使用绝对定位,则执行FlowLayout。在绝对布局的情况下,您必须为每个容器的子项调用StatsPanel.setLayout(null);方法。有关详细信息,请参阅Doing Without a Layout Manager

作为旁注,发布的代码段的命名约定有点令人困惑。请参阅Java Naming Conventions