繁体   English   中英

如何更改JScrollPane中内容的对齐方式

[英]How to change the alignment of contents in a JScrollPane

我正在使用GUI来管理银行帐户。

我在JFrame使用JPanel将所有帐户注册列为按钮,并在JScrollPane以扩展视图。 当我运行程序时,滚动条出现,但是按钮显示为水平。 我必须将其更改为垂直显示吗?

我将更改LayoutManager。 在您的情况下, GridLayout会更好。 您的代码如下所示:

// create all buttons
JButton[] buttons = { new JButton("First Button"), new JButton("Second Button"), ... };

// create the JPanel, your contentPane
JPanel p = new JPanel();
// set the LayoutManager to GridLayout and use a Grid of 1 x buttons.length
p.setLayoutManager(new GridLayout(1, buttons.length));

// add all buttons
for (JButton b : buttons)
    p.add(b);

// set the contentPane
frame.setContentPane(p);

这正是我想要的,但是我更喜欢在下面使用它来进行垂直对齐:

// set the LayoutManager to GridLayout and use a Grid of 1 x buttons.length
p.setLayoutManager(new GridLayout(1, buttons.length));

感谢大家。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM