JTabbedPane的JTextfield超大

时间:2013-02-20 14:15:25

标签: java swing

    textArea = new JTextArea(textString);
    JScrollPane text = new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    text.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

我将JScrollPane文本添加到JTabbedPane,没什么特别的。

然而,当我切换标签时,texfield会扩展整个窗口并调整其大小。

    JFrame frame = new JFrame();
    frame.setSize(600, 500);
    JPanel main = new JPanel();
    main.setLayout(new BorderLayout(0,0));
    main.setBorder(new EmptyBorder(10, 10, 10, 10) );
    textArea = new JTextArea(textString);
    JScrollPane text = new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    text.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

    JTabbedPane tabs = new JTabbedPane();
    tabs.add("Text lines", text);
    tabs.add("Another", new JPanel());
    main.add(new JLabel("test"), BorderLayout.NORTH);
    main.add(tabs, BorderLayout.SOUTH);
    frame.setContentPane(main);
    frame.pack();

1 个答案:

答案 0 :(得分:2)

我在您的示例中发现的问题是,当您在文本区域中输入多行并切换到另一个选项卡时,选项卡式窗格的大小会增加。为了解决这个问题,只需输入要在JTextArea构造函数中显示的文本区域行数。例如,要显示5行:

textArea = new JTextArea(textString, 5, 0);

现在,选项卡式窗格不会调整大小。