我正在尝试创建2dimesional JTabbedPane。我在coderanch处发现了一个小示例,以二维创建JTabbedPane。我现在想做的是在创建表之后,从外部类向每个选项卡添加一个图形。意思是,我创建了这张表,然后将想要的图形放在表中(也许用户事先不知道所需的顺序,只知道要使用的图形数)。但是我似乎无法更新该标签。我希望有人可以告诉我我做错了什么。这是用于添加文本(而不是图形)的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)));
            }


java - 添加到2D选项卡式JTabbedPane-LMLPHP

或者像这个

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;
    }

}


java - 添加到2D选项卡式JTabbedPane-LMLPHP

关于java - 添加到2D选项卡式JTabbedPane,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41127037/

10-12 18:47