JFrame组件未显示

时间:2013-09-08 13:17:00

标签: java swing jframe jpanel windowbuilder

我遇到的问题是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);
}

1 个答案:

答案 0 :(得分:2)

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

add(splinterLeft, BorderLayout.CENTER);