我有一個主框架,並在此框架內顯示其他面板。 但其中一個面板包含按鈕,目的是顯示另一個面板 - 如面板內的顯示面板。面板內的加載面板
這是面板的代碼,我想加載另一個面板:
public class PnlConnectionType extends JPanel {
private JPanel cardPanel;
private CardLayout cl;
/**
* Create the panel.
*/
public PnlConnectionType() {
cardPanel = new JPanel();
cardPanel.setBounds(new Rectangle(10, 11, 775, 445));
cl = new CardLayout();
cardPanel.setLayout(cl);
setBounds(new Rectangle(10, 11, 774, 465));
setLayout(null);
JButton btnRS485 = new JButton("CONNECTION BY RS485");
btnRS485.setBounds(20, 23, 266, 107);
add(btnRS485);
JButton btnNoConnectionoffline = new JButton("NO CONNECTION (OFFLINE)");
btnNoConnectionoffline.setBounds(20, 159, 266, 107);
add(btnNoConnectionoffline);
final settingsPanel2 settPnl2 = new settingsPanel2();
settPnl2.setBounds(new Rectangle(10, 11, 774, 465));
settPnl2.setSize(775,465);
settPnl2.setBounds(10, 11, 775, 445);
cardPanel.add(settPnl2, "1");
btnRS485.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
cl.show(cardPanel, "1");
}
});
}
}
當我點擊按鈕面板上沒有顯示。我做錯了什麼?
您正在使用空佈局,由於很多原因,這是麻煩的。看看[如何使用佈局管理器](https://docs.oracle.com/javase/tutorial/uiswing/layout/using.html)。 – user1803551