【发布时间】:2015-03-10 03:09:44
【问题描述】:
我想创建两个固定大小的不可编辑的文本框(每个将只包含一行),但我希望它们是可滚动的(仅水平滚动),因为我知道它们将包含的文本会很长。我希望它们位于我在下面定义的两个按钮下方,并且我希望每个文本框都位于它们自己的行中。
问题是,一切都显示出来,按钮按预期工作,但文本框不会滚动,尽管我可以以某种方式拖动并选择框中不可见的其余文本。我不知道标签是否可以滚动,它们会是更好的选择吗?
代码:
public static void main(String[] args)
{
JFrame win = new JFrame("Window");
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.setSize(400, 300);
GridBagConstraints c = new GridBagConstraints();
win.setLayout( new GridBagLayout() );
JTextArea master = new JTextArea(1,1);
JTextArea vendor = new JTextArea(1,1);
master.setEditable(false);
vendor.setEditable(false);
master.setPreferredSize( new Dimension(100,20) );
vendor.setPreferredSize( new Dimension(100,20) );
master.setText(/*some really long string*/);
vendor.setText(/*some really long string*/);
JScrollPane mPane = new JScrollPane(master, JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
JScrollPane vPane = new JScrollPane(vendor, JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
mPane.getHorizontalScrollBar().isVisible();
vPane.getHorizontalScrollBar().isVisible();
JButton one = new JButton("Select");
ActionListener select = new SelectButton(master, vendor);
one.addActionListener(select);
JButton two = new JButton("Run");
c.gridx = 0;
c.gridy = 0;
win.add(one, c);
c.gridx = 1;
c.gridy = 0;
win.add(two, c);
c.gridx = 0;
c.gridy = 1;
win.add(master, c);
win.add(mPane, c);
c.gridx = 0;
c.gridy = 2;
win.add(vendor, c);
win.add(vPane, c);
win.setLocationRelativeTo(null);
win.setVisible(true);
return;
}
【问题讨论】:
-
永远不要使用
setPreferredSize! Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?
标签: java swing user-interface layout-manager gridbaglayout