在JScrollPane中使用绝对布局 [英] Use a absolute layout inside a JScrollPane

查看:313
本文介绍了在JScrollPane中使用绝对布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用具有绝对layoute的JScrollPane.我知道完全不建议使用setLayout(null).我一直在阅读,如果您想对JScrollPane使用绝对布局,则必须设置内部元素的首选size属性,以便JScrollPane可以计算其大小.

I need to use a JScrollPane with absolute layoute. I know that the use of setLayout(null) is not recommendable at all. I've been reading that if you want to use the absolute layout with a JScrollPane it is necessary to set the preferred size property of the elements inside in order to JScrollPane can calculate its size.

我一直在尝试下一个代码来更改元素的顺序和大小,但是我无法弄清哪里出错了.

I've been trying the next code changing the order and sizes of elemnts but I can't work out where I've been wrong.

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;


public class frame extends JFrame {

private JPanel contentPane;
private JScrollPane scrollPane;

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                frame frame = new frame();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

public frame() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setLayout(null);
    setContentPane(contentPane);

    JPanel panel = new JPanel();
    panel.setBackground(Color.red);
    panel.setBounds(0,0,600,600);
    panel.setPreferredSize(new Dimension(420,280));

    scrollPane = new JScrollPane(panel);
    scrollPane.setBounds(0, 0, 600, 600);
    scrollPane.setLayout(null);
    scrollPane.setBackground(Color.green);

    scrollPane.add(panel);
    scrollPane.setPreferredSize(new Dimension(450,300));
    contentPane.add(scrollPane);
}
}

为什么setPreferredSize工作不正确? 预先感谢.

Why setPreferredSize is not doing the right work? Thanks in advance.

推荐答案

更改实际JScrollPane的布局管理器没有任何意义;如果您希望在应用程序中使用绝对布局,则通常会在容器类(例如JPanel)上调用setLayout(null),以便使用绝对定位在其内定位子组件.

Changing the layout manager of the actual JScrollPane does not make sense; if you wish to use absolute layout within your application you would typically call setLayout(null) on a container class (such as JPanel) in order to position child components within it using absolute positioning.

我建议您尝试:

  • JScrollPane中包含的JPanel上调用setLayout(null)(我想 这就是您要执行的操作).
  • 不是直接使用JPanel,而是对其进行子类化,并让您的子类实现Scrollable接口,该接口向JScrollPane提供有关最佳可滚动视口大小的其他信息.
  • Call setLayout(null) on the JPanel contained within the JScrollPane (as I think this is what you're trying to do).
  • Rather than use JPanel directly, subclass it and have your subclass implement the Scrollable interface, which provides additional information to the JScrollPane regarding the optimal scrollable viewport size.

这篇关于在JScrollPane中使用绝对布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆