JScrollPane屏幕撕裂

时间:2014-06-18 13:03:44

标签: java swing jscrollpane

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

以下是正在发生的事情的屏幕截图(第一张图片是加载视图的时候,第二张图片是我滚动后发生的事情)。显示名称和用户名,以保护隐私,但您仍然可以通过查看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;
    }
  }
}

2 个答案:

答案 0 :(得分:2)

  

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

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

有关更多信息和可能的解决方案,请参阅Background With Transparency

答案 1 :(得分:0)

通过添加行mainPanel.setBackground(new Color(0xF5F5F5));来修复它。奇怪的错误。