JPanel不会在JFrame上显示

时间:2013-11-20 02:19:14

标签: java swing jframe awt

这是代码

import Sprites.*;

import javax.swing.*;

import java.awt.*;
public class Main {
public int Health = 100;
public int ArmourPt = 0;
public 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 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));
    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, 1135);
    MPanel.add(ControlPanel);

    MainWindow.setVisible(true);

    }

}

应该做什么:使用JPanels将屏幕分成四个部分

它的作用:显示三个面板,Stats,Inv和Display,但不显示Control。

请注意!我以前曾经告诉过我,所以现在让我说清楚。我没有计划,也不想使用布局管理器。这个维度必须非常具体,我不想使用技巧来使用布局管理器。回答时请记住这一点。如果你提到它,那没关系,但请不要直截了当地说“使用布局管理器”&#39;谢谢。

2 个答案:

答案 0 :(得分:2)

三件事......

  1. 您应该确保在Event Dispatching Thread的上下文中启动UI。有关详细信息,请参阅Initial Threads
  2. 确保将内容(MPanel)实际添加到框架(MainWindow),然后再将其显示
  3. 避免使用null布局
  4. 您可能还想查看Code Conventions for the Java Programming Language

答案 1 :(得分:0)

ControlPanel.setLocation(350, 1135);

应该是

ControlPanel.setLocation(350, 450);

然后你应该没事。

也许你应该用一个起始的lowerCase命名你的变量,我总是感到困惑,好像它们是类名并像静态一样使用。