我试图用几个选项卡式窗格创建一个应用程序,并且为了使代码易于管理,我想将这些窗格的内容放在单独的类中,放在单独的.java文件中。

我目前有3个文件
(i)TestLayout.java

package testlayout;
public class TestLayout
{
    public static void main(String[] args)
    {
        MainFrame mainFrameObject = new MainFrame();
        mainFrameObject.displayMainFrame();
    }
}


(ii)MainFrame.java

package testlayout;

import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingConstants;

public class MainFrame
{
    JFrame masterFrame = new JFrame("JAVA 1.1");
    JTabbedPane tabbedPane = new JTabbedPane();
    public void displayMainFrame()
    {
        masterFrame.setSize(1000, 600);
        masterFrame.setVisible(true);
        masterFrame.setResizable(false);
        masterFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        masterFrame.add(tabbedPane);

        DisplayReadMe drmObj = new DisplayReadMe();
        drmObj.showReadMe();
        //showReadMe();
    }

    /*
    public void showReadMe()
    {
        JPanel panelReadMe = new JPanel(new GridLayout(10,1,8,8));
        panelReadMe.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
        tabbedPane.addTab("Read Me", null, panelReadMe, "First Tab");
        String testreadMeTestMessage = "This is a test message";

        JLabel testreadMeLabel = new JLabel(testreadMeTestMessage, SwingConstants.LEFT);
        testreadMeLabel.setBorder(BorderFactory.createLineBorder(Color.orange,3));
        panelReadMe.add(testreadMeLabel);
    }
    */
}



(iii)DisplayReadMe.java

package testlayout;

import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

public class DisplayReadMe extends MainFrame
{
    public DisplayReadMe()
    {

    }
    public void showReadMe()
    {
        System.out.println("method showReadMe begins");
        JPanel panelReadMe = new JPanel(new GridLayout(10,1,8,8));
        panelReadMe.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
        tabbedPane.addTab("Read Me", null, panelReadMe, "First Tab");
        String testreadMeTestMessage = "This is a test message";

        JLabel testreadMeLabel = new JLabel(testreadMeTestMessage, SwingConstants.LEFT);
        testreadMeLabel.setBorder(BorderFactory.createLineBorder(Color.orange,3));
        panelReadMe.add(testreadMeLabel);
        System.out.println("method showReadMe ends");
    }
}


我的查询是,当我取消注释// showReadMe();时。和MainFrame中的showReadMe方法一起使用。该选项卡已添加到JFrame,并且测试消息显示在框中。

但是应该

DisplayReadMe drmObj = new DisplayReadMe();
drmObj.showReadMe();


代码,不做同样的事情?我不是从DisplayReadMe类调用showReadMe方法吗,类似于showReadMe()。

我尝试过重新验证,重新绘制和线程化,似乎什么也没有调用该方法并显示选项卡和消息?

任何指导将不胜感激
非常感谢
PG

最佳答案

该方法实际上是有效的,但相对于MainFrame中的JTabbedPane类成员,drmObj中的tabbedPane实例是不同的。尝试将tabbedPane添加为showReadMe()中的参数,并在添加元素时引用该实例。它应该工作。

公共无效的showReadMe(JTabbedPane tabbedPane);

...

drmObj.showReadMe(this.tabbedPane);

希望能帮助到你!

08-25 02:00