0

我有一个问题是 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);
}
4

1 回答 1

2

简而言之,您永远不会向容器中添加任何组件。例如,在您的GasStationPanel代码中,也许您应该尝试add(Component)通过传入JSplitPanes 作为参数来调用。例如:

add(splinterLeft, BorderLayout.CENTER);
于 2013-09-08T13:22:50.690 回答