gpt4 book ai didi

java - JScrollPane 屏幕撕裂

转载 作者:行者123 更新时间:2023-11-30 09:06:46 24 4
gpt4 key购买 nike

我正在尝试将 JScrollPane 添加到 JPanel,后者将添加到 JFrame。但是,当我向下滚动 JScrollPane 时,JPanel 开始撕裂。

以下是所发生情况的屏幕截图(第一张图片是加载 View 时的情况,第二张是我滚动后发生的情况)。显示名称和用户名已涂黑以保护隐私,但您仍然可以通过查看 actionButton 来了解发生了什么:

enter image description here enter image description here

代码如下:

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

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.EmptyBorder;

public class SSCCE extends JFrame {
public static void main(String[] args) {
SSCCE test = new SSCCE();
}

private final EmptyBorder padding = new EmptyBorder(0, 10, 0, 0);

public SSCCE() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// this.setUndecorated(true);
this.setBackground(new Color(1.0f, 1.0f, 1.0f, 0.0f));
this.setPreferredSize(new Dimension(300, 400));
this.setResizable(false);

JPanel container = new JPanel();
BoxLayout boxLayout2 = new BoxLayout(container, BoxLayout.X_AXIS);
container.setLayout(boxLayout2);
container.add(Box.createHorizontalGlue());

this.add(container, BorderLayout.WEST);
this.add(build());

this.pack();
this.setLocationRelativeTo(null);
this.setVisible(true);
}

public JPanel build() {
JPanel superPanel = new JPanel();
superPanel.setBackground(new Color(1.0f, 1.0f, 1.0f, 0.0f));
BoxLayout boxLayout4 = new BoxLayout(superPanel, BoxLayout.Y_AXIS);
superPanel.setLayout(boxLayout4);
superPanel.add(Box.createVerticalGlue());
superPanel.setPreferredSize(new Dimension(300, 750));

JPanel listContainer = new JPanel();
BoxLayout boxLayout1 = new BoxLayout(listContainer, BoxLayout.Y_AXIS);
listContainer.setLayout(boxLayout1);
listContainer.add(Box.createVerticalGlue());

Friend[] friendList = new Friend[] { new Friend("test1", "Test1"),
new Friend("test2", "Test2"), new Friend("test3", "Test3"),
new Friend("test4", "Test4"), new Friend("test5", "Test5"),
new Friend("test6", "Test6"), new Friend("test7", "Test7"),
new Friend("test8", "Test8"), new Friend("test9", "Test9"),
new Friend("test10", "Test10"), new Friend("test11", "Test11"),
new Friend("test12", "Test12"), new Friend("test13", "Test") };

for (int i = 0; i < friendList.length; i++) {
JPanel mainPanel = new JPanel();
JPanel renderer = new JPanel();

final Friend f = friendList[i];

final JTextPane username = new JTextPane();

final JTextPane displayName = new JTextPane();
// displayName.setHighlighter(null);
displayName.setEditable(true);
displayName.setText(f.getDisplayName());
displayName.setForeground(new Color(0x000000));
displayName.setVisible(true);
displayName.setBorder(this.padding);

JButton actionButton = new JButton();
actionButton.setPreferredSize(new Dimension(40, 40));

username.setHighlighter(null);
username.setEditable(false);
username.setText(f.getUsername());
username.setBorder(this.padding);
username.setVisible(true);
if (!f.getDisplayName().equals(f.getUsername()))
username.setForeground(new Color(0x666666));
else
username.setForeground(new Color(0xF5F5F5));

renderer.setBackground(new Color(0xF5F5F5));
renderer.setPreferredSize(new Dimension(300, 50));

mainPanel.add(actionButton, BorderLayout.EAST);

BoxLayout boxLayout2 = new BoxLayout(renderer, BoxLayout.Y_AXIS);
renderer.setLayout(boxLayout2);
renderer.add(Box.createVerticalGlue());
renderer.add(displayName, BorderLayout.EAST);
renderer.add(username, BorderLayout.EAST);
mainPanel.add(renderer, BorderLayout.CENTER);

listContainer.add(mainPanel);
}
JScrollPane scrollPane = new JScrollPane();
scrollPane.setPreferredSize(new Dimension(300, 750));
scrollPane.setViewportView(listContainer);
scrollPane
.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.getVerticalScrollBar().setUnitIncrement(10);

superPanel.add(scrollPane);

superPanel.setVisible(true);
return superPanel;
}

private static class Friend {
private String displayName;
private String userName;

public Friend(String un, String dn) {
this.displayName = dn;
this.userName = un;
}

public String getUsername() {
return this.userName;
}

public String getDisplayName() {
return this.displayName;
}
}
}

最佳答案

this.setBackground(new Color(1.0f, 1.0f, 1.0f, 0.0f));

您已将背景设置为包含透明色。这会导致重绘出现问题。

参见 Background With Transparency了解更多信息和可能的解决方案。

关于java - JScrollPane 屏幕撕裂,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24286357/

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