我遇到了下一个 JTabbedPanes 的问题,这让我很困惑。我已将问题隔离为一个简短的演示应用程序(基于 oracle 选项卡式窗格教程):
package main.java;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
public class TabbedPaneDemo extends JPanel {
public static JTabbedPane topTabs = new JTabbedPane();
public TabbedPaneDemo() {
super(new BorderLayout());
topTabs.setPreferredSize(new Dimension(400, 200));
topTabs.add(makeReadoutPanel("\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15"));
add(topTabs);
}
private static JTabbedPane makeReadoutPanel(String text) {
// Create tabbed pane
JTabbedPane tabs = new JTabbedPane();
tabs.setName("Tabs");
// Create text field
JTextArea textArea = new JTextArea(text);
textArea.setEditable(false);
textArea.setFont(new Font("Monospaced", Font.PLAIN, 14));
// Create first panel for text field
JPanel textPanel = new JPanel();
textPanel.setLayout(new BorderLayout());
textPanel.setName("Text Panel");
textPanel.add(textArea, BorderLayout.CENTER);
// Add text field panel to tabs
tabs.add(textPanel);
return tabs;
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("TabbedPaneDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Add content to the window.
frame.add(new TabbedPaneDemo(), BorderLayout.CENTER);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event dispatch thread:
//creating and showing this application's GUI.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
//Turn off metal's use of bold fonts
UIManager.put("swing.boldMetal", Boolean.FALSE);
createAndShowGUI();
}
});
}
}
当它运行时,JTextArea 被截断,没有滚动条,而不是垂直于面板。textPanel 中的 BorderLayout 不应该将文本区域大小设置为选项卡吗?这只发生在嵌套选项卡上:如果我只使用单级选项卡,则文本区域适合。