在DesignGridLayout中强制固定大小的JScrollPane

时间:2013-05-05 14:12:17

标签: java jscrollpane jtextarea jinternalframe

我想显示带有2个可见行和30个可见列的JTextArea。

包测试;

import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JComboBox;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import net.java.dev.designgridlayout.DesignGridLayout;

/**
 *
 * @author Denni
 */
public class TestWindow extends JFrame {
    public TestWindow() {
        this.setPreferredSize(new Dimension(700, 600));
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        JDesktopPane desktopPane = new JDesktopPane();
        this.getContentPane().add(desktopPane);
        ModifyFrame modifyFrame = new ModifyFrame();
        modifyFrame.pack();
        modifyFrame.setVisible(true);
        desktopPane.add(modifyFrame);
        this.pack();
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new TestWindow().setVisible(true);
            }
        });
    }

    class ModifyFrame extends JInternalFrame {
        private JTabbedPane tabPane;
        private JPanel panel;
        private JComboBox jcb;
        private JTextArea jta;
        private JTextField jtf1;
        private JTextField jtf2;
        private JTextField jtf3;

        public ModifyFrame() {
            super("Testting", true, true, true);
            initComponents();
        }
        private void initComponents() {
            this.setPreferredSize(new Dimension(600,450));
            this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
            tabPane = new JTabbedPane();
            this.getContentPane().add(BorderLayout.CENTER, tabPane);
            panel = new JPanel();
            jta = new JTextArea(2, 30);
            Dimension d = jta.getPreferredSize();
            jcb = new JComboBox();
            jtf1 = new JTextField();
            jtf2 = new JTextField();
            jtf3 = new JTextField();
            JScrollPane tmp = new JScrollPane();
            tmp.setPreferredSize(d);
            tmp.setSize(d);
            tmp.setMaximumSize(d);
            tmp.getViewport().add(jta);
            DesignGridLayout layout = new DesignGridLayout(panel);
            layout.disableSmartVerticalResize();
            layout.row().grid().empty().grid(new JLabel("Testing")).add(new JSeparator());
            layout.row().grid(new JLabel("jtf1")).add(jtf1).grid(new JLabel("jta")).add(tmp);
            layout.row().grid(new JLabel("jtf2")).add(jtf2).grid().spanRow();
            layout.row().grid(new JLabel("jtf3")).add(jtf3).grid(new JLabel("jcb")).add(jcb);
            this.getContentPane().add(tabPane);
            tabPane.add("Tab ", panel);
        }
    }    
}

使用这些代码,我无法使用2行和30列大小查看我的jta。 JScrollPane总是将自身调整为超过10个和50列的大小。 但是,如果我没有将jta放入JScrollPane,它可以查看精确的2行和30列大小!

如何强制JScrollPane服从大小?

0 个答案:

没有答案