我正在尝试创建一个2dimesional JTabbedPane。我在coderanch找到了一个小例子来创建2维的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();
}
}
答案 0 :(得分:1)
要么你想要标签导航上的数字,比如
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;
}
}