gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 09:07:44 25 4
gpt4 key购买 nike

我正在尝试创建一个二维 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)));
}

enter image description here

或者像这个

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

}

enter image description here

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com