简体   繁体   English

IntelliJ IDEA中的自定义视图类

[英]Custom view classes in IntelliJ IDEA

So I am building a Swing layout in IntelliJ IDEA using the layout manager GridLayoutManager(IntelliJ). 因此,我正在使用布局管理器GridLayoutManager(IntelliJ)在IntelliJ IDEA中构建Swing布局。

It's going ok, I can layout all my stuff etc but everything is in the same code file and considering I am using a JPanel with a JTabbedPane, I would like each pane of the tabbed pane to be represented in a separate class. 可以,我可以布局所有内容,但所有内容都在同一代码文件中,考虑到我将JPanel与JTabbedPane结合使用,我希望选项卡式窗格的每个窗格都在一个单独的类中表示。

How is this possible? 这怎么可能?

There are a couple ways you could do this, either extending JPanel or creating another class which contains a JPanel 您可以通过几种方法来执行此操作,即扩展JPanel或创建另一个包含JPanel

Inheritance Based Solution 基于继承的解决方案

public class MyCustomPanel extends JPanel {

    // implement your custom behavior in here
}

Then in your where you create your JTabbedPane you'd have something like this: 然后,在您创建JTabbedPane您将获得以下内容:

private void init() {
    JTabbedPane jtp = new JTabbedPane();
    JPanel jp = new MyCustomPanel();
    jtp.add(jp);
}

Although this works, extending JPanel may cause headaches in the long run. 尽管这样做有效, JPanel长远来看,扩展JPanel可能会引起头痛。 Another approache, which favors composition over inheritance might look something like this: 另一个主张组合而不是继承的方法可能看起来像这样:

Composition Based Solution 基于组合的解决方案

public class MyCustomPanel {
    private JPanel myPanel = new JPanel();

    public MyCustomPanel() {
        // add your customizations to myPanel
    }

    public JPanel getPanel() {
        return myPanel;
    }
}

and then where you create your JTabbedPane 然后在其中创建JTabbedPane

private void init() {
    JTabbedPane jtp = new JTabbedPane();
    MyCustomPanel mp = new MyCustomPanel();
    jtp.add(mp.getPanel());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM