我写使用swing.So以后会有一个JScrollPane加到JPanel的N,然后我将在verticle方式如何设置在JPanel上添加的按钮的大小和边界?
JPanel panel=new JPanel();
panel.setBackground(Color.WHITE);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;
int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS;
JScrollPane jsp=new JScrollPane(panel,v,h);
jsp.setPreferredSize(new Dimension(600,600));
jsp.setBounds(150,670,850,200);
frame.add(jsp);
然后我添加按钮添加固定大小的按钮的JPanel到它在Java代码在运行时。
for(i=0;i<n;i++)
{
button[i]=new JButton();
button[i].setBounds(20,y,120,120);
button[i].setSize(120,120);
button[i].setToolTipText(file[i].toString());
button[i].setIcon(Icon);
panel.add(button[i]);
y=y+140; //initially y=20 so 1st button on x=20,y=20 2nd button on x=20,160
}
我想添加一个按钮,一个下方的另一...(即我得到一个verticle滚动条)
即Button1的
button2
'
'
问题是大小和按钮的边界我设置使用setsize/preffered大小和setbounds是不影响大小和位置按钮(这是在面板上添加)...
该怎么办? 任何人都可以帮助我?
你在这个主题的原始发帖中给出了答案,但尚未回复,所以我不会在这里浪费时间。答案依然如此,请使用合适的布局管理器。使用setSize()和setBounds()“不会”工作。 – camickr 2010-02-18 16:17:29