[英]External JPanle doesn't display on JFrame
我正在做一個大學項目。 該項目是一個單例秋千應用程序。 我已經為一個大型機提供了服務,當用戶單擊其他菜單選項卡時,每次使用一個新的外部面板時,我都會嘗試更新該大型機。 問題是當我在Mainframe中加載外部JPanel時不顯示任何內容。 我還問我采取的方式是否正確。 謝謝!
Mainframe.js
public class MainFrame extends JFrame {
JMenuBar menuBar;
JMenu homeshop, topCat, orders, login, register, exit;
ShopPanel shopPanel;
private OrderTrack orderTrack = new OrderTrack();
private void changePanel(JPanel panel) {
getContentPane().removeAll();
getContentPane().add(panel, BorderLayout.CENTER);
getContentPane().doLayout();
update(getGraphics());
}
private class MenuAction implements ActionListener {
private JPanel panel;
private MenuAction(JPanel pnl) {
this.panel = pnl;
}
@Override
public void actionPerformed(ActionEvent e) {
changePanel(panel);
}
}
private void initMenu() {
// Menu setup
menuBar = new JMenuBar();
//Items
homeshop = new JMenu("Shop");
menuBar.add(homeshop);
topCat = new JMenu("Top Books");
menuBar.add(topCat);
orders = new JMenu("Orders");
//orders.addActionListener(new MenuAction(orderTrack));
menuBar.add(orders);
login = new JMenu("Login");
menuBar.add(login);
register = new JMenu("Register");
menuBar.add(register);
setJMenuBar(menuBar);
}
public void initMainFrame () {
// Frame setup
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
shopPanel = new ShopPanel();
add(shopPanel);
setVisible(true);
}
public MainFrame() {
super("Welcome!");
initMenu();
initMainFrame();
}
}
ShopPanel.j
public class ShopPanel extends JPanel {
JPanel panel = new JPanel();
private JList<String> shopList;
private ArrayList<String> titleBooks;
public ShopPanel() {
// QUERY //
DBQuery bookTitle = new DBQuery();
titleBooks = bookTitle.QueryOne("select title from book", "title");
// GRIDBAG LAYOUT //
panel.setLayout(new GridBagLayout());
// label //
JLabel lblInsertOrderId = new JLabel("Insert order ID");
panel.add(lblInsertOrderId);
// LIST OF BOOKS //
shopList = new JList(titleBooks.toArray());
shopList.setVisibleRowCount(6);
shopList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
panel.add(shopList);
}
}
App.js
public class App {
public static void main (String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new MainFrame();
}
});
}
}
沒有錯誤信息。
您的ShopPanel本身就是JPanel。 無需擁有成員JPanel並將所有內容添加到該成員中。 那不會被顯示,因為它不會被添加到整個組件樹中。 嘗試刪除“面板”,並在沒有它的情況下執行所有操作,例如:add()而不是panel.add()。 沒嘗試...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.