Java的JPanel(流式布局)添加到JScrollPane滚动窗格后垂直滚动条不正常且流式布局特点没了?求解答。
我是在JPanel中加入多个矩形的JPanel(作为图片大图标查看),整个JPanel采用流式布局后添加到了JScrollPane中,最后又把JScrollPane加到了...
我是在JPanel中加入多个矩形的JPanel(作为图片大图标查看),整个JPanel采用流式布局后添加到了JScrollPane中,最后又把JScrollPane加到了JFrame上。结果如图,图片没显示完整滚动条就失效了(这里的滚动条我设置了一直显示),而且窗口的流式布局特点也没了。
刚发现一个问题:就是JScrollPane滚动条不完整的原因是整个JPanel给我setPreferredSize了,但是如果我不这么做的话,整个窗口就只会显示一行(横向)图片,横向滚动条正常了,图片布局就不正常了。 展开
刚发现一个问题:就是JScrollPane滚动条不完整的原因是整个JPanel给我setPreferredSize了,但是如果我不这么做的话,整个窗口就只会显示一行(横向)图片,横向滚动条正常了,图片布局就不正常了。 展开
展开全部
额,不知道这样是你要的不:
首先你的jscrollPane要这样设置:
JscrollPane.getHorizontalScrollBar().setAutoscrolls(false);
JscrollPane.getVerticalScrollBar().setAutoscrolls(true);
这样你的滚动条就只会显示竖线。
接下来是关键:
将jp.setlayout(new ModifiedFlowLayout());这个布局是继承flowlayout的。
也就是说将布局改为下面这个布局就可以了:
import java.awt.*;
public class ModifiedFlowLayout extends FlowLayout{
public ModifiedFlowLayout(){
super();
}
public ModifiedFlowLayout(int align){
super(align);
}
public ModifiedFlowLayout(int align,int hgap,int vgap){
super(align, hgap, vgap);
}
public Dimension minimumLayoutSize(Container target){
// Size of largest component, so we can resize it in
// either direction with something like a split-pane.
return computeMinSize(target);
}
public Dimension preferredLayoutSize(Container target){
return computeSize(target);
}
private Dimension computeSize(Container target){
synchronized(target.getTreeLock()){
int hgap = getHgap();
int vgap = getVgap();
int w = target.getWidth();
// Let this behave like a regular FlowLayout (single row)
// if the container hasn't been assigned any size yet
if(w ==0){
w =Integer.MAX_VALUE;
}
Insets insets = target.getInsets();
if(insets ==null){
insets =newInsets(0,0,0,0);
}
int reqdWidth =0;
int maxwidth = w -(insets.left + insets.right + hgap *2);
int n = target.getComponentCount();
int x =0;
int y = insets.top + vgap;// FlowLayout starts by adding vgap, so do that here too.
int rowHeight =0;
for(int i =0; i < n; i++){
Component c = target.getComponent(i);
if(c.isVisible()){
Dimension d = c.getPreferredSize();
if((x ==0)||((x + d.width)<= maxwidth)){
// fits in current row.
if(x >0){
x += hgap;
}
x += d.width;
rowHeight =Math.max(rowHeight, d.height);
}
else{
// Start of new row
x = d.width;
y += vgap + rowHeight;
rowHeight = d.height;
}
reqdWidth =Math.max(reqdWidth, x);
}
}
y += rowHeight;
y += insets.bottom;
return new Dimension(reqdWidth+insets.left+insets.right, y);
}
}
private Dimension computeMinSize(Container target){
synchronized(target.getTreeLock()){
int minx =Integer.MAX_VALUE;
int miny =Integer.MIN_VALUE;
boolean found_one =false;
int n = target.getComponentCount();
for(int i =0; i < n; i++){
Component c = target.getComponent(i);
if(c.isVisible()){
found_one =true;
Dimension d = c.getPreferredSize();
minx =Math.min(minx, d.width);
miny =Math.min(miny, d.height);
}
}
if(found_one){
return new Dimension(minx, miny);
}
return new Dimension(0,0);
}
}
}
首先你的jscrollPane要这样设置:
JscrollPane.getHorizontalScrollBar().setAutoscrolls(false);
JscrollPane.getVerticalScrollBar().setAutoscrolls(true);
这样你的滚动条就只会显示竖线。
接下来是关键:
将jp.setlayout(new ModifiedFlowLayout());这个布局是继承flowlayout的。
也就是说将布局改为下面这个布局就可以了:
import java.awt.*;
public class ModifiedFlowLayout extends FlowLayout{
public ModifiedFlowLayout(){
super();
}
public ModifiedFlowLayout(int align){
super(align);
}
public ModifiedFlowLayout(int align,int hgap,int vgap){
super(align, hgap, vgap);
}
public Dimension minimumLayoutSize(Container target){
// Size of largest component, so we can resize it in
// either direction with something like a split-pane.
return computeMinSize(target);
}
public Dimension preferredLayoutSize(Container target){
return computeSize(target);
}
private Dimension computeSize(Container target){
synchronized(target.getTreeLock()){
int hgap = getHgap();
int vgap = getVgap();
int w = target.getWidth();
// Let this behave like a regular FlowLayout (single row)
// if the container hasn't been assigned any size yet
if(w ==0){
w =Integer.MAX_VALUE;
}
Insets insets = target.getInsets();
if(insets ==null){
insets =newInsets(0,0,0,0);
}
int reqdWidth =0;
int maxwidth = w -(insets.left + insets.right + hgap *2);
int n = target.getComponentCount();
int x =0;
int y = insets.top + vgap;// FlowLayout starts by adding vgap, so do that here too.
int rowHeight =0;
for(int i =0; i < n; i++){
Component c = target.getComponent(i);
if(c.isVisible()){
Dimension d = c.getPreferredSize();
if((x ==0)||((x + d.width)<= maxwidth)){
// fits in current row.
if(x >0){
x += hgap;
}
x += d.width;
rowHeight =Math.max(rowHeight, d.height);
}
else{
// Start of new row
x = d.width;
y += vgap + rowHeight;
rowHeight = d.height;
}
reqdWidth =Math.max(reqdWidth, x);
}
}
y += rowHeight;
y += insets.bottom;
return new Dimension(reqdWidth+insets.left+insets.right, y);
}
}
private Dimension computeMinSize(Container target){
synchronized(target.getTreeLock()){
int minx =Integer.MAX_VALUE;
int miny =Integer.MIN_VALUE;
boolean found_one =false;
int n = target.getComponentCount();
for(int i =0; i < n; i++){
Component c = target.getComponent(i);
if(c.isVisible()){
found_one =true;
Dimension d = c.getPreferredSize();
minx =Math.min(minx, d.width);
miny =Math.min(miny, d.height);
}
}
if(found_one){
return new Dimension(minx, miny);
}
return new Dimension(0,0);
}
}
}
展开全部
可以贴代码吗?
这样看,不能很好的找到问题
JScrollPane,中放一个主PANEL,是流式布局
然后其它的JPanel添加到这个,主PANEL上是吗?
这样看,不能很好的找到问题
JScrollPane,中放一个主PANEL,是流式布局
然后其它的JPanel添加到这个,主PANEL上是吗?
追问
追答
在你的代码中改了一下,主要是这一句
jp.setPreferredSize(new Dimension(jsp.getWidth(), 150 * 21));
用panel的setPreferredSize来控制一下容器的大小,
---------------------------------------------------
又给你改了一下,需要用侦听事件,这样你以前的主是不很方便了,又写了一个,你看一下吧。
---------------------------------------------------
import java.awt.Container;
public class AppFrame extends JFrame implements ComponentListener {
private JButton[] jbt = new JButton[100];
private JPanel panel = new JPanel();
private JScrollPane scrollPane = new JScrollPane();
private int len = 150;
public AppFrame() {
Container containerPane = getContentPane();
containerPane.setLayout(new BoxLayout(containerPane, BoxLayout.Y_AXIS));
scrollPane = new JScrollPane();
scrollPane.setBounds(12, 10, 816, 433);
getContentPane().add(scrollPane);
scrollPane.setViewportView(panel);
for (int i = 0; i < jbt.length; i++) {
jbt[i] = new JButton("OK" + i);
jbt[i].setPreferredSize(new Dimension(len, len));
panel.add(jbt[i]);
}
containerPane.add(scrollPane);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(848, 480);
setLocationRelativeTo(null);
this.addComponentListener(this);
}
public static void main(String[] args) {
new AppFrame();
}
public void componentHidden(ComponentEvent arg0) {
}
public void componentMoved(ComponentEvent arg0) {
}
public void componentResized(ComponentEvent arg0) {
// 竖滚动条的宽是20,减去以后,就没有横向滚动条了。
int width = scrollPane.getWidth() - 20;
int number = width / len;
int row = jbt.length / number + 1;
int height = len * row;
int intr = 0;
if (jbt.length % number != 0) {
intr = 100;
}
height += intr;
panel.setPreferredSize(new Dimension(width, height));
repaint();
}
public void componentShown(ComponentEvent arg0) {
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询