当前位置: 首页 > 知识库问答 >
问题:

JScrollPane在窗口上收缩/折叠最小化

山越
2023-03-14

我有一个JScrollPane,其中包含一个JTextArea。当窗口最小化然后恢复时,JScrollPane将自行崩溃。注意,只有当JTextArea中的文本超过JTextArea的给定宽度和/或高度(即,出现水平或垂直滚动条)时,才会发生挤压。

这里的问题:JScrollpane在最小化后失去大小会带来同样的问题,但是这个问题从未得到解决,除了向JScrollPane添加权重、权重和填充约束,我已经不得不开始了。

下面是一个简单的示例,演示了这个问题。在最小化并恢复窗口后,如何使JScrollPane保持其大小?

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.ExecutionException;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
import javax.swing.border.EtchedBorder;

public class GUITest implements ActionListener {

    JButton button = new JButton("Button");
    JTextArea textArea = new JTextArea();
    SwingWorker<String, String> mySwingWorker = null;

    public static void main(String[] args) throws IOException {
        GUITest tracer = new GUITest();
    }

    public GUITest() throws IOException {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                try {
                    createAndShowGUI();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void createAndShowGUI() throws IOException {
        JFrame frame = new JFrame("GUI Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new GridBagLayout());
        GridBagConstraints mainPanelConstraints = new GridBagConstraints();
        mainPanelConstraints.gridx = 0;
        mainPanelConstraints.gridy = 0;
        mainPanelConstraints.fill = GridBagConstraints.BOTH;
        button.addActionListener(this);
        mainPanel.add(button, mainPanelConstraints);
        mainPanelConstraints.gridx = 0;
        mainPanelConstraints.gridy = 1;
        mainPanelConstraints.fill = GridBagConstraints.BOTH;
        mainPanel.add(buildTextAreaPanel(), mainPanelConstraints);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setVisible(true);
    }

    private JPanel buildTextAreaPanel() {
        JPanel textAreaPanel = new JPanel();
        textAreaPanel.setLayout(new GridBagLayout());
        GridBagConstraints textAreaPanelConstraints = new GridBagConstraints();
        textAreaPanel.setBorder(BorderFactory.createTitledBorder(new EtchedBorder(EtchedBorder.RAISED), "TextArea"));
        textArea.setColumns(30);
        textArea.setRows(15);
        textArea.setEditable(false);
        JScrollPane textAreaScrollPane = new JScrollPane(textArea);
        textAreaScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        textAreaScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        textAreaPanelConstraints.gridx = 0;
        textAreaPanelConstraints.gridy = 0;
        textAreaPanelConstraints.weightx = 1.0;
        textAreaPanelConstraints.weighty = 1.0;
        textAreaPanelConstraints.fill = GridBagConstraints.BOTH;
        textAreaPanel.add(textAreaScrollPane, textAreaPanelConstraints);
        return textAreaPanel;
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == button) {
            mySwingWorker = new MySwingWorker();
            mySwingWorker.execute();
        }
    }

    private class MySwingWorker extends SwingWorker<String, String> {
        public String doInBackground() throws Exception {
            for (int i = 0; i < textArea.getRows(); i++) {
                publish("text\n");
            }
            publish("more text\n");

            return "Done.";
        }
        public void process(List<String> chunks) {
            for (String msg : chunks) {
                textArea.append(msg);
            }
        }   
        public void done() {
            try {
                String msg = get();
                textArea.append("\n" + msg);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }

    }

}

共有3个答案

慕铭
2023-03-14

我通过删除所有GridBagLayouts并将其替换为BorderLayouts,解决了这个特定代码示例中的收缩问题。我不知道为什么JScrollPane会像您在使用GridBagLayout时发现的那样反应。

从类构造函数启动事件调度线程时,我感到非常不舒服。我将SwingUtilities调用器移到了主方法中。

无论如何,这是代码。

package com.ggl.testing;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.ExecutionException;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
import javax.swing.border.EtchedBorder;

public class JScrollPaneTest implements ActionListener {

    JButton button = new JButton("Button");
    JTextArea textArea = new JTextArea();
    SwingWorker<String, String> mySwingWorker = null;

    public static void main(String[] args) throws IOException {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                try {
                    new JScrollPaneTest().createAndShowGUI();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

    }

    public JScrollPaneTest() throws IOException {

    }

    public void createAndShowGUI() throws IOException {
        JFrame frame = new JFrame("JScrollPane Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(true);

        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BorderLayout());
        mainPanel.add(button, BorderLayout.NORTH);
        mainPanel.add(buildTextAreaPanel(), BorderLayout.CENTER);

        button.addActionListener(this);

        frame.add(mainPanel, BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
    }

    private JPanel buildTextAreaPanel() {
        JPanel textAreaPanel = new JPanel();
        textAreaPanel.setLayout(new BorderLayout());
        textAreaPanel.setBorder(BorderFactory.createTitledBorder(
                new EtchedBorder(EtchedBorder.RAISED), "TextArea"));

        textArea.setColumns(30);
        textArea.setRows(15);
        textArea.setEditable(false);

        JScrollPane textAreaScrollPane = new JScrollPane(textArea);

        textAreaPanel.add(textAreaScrollPane, BorderLayout.CENTER);
        return textAreaPanel;
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == button) {
            mySwingWorker = new MySwingWorker();
            mySwingWorker.execute();
        }
    }

    private class MySwingWorker extends SwingWorker<String, String> {
        public String doInBackground() throws Exception {
            for (int i = 0; i < textArea.getRows(); i++) {
                publish("text\n");
            }
            publish("more text\n");

            return "Done.";
        }

        public void process(List<String> chunks) {
            for (String msg : chunks) {
                textArea.append(msg);
            }
        }

        public void done() {
            try {
                String msg = get();
                textArea.append("\n" + msg);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }

    }

}
欧桐
2023-03-14

将父容器(内容窗格)的布局设置为FlowLayout

...
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(mainPanel);
...
邬阳
2023-03-14

您需要在两种布局中设置权重X和权重Y。在createAndShowGui中,设置mainPanelConstraints。权重X和主面板约束。在添加文本区域面板之前,将weight设置为1。

 类似资料:
  • 我正在尝试在CSS flexbox中,将两个相邻的绘图拟合到一起。我希望它们在调整窗口大小时调整大小。当窗口变大时,它会按预期工作,但当窗口变小时,绘图不会缩小。 JSFiddle:https://jsfiddle.net/bd0reepd/ 我可能误解了flexbox的工作原理,但如果我用图像替换绘图,它们会像预期的那样缩小。 我试着调试它,发现plotlys的内部函数,它调用并相应地设置打印大

  • 问题内容: 我有以下JQuery代码: 唯一的问题是,这仅在首次加载浏览器时有效,我是否还希望在调整窗口大小时进行检查? 有任何想法吗? 问题答案: 这是一个使用jQuery,javascript和css处理调整大小事件的示例。 (如果您只是通过调整大小来设置样式(媒体查询),则最好的方法是CSS) CSS javascript jQuery 如何停止调整大小的代码执行如此频繁! 这是绑定大小调整

  • 我是Javafx新手,我正在从事Javafx项目。我想最小化javafx窗口。我使用初级阶段。initStyle(舞台风格。未装饰)。我还想将clickevent添加到fxml中的imageview中。

  • Collapsibles 是可折叠元素,扩大时,点击。他们允许您隐藏不立即与用户相关的内容。 气孔 创建一个气孔式可折叠组件,只要增加类 popout。 <ul class="collapsible popout" data-collapsible="accordion"> 可折叠组件的 HTML 结构 <ul class="collapsible" data-collapsible="accor

  • 如果你仅仅希望看到你所处理的代码文件的结构概览,折叠会是个非常有用的工具。折叠可以隐藏像函数和循环这样的代码块,来简化你屏幕上显示的东西。 当你把鼠标移到数字栏上,你就可以点击显示的箭头来折叠代码段。你也可以使用快捷键alt-cmd-[和alt-cmd-]来折叠和展开代码段。 使用alt-cmd-shift-{来折叠所有代码段,使用alt-cmd-shift-}来展开所有代码段。你也可以使用cmd

  • zf数字 创建折叠,数字参数为折叠行数 zo 打开折叠 zc 关闭折叠 zd 删除折叠 :set foldcolumn=4 显示折叠树 :mkview 保存折叠 :loadview 读取折叠 zr 打开同一层级所有折叠 zm 关闭同一层级所有折叠 zO 打开某一行的所有层级折叠 zC 关闭某一行的所有层级折叠 zR 打开所有折叠 zM 关闭所有折叠 :set foldclose=all 光标离开折