gpt4 book ai didi

java - JFrame 组件未显示

转载 作者:行者123 更新时间:2023-12-02 06:45:28 26 4
gpt4 key购买 nike

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

关于java - JFrame 组件未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18684102/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com