我已经在下面设置了TabbedPane测试。我希望标签从左上角到右下角排序,这与下面的代码生成的相反。
以下是下面代码生成的图片。如果我尝试按字母顺序排序标签(例如),这将非常困难。
因此,为了清楚起见,BBL13应为BBL0,BBL5应为BBL19。像这样:
BBL0-BBL5
BBL6-BBL12
BBL13-BBL19
而不是:
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
new Test();
}
private Test() {
try {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame("Main");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setSize(600, 300);
frame.add(createLayout());
frame.setVisible(true);
});
}
private JPanel createLayout() {
JPanel mainLayout = new JPanel(new GridBagLayout());
JTabbedPane mainTabs = new JTabbedPane();
mainTabs.setTabLayoutPolicy(JTabbedPane.WRAP_TAB_LAYOUT);
ArrayList<JComponent> alBible = new ArrayList<>();
for(int i = 0; i < 20; i++) {
alBible.add(i, makeTextArea());
mainTabs.addTab("BBL" + i, alBible.get(i));
}
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty = 1.0;
c.insets = new Insets(0,0,0,0);
mainLayout.add(mainTabs, c);
return mainLayout;
}
private JScrollPane makeTextArea() {
JTextArea textArea = new JTextArea();
textArea.setEditable(true);
JScrollPane scrollTextArea = new JScrollPane(textArea);
scrollTextArea.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
return scrollTextArea;
}
}
答案 0 :(得分:0)
看起来顶部的标签由TabbedPaneUI
public abstract class TabbedPaneUI extends ComponentUI {
public abstract int tabForCoordinate(JTabbedPane pane, int x, int y);
public abstract Rectangle getTabBounds(JTabbedPane pane, int index);
public abstract int getTabRunCount(JTabbedPane pane);
}
仔细研究一下,您使用的默认UI是MetalTabbedPaneUI
这又包含public class TabbedPaneLayout implements LayoutManager
public void layoutContainer(Container parent)
将扩展它的测试放在一起
答案 1 :(得分:-2)
这是不可能的,至少对我而言。因为如果用户选择了BBL0选项卡,那么选项卡是否按照您的要求排序?如何向用户看到其他标签?梦见它。