请问如何在保证JFrame有根据里面的内容的自动缩放功能的前提下,为JFrame内的内容添加一个垂直滚动条(就像qq游戏大厅的坐位)
程序如下:
import java.awt.*;
import javax.swing.*;
public class TestFlowLayout
{
JPanel panel = new JPanel();
JFrame frame = new JFrame();
public void init()
{
//当取消下面这句时,JFrame可以根据内容自动缩放(?不知为何)
//frame.setLayout(new FlowLayout());
for(int i = 0; i < 500; i++)
{
JLabel label = new JLabel("test" + i);
label.setPreferredSize(new Dimension(50, 50));
panel.add(label);
}
/*当使用JScrollPane添加panel时,JFrame就失去了根据内容自动缩放的功能(?不知为何)
JScrollPane scrollPane = new JScrollPane(panel);
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
frame.add(scrollPane);
*/
frame.add(panel);
frame.setPreferredSize(new Dimension(400, 400));
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args)
{
new TestFlowLayout().init();
}
}
程序如下:
import java.awt.*;
import javax.swing.*;
public class TestFlowLayout
{
JPanel panel = new JPanel();
JFrame frame = new JFrame();
public void init()
{
//当取消下面这句时,JFrame可以根据内容自动缩放(?不知为何)
//frame.setLayout(new FlowLayout());
for(int i = 0; i < 500; i++)
{
JLabel label = new JLabel("test" + i);
label.setPreferredSize(new Dimension(50, 50));
panel.add(label);
}
/*当使用JScrollPane添加panel时,JFrame就失去了根据内容自动缩放的功能(?不知为何)
JScrollPane scrollPane = new JScrollPane(panel);
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
frame.add(scrollPane);
*/
frame.add(panel);
frame.setPreferredSize(new Dimension(400, 400));
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args)
{
new TestFlowLayout().init();
}
}
import javax.swing.*;public class TestFlowLayout {
JPanel panel = new JPanel(); JFrame frame = new JFrame(); public void init() {
panel.setLayout(new GridLayout(50,10));
for (int i = 0; i < 500; i++) {
JLabel label = new JLabel("test" + i);
label.setPreferredSize(new Dimension(50, 50));
panel.add(label);
}
JScrollPane scrollpane = new JScrollPane(
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); scrollpane.getViewport().setView(panel);
frame.add(scrollpane); frame.setPreferredSize(new Dimension(400, 400));
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
} public static void main(String[] args) {
new TestFlowLayout().init();
}
}
你好
首先谢谢你的回复
不过你可能误会了我的意思
我要的是qq游戏大厅中那些坐位样的,可以根据主窗体的宽度的大小而自由缩进的效果
比如大厅中的坐位这样编排
1 2 3 4 5
6 7 8 9 10
。。
然后当大厅的宽度减小时,同一行中当然不能再容纳5个坐位,这是这样坐位就能自动缩进
1 2 3 4
5 6 7 8
9 10.
我要的是这效果,不知有无办法