首页 > 解决方案 > 为什么改变窗口大小后,swing 应用的视口大小会发生变化?

问题描述

我正在尝试在我的摇摆应用程序中应用 scrollPane。
但是一旦我改变了窗口的大小,我就看不到我的滚动条了。

我知道滚动窗格中有 9 个组件:视口、2 个标题、2 个滚动条和 4 个角。但是当我将边框的颜色设置为红色(应用于视口)时,结果显示我的窗口中唯一的元素仅仅是视口。(因为红色边框在窗口周围。)

这是我的代码。

package swingDemo;

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

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;;

public class ScrollDemo1 extends JFrame{

    public ScrollDemo1() {
        JLabel label = new JLabel();
        label.setBounds(50,0,1000,100);
        label.setText("Do you want to have a coffee together?");

        JButton button = new JButton("Of course!");
        button.setBounds(10,60,150,50);
        button.addActionListener(e -> label.setText("Well, let's go!"));
        JButton button2 = new JButton("No,sorry.");
        button2.setBounds(170,60,150,50);
        button2.addActionListener(e -> label.setText("Well, let's go!"));

        JPanel panel = new JPanel();
        panel.setBounds(10,10,10,10);
        panel.add(button2);
        panel.add(button);
        panel.add(label);
        panel.setLayout(null);

        JScrollPane scrollPane = new JScrollPane(panel);
        scrollPane.setViewportBorder(BorderFactory.createLineBorder(Color.red));
        scrollPane.setPreferredSize(new Dimension(20,20));
        add(scrollPane);
        scrollPane.setViewportView(panel);
        setBounds(10,10,40,60);
        setTitle("Dating Robot");

    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        SwingUtilities.invokeLater(() -> {
            ScrollDemo1 obj = new ScrollDemo1();
            obj.setVisible(true);
        });

    }

}

我想看看滚动条。有人请吗?谢谢你!

标签: javaswingscrollbar

解决方案


代码有一些问题,这个例子应该可以解决其中的一些问题。

主要问题是由于将布局管理器设置为null. 当滚动窗格试图获取包含的面板的大小时,它无法得到一个好的答案,因此它无法显示滚动条。

您可能已经注意到,实际上一切都在屏幕上,只是都被挤压了。通过设置大小,一切都是可见的。

通过注释掉setLayout(null),我可能破坏了您想要的视觉布局,因此请根据口味进行调整。不过,最好使用其中一种布局管理器。

调用scrollPane.setViewportView(panel)是不必要的,因为调用new JScrollPane(panel). 这可能是良性的,但正是这些冗余调用有时会导致 Swing 怪异,因为 Swing 经常在幕后注册侦听器等。

最后,我添加了一个调用,setDefaultCloseOperation以便应用程序在单击窗口关闭按钮时退出。

我希望这有帮助!

package swingDemo;

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

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

;

public class ScrollDemo1 extends JFrame {

  public ScrollDemo1() {
    JLabel label = new JLabel();
    label.setBounds(50, 0, 1000, 100);
    label.setText("Do you want to have a coffee together?");

    JButton button = new JButton("Of course!");
    button.setBounds(10, 60, 150, 50);
    button.addActionListener(e -> label.setText("Well, let's go!"));
    JButton button2 = new JButton("No,sorry.");
    button2.setBounds(170, 60, 150, 50);
    button2.addActionListener(e -> label.setText("Well, let's go!"));

    JPanel panel = new JPanel();
    panel.setBounds(10, 10, 10, 10);
    panel.add(button2);
    panel.add(button);
    panel.add(label);
    // This causes problems:
//    panel.setLayout(null);

    JScrollPane scrollPane = new JScrollPane(panel);
    scrollPane.setViewportBorder(BorderFactory.createLineBorder(Color.red));
    scrollPane.setPreferredSize(new Dimension(20, 20));
    add(scrollPane);
    // This is redundent:
//    scrollPane.setViewportView(panel);
    setBounds(10, 10, 40, 60);
    setTitle("Dating Robot");
    // Some initial size (or "pack") is needed to make it big enough to see
    setSize(500, 500);
    // This makes the app exit cleanly when closing the frame
    this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
  }

  public static void main(String[] args) {
    // TODO Auto-generated method stub
    SwingUtilities.invokeLater(() -> {
      ScrollDemo1 obj = new ScrollDemo1();
      obj.setVisible(true);
    });
  }
}

推荐阅读