我想显示具有 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 服从大小?