gpt4 book ai didi

java - JScrollPane、JTextArea 和 JPanel 的问题

转载 作者:行者123 更新时间:2023-12-03 22:20:26 25 4
gpt4 key购买 nike

我需要向我的 JPanel 添加一个垂直滚动条。当长文本字符串没有正确换行等时,问题就出现了。所以我按照在线步骤将它放在文本区域并且它工作正常(没有滚动 Pane )。

然而,在将 JScrollpane(它应该只是垂直的,请假设它是必需的,即不能设置最小尺寸)添加到 JPanel 后,当我水平调整它的大小时,它完全搞砸了。它不会在调整大小时按应有的方式换行,并且其他组件的位置会困惑。

这可能看起来很奇怪,我在这里有不必要的面板,但是我正在作为一个更大的程序的一部分工作并且需要这些面板(for 循环仅用于测试目的)。

总结一下:在水平调整窗口大小时,它的行为与预期不符。

希望得到一些指导。

import java.awt.BorderLayout;
import java.awt.GridLayout;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;

public class Main {

public static void main(String[] args) {

String s = "Hello my name is test. I am a test. Hello my name is test. I am a test. Hello my name is test. I am a test. Hello my name is test. I am a test. ";
JFrame jf = new JFrame("Test");
jf.setSize(600, 200);

JPanel big = new JPanel();
big.setLayout(new BoxLayout(big, BoxLayout.Y_AXIS));

for (int i = 0; i < 2; i++) {
JPanel first = new JPanel(new BorderLayout());

JPanel insideTop = new JPanel(new GridLayout(2, 2));
insideTop.add(new JLabel("test"));
insideTop.add(new JLabel("test"));
insideTop.add(new JLabel("test"));
insideTop.add(new JLabel("test"));

JPanel insideMiddle = new JPanel(new BorderLayout());
insideMiddle.add(new JLabel("Description"), BorderLayout.NORTH);
JTextArea jta = new JTextArea(s);
jta.setWrapStyleWord(true);
jta.setLineWrap(true);
jta.setEditable(false);
jta.setFocusable(false);
jta.setOpaque(false);
insideMiddle.add(jta, BorderLayout.CENTER);

JPanel insideBottom = new JPanel(new BorderLayout());
insideBottom.add(new JLabel("Bottom left"), BorderLayout.WEST);
insideBottom.add(new JButton("Bottom right"), BorderLayout.EAST);

first.add(insideTop, BorderLayout.NORTH);
first.add(insideMiddle, BorderLayout.CENTER);
first.add(insideBottom, BorderLayout.SOUTH);
big.add(first);
}

JScrollPane scrollPane = new JScrollPane(big, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

jf.add(scrollPane);
jf.setVisible(true);

}
}

最佳答案

部分问题是,一旦允许组件扩展,它就不想收缩,因此您需要一些方法来限制组件始终为可视区域的宽度。

幸运的是,有一种相对简单的方法可以使用 Scrollable 接口(interface)来实现...

public class BigPane extends JPanel implements Scrollable {

@Override
public Dimension getPreferredScrollableViewportSize() {
return new Dimension(200, 200);
}

@Override
public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
return 128;
}

@Override
public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
return 128;
}

@Override
public boolean getScrollableTracksViewportWidth() {
return true;
}

@Override
public boolean getScrollableTracksViewportHeight() {
boolean track = true;
Container parent = getParent();
if (parent instanceof JViewport) {

JViewport viewport = (JViewport) parent;
if (viewport.getHeight() < getPreferredSize().height) {
track = false;
}

}

return track;
}

}

这基本上是让组件始终遵循 JViewport 的宽度,因此当 JScrollPane/JViewport 执行它们的操作时布局,他们知道将组件的宽度限制为可视区域的宽度。因为我喜欢它以这种方式工作,所以当组件的首选高度小于可视区域时,它也会填充空间,但您可以对此做出自己的决定...

Scrollable

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Rectangle;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JViewport;
import javax.swing.ScrollPaneConstants;
import javax.swing.Scrollable;

public class Main {

public static void main(String[] args) {

String s = "Hello my name is test. I am a test. Hello my name is test. I am a test. Hello my name is test. I am a test. Hello my name is test. I am a test. ";
JFrame jf = new JFrame("Test");
jf.setSize(600, 200);

BigPane big = new BigPane();
big.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.BOTH;

for (int i = 0; i < 2; i++) {
JPanel first = new JPanel(new BorderLayout());

JPanel insideTop = new JPanel(new GridLayout(2, 2));
insideTop.add(new JLabel("test"));
insideTop.add(new JLabel("test"));
insideTop.add(new JLabel("test"));
insideTop.add(new JLabel("test"));

JPanel insideMiddle = new JPanel(new BorderLayout());
insideMiddle.add(new JLabel("Description"), BorderLayout.NORTH);
JTextArea jta = new JTextArea(s);
jta.setWrapStyleWord(true);
jta.setLineWrap(true);
jta.setEditable(false);
jta.setFocusable(false);
jta.setOpaque(false);
insideMiddle.add(jta, BorderLayout.CENTER);

JPanel insideBottom = new JPanel(new BorderLayout());
insideBottom.add(new JLabel("Bottom left"), BorderLayout.WEST);
insideBottom.add(new JButton("Bottom right"), BorderLayout.EAST);

first.add(insideTop, BorderLayout.NORTH);
first.add(insideMiddle, BorderLayout.CENTER);
first.add(insideBottom, BorderLayout.SOUTH);
big.add(first, gbc);
}

JScrollPane scrollPane = new JScrollPane(big, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

jf.add(scrollPane);
jf.setVisible(true);

}

public static class BigPane extends JPanel implements Scrollable {

@Override
public Dimension getPreferredScrollableViewportSize() {
return new Dimension(200, 200);
}

@Override
public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
return 128;
}

@Override
public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
return 128;
}

@Override
public boolean getScrollableTracksViewportWidth() {
return true;
}

@Override
public boolean getScrollableTracksViewportHeight() {
boolean track = true;
Container parent = getParent();
if (parent instanceof JViewport) {

JViewport viewport = (JViewport) parent;
if (viewport.getHeight() < getPreferredSize().height) {
track = false;
}

}

return track;
}

}
}

我还切换到 GridBagLayout,同样,这是个人选择,您可能会发现 BoxLayout 适合您

关于java - JScrollPane、JTextArea 和 JPanel 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35443272/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com