簡體   English   中英

JFrame組件未顯示

[英]JFrame Components not showing up

我有一個問題,JFrame沒有顯示我的組件。 當我在WindowBuilder中打開GasStationPanel時,它顯示效果很好,但MainFrame顯示為空白窗口。 拜托,我需要你幫忙。 謝謝!

JFrame代碼是:

public class MainFrame extends JFrame {
    private GasStationPanel pnlMainGasStation;

    public MainFrame() throws SecurityException, IOException {
        try {               UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            SwingUtilities.updateComponentTreeUI(this);
        } catch (Exception e) {
            e.printStackTrace();
        }

        getContentPane().setLayout(new BorderLayout());
        this.pnlMainGasStation = new GasStationPanel("all cars","pumps","coffee");
        this.add(pnlMainGasStation, BorderLayout.CENTER);
        setLocationRelativeTo(null);

        setTitle("GasStation");
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                Utils.closeApplication(MainFrame.this);
            }
        }); 

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = new Dimension();
        frameSize.setSize(screenSize.width*0.7, screenSize.height*0.9);
        setSize(frameSize);
        setVisible(true);    
    }
    public GasStationPanel getMainPanel() {
        return pnlMainGasStation;
    }
}

GasStationPanel代碼:

public class GasStationPanel extends JPanel {
private JSplitPane splinterRight, splinterLeft;
private AllCarsPanel allCarsPanel;
private FuelPumpListPanel fuelPumpsListPanel;
private CoffeeHousePanel coffeeHousePanel;

private List<GasStationController> allListeners;

public AllCarsPanel getAllCarsPanel() {
    return allCarsPanel;
}

public FuelPumpListPanel getFuelPumpsListPanel() {
    return fuelPumpsListPanel;
}

public CoffeeHousePanel getCoffeeHousePanel() {
    return coffeeHousePanel;
}

public GasStationPanel(String allCarsStr, String fuelPumpsListStr,
        String coffeeHousePanelStr) throws SecurityException, IOException {
    // Init Listeners List
    this.allListeners = new ArrayList<GasStationController>();
    // Layout and size
    setLayout(new BorderLayout());

    // Build panels
    allCarsPanel = new AllCarsPanel();
    fuelPumpsListPanel = new FuelPumpListPanel();
    coffeeHousePanel = new CoffeeHousePanel();

    // Split the screen to three
    splinterRight = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
    splinterLeft = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
    splinterLeft.setLeftComponent(allCarsPanel);
    splinterLeft.setRightComponent(fuelPumpsListPanel);
    splinterRight.setLeftComponent(splinterLeft);
    splinterRight.setRightComponent(coffeeHousePanel);
}

public void registerListener(GasStationController gasStationController) {
    this.allListeners.add(gasStationController);
}

簡而言之,您永遠不會向容器中添加任何組件。 例如,在您的GasStationPanel代碼中,您可能應該嘗試通過傳入JSplitPane作為參數來調用add(Component) 例如:

add(splinterLeft, BorderLayout.CENTER);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM