[英]Custom view classes in IntelliJ IDEA
因此,我正在使用布局管理器GridLayoutManager(IntelliJ)在IntelliJ IDEA中構建Swing布局。
可以,我可以布局所有內容,但所有內容都在同一代碼文件中,考慮到我將JPanel與JTabbedPane結合使用,我希望選項卡式窗格的每個窗格都在一個單獨的類中表示。
這怎么可能?
您可以通過幾種方法來執行此操作,即擴展JPanel
或創建另一個包含JPanel
基於繼承的解決方案
public class MyCustomPanel extends JPanel {
// implement your custom behavior in here
}
然后,在您創建JTabbedPane
您將獲得以下內容:
private void init() {
JTabbedPane jtp = new JTabbedPane();
JPanel jp = new MyCustomPanel();
jtp.add(jp);
}
盡管這樣做有效, JPanel
長遠來看,擴展JPanel
可能會引起頭痛。 另一個主張組合而不是繼承的方法可能看起來像這樣:
基於組合的解決方案
public class MyCustomPanel {
private JPanel myPanel = new JPanel();
public MyCustomPanel() {
// add your customizations to myPanel
}
public JPanel getPanel() {
return myPanel;
}
}
然后在其中創建JTabbedPane
private void init() {
JTabbedPane jtp = new JTabbedPane();
MyCustomPanel mp = new MyCustomPanel();
jtp.add(mp.getPanel());
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.