Java Jtabbedpane或如何获取创建选项卡的自定义对象

发布于 2025-01-23 11:30:19 字数 2566 浏览 2 评论 0原文

我经常为创建EG GUI组件的自定义对象问题而苦苦挣扎。而且我永远不知道从GUI组件回到对象的最佳方法是什么。

因此,欢迎多个骇客和技巧。

让我向您解释一下:

这是我的自定义对象,以后我需要找到

public class MyObject {
    int yearOfBirth;
    String name;
    public MyObject(int yearOfBirth, String name) {
        this.yearOfBirth = yearOfBirth;
        this.name = name;
    }
    public int getYearOfBirth() {
        return yearOfBirth;
    }
    
    public Component getPanel() {
        Component panel1 = makeTextPanel("This is the personal tab of "+name);
        return panell;
    }
}

我需要通过标签找到它的地方

public class MyTabControl implements ChangeListener {
    JTabbedPane myTabPane = new JTabbedPane();
    
    public MyTabControl(){
        //This will add a Listener for clicking on one Tab
        myTabPane.addChangeListener(this);
    }
    
    public void oneMoreTab(MyObject myObject) {
        myTabPane.addTab(myObject.name, myObject.getPanel())
        
    }
    
    public void stateChanged(ChangeEvent arg0) {
        System.out.println("Focus of Tab changed");
        int actualFocusedTabIndex = myTabPane.getSelectedIndex();
        Component acutalFocusedComponent = myTabPane.getComponentAt(actualFocusedTabIndex);
        //This works fine, I can get the Tab. Or at least the Component.
        //But how do I get the yearOfBirth or better the Object itself?
        int yearOfBirthOfTheSelectedTab = ???
    }
}

这是 只是主函数

public static void main(String[] args) {
    //Commands to start and create the GUI
    
    MyTabControl myTabControl = new MyTabControl();
    
    MyObject mother = new MyObject(1960, "Helen");
    myTabControl.oneMoreTab(mother);
    
    MyObject father = new MyObject(1955, "James");
    myTabControl.oneMoreTab(father);
}

编辑:

1不工作解决方案:扩展组件类

我试图扩展类组件。但这将造成失败(请参阅代码中的注释):

public class ComponentWithExtras extends Component {
   MyObject myObject;
   public void addMyObject(MyObject myObject) {
      this.myObject = myObject;
   }
}


// The following line will create failure: Can't cast Component to ComponentWithExtras
ComponentWithExtras componentWithExtras = (ComponentWithExtras) myObject.getPanel();
componentWithExtras.addMyObject(myObject);
myTabPane.addTab(myObject.name, componentWithExtras);

I am often struggling with the same problem of custom Objects that creates a e.g. gui Component. And I never know what is the best way to get from the gui Component back to the object.

So multiple hacks and tricks are welcome.

Let me explain it to you:

This is my custom Object I need to find afterwards

public class MyObject {
    int yearOfBirth;
    String name;
    public MyObject(int yearOfBirth, String name) {
        this.yearOfBirth = yearOfBirth;
        this.name = name;
    }
    public int getYearOfBirth() {
        return yearOfBirth;
    }
    
    public Component getPanel() {
        Component panel1 = makeTextPanel("This is the personal tab of "+name);
        return panell;
    }
}

This is where I need to find it through the Tab I am focusing

public class MyTabControl implements ChangeListener {
    JTabbedPane myTabPane = new JTabbedPane();
    
    public MyTabControl(){
        //This will add a Listener for clicking on one Tab
        myTabPane.addChangeListener(this);
    }
    
    public void oneMoreTab(MyObject myObject) {
        myTabPane.addTab(myObject.name, myObject.getPanel())
        
    }
    
    public void stateChanged(ChangeEvent arg0) {
        System.out.println("Focus of Tab changed");
        int actualFocusedTabIndex = myTabPane.getSelectedIndex();
        Component acutalFocusedComponent = myTabPane.getComponentAt(actualFocusedTabIndex);
        //This works fine, I can get the Tab. Or at least the Component.
        //But how do I get the yearOfBirth or better the Object itself?
        int yearOfBirthOfTheSelectedTab = ???
    }
}

This is just the main function

public static void main(String[] args) {
    //Commands to start and create the GUI
    
    MyTabControl myTabControl = new MyTabControl();
    
    MyObject mother = new MyObject(1960, "Helen");
    myTabControl.oneMoreTab(mother);
    
    MyObject father = new MyObject(1955, "James");
    myTabControl.oneMoreTab(father);
}

EDIT:

1 not working solution: Extend Component class

I have tried to extend the class Component. But this will create a failure (see comment in code):

public class ComponentWithExtras extends Component {
   MyObject myObject;
   public void addMyObject(MyObject myObject) {
      this.myObject = myObject;
   }
}


// The following line will create failure: Can't cast Component to ComponentWithExtras
ComponentWithExtras componentWithExtras = (ComponentWithExtras) myObject.getPanel();
componentWithExtras.addMyObject(myObject);
myTabPane.addTab(myObject.name, componentWithExtras);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文