添加到 2D 选项卡式 JTabbedPane

Adding to 2D tabbed JTabbedPane

我正在尝试创建一个 2dimesional JTabbedPane。我在 coderanch 找到了一个创建二维 JTabbedPane 的小例子。我现在想做的是在创建 table 后从外部 class 向每个选项卡添加一个图形。意思是,我创建了这个 table,然后将我想要的图形放在 table 中(也许用户事先不知道需要的顺序,只知道要使用的图形数量)。但我似乎无法更新标签。我希望有人能告诉我我做错了什么。这是一个用于添加文本(而不是图形)的 MWE。

import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTabbedPane;

public class DCTabbed2DPane {
  private Dimension screensize;
  private JFrame frame;
  private JTabbedPane topTabbedPane;
  private JTabbedPane[] leftTabbedPane;
  private int rows;
  private int cols;

  private String frameName;

public DCTabbed2DPane(String frameName, int rows, int cols) {
    this.frameName = frameName;
    this.rows = rows;
    this.cols = cols;
    init();
}

private void setScreenSize() {
    screensize = Toolkit.getDefaultToolkit().getScreenSize();
}

private void setJFrame() {
    frame = new JFrame(frameName);
    frame.setSize((int) (screensize.getHeight() * .75 * 1.618), (int) (screensize.getHeight() * .75));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

private void setJTabbedPane() {
    topTabbedPane = new JTabbedPane(JTabbedPane.TOP);
    leftTabbedPane = new JTabbedPane[cols];

}

private void setTabs() {
    for (int i = 0; i < cols; i++) {
        leftTabbedPane[i] = new JTabbedPane(JTabbedPane.LEFT);
        for (int j = 0; j < rows; j++) {
            leftTabbedPane[i].addTab("Super Layer " + (j + 1), new JLabel("Sector " + (i + 1) + " - " + (j + 1)));
        }
        topTabbedPane.addTab("Sector " + (i + 1), leftTabbedPane[i]);
    }
    frame.add(topTabbedPane);
    topTabbedPane.setSelectedIndex(-1);
}

private void init() {
    setScreenSize();
    setJFrame();
    setJTabbedPane();
    setTabs();
}

public void addCanvasToPane(Integer row, String str) {
    leftTabbedPane[row].addTab("", new JLabel(str));
    frame.add(topTabbedPane);
    frame.revalidate();
    frame.repaint();
}

public void showFrame() {

    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

public static void main(String[] args) {
    int rows = 6;
    int cols = 6;

    DCTabbed2DPane test = new DCTabbed2DPane("PooperDooper", rows, cols);
    for (int j = 0; j < cols; j++) {
        test.addCanvasToPane(j, "Name " + j);
    }
    test.showFrame();

}

}

要么你想要标签导航上的数字,比如

 leftTabbedPane[i] = new JTabbedPane(JTabbedPane.LEFT);
            for (int j = 0; j < rows; j++) {
                leftTabbedPane[i].addTab("Super Layer " + (j + 1),new ImageIcon(getClass().getResource("Images/map.png")),  new JLabel("Sector " + (i + 1) + " - " + (j + 1)));
            }

或者喜欢这个

leftTabbedPane[i] = new JTabbedPane(JTabbedPane.LEFT);
            for (int j = 0; j < rows; j++) {
                leftTabbedPane[i].addTab("Super Layer " + (j + 1), new MyLabel("Images/Horse.png","").getLabel());         
            }

其中 MyLabel

public class MyLabel {
    JLabel label;
    String path;
    String text;

    public MyLabel(String path, String text) {
        this.label = new JLabel();
        this.path = path;
        this.text = text;      
    }
    public void setUpImage(String path) {
        this.label.setIcon(new ImageIcon(getClass().getResource(path)));
    }

    public JLabel getLabel() {
      setUpImage(this.path);
        return this.label;
    }

}