2013-03-12 61 views
0

我有一个相当简单的视图类,我正在构建过程中。我测试,因为我去,所以代码是很不完整的。但是,当我测试时,一切都很好,直到我滚动。如果通过单击滚动条上的空白区域一次滚动页面,则一切正常。如果我使用滚动箭头,拖动滚动条或使用鼠标滚轮,则新显示的内容将完全损坏。这发生在1.6.35和1.7.09两者。当我点击并拖动“日志行”(这是一个JTextField)时,我也注意到了这种情况。请告诉我我在这里做错了什么。代码应该按原样运行。JScrollPane整理内容

package com.mycompany.utility.logs; 

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.EventQueue; 
import java.awt.Font; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 

import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JSplitPane; 
import javax.swing.JTextField; 
import javax.swing.JTextPane; 
import javax.swing.ScrollPaneConstants; 
import javax.swing.border.EmptyBorder; 

/** 
* This class implements the log viewer view. 
*/ 
public class LogViewer extends JFrame 
{ 
    private static final long serialVersionUID = 1L; 
    private static final Color TRANSPARENT = new Color(255, 255, 255, 0); 
    private JPanel contentPane; 

    /** 
    * Launch the application. 
    */ 
    public static void main(String[] args) 
    { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() 
      { 
       try 
       { 
        LogViewer frame = new LogViewer(); 
        frame.setVisible(true); 
       } 
       catch (Exception e) 
       { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    /** 
    * Create the view. 
    */ 
    public LogViewer() 
    { 
     GridBagConstraints gridBagConstraints = null; 
     int row = 0; 

     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setBounds(100, 100, 713, 684); 
     contentPane = new JPanel(); 
     contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
     contentPane.setLayout(new BorderLayout(0, 0)); 
     setContentPane(contentPane); 

     JPanel topPanel = new JPanel(); 
     contentPane.add(topPanel, BorderLayout.NORTH); 
     GridBagLayout gbl_topPanel = new GridBagLayout(); 
     gbl_topPanel.columnWidths = new int[] { 0 }; 
     gbl_topPanel.rowHeights = new int[] { 0 }; 
     gbl_topPanel.columnWeights = new double[] { Double.MIN_VALUE }; 
     gbl_topPanel.rowWeights = new double[] { Double.MIN_VALUE }; 
     topPanel.setLayout(gbl_topPanel); 

     JLabel titleLabel = new JLabel("Tattle Tail Log Viewer"); 
     titleLabel.setFont(new Font("Lucida Sans", Font.BOLD, 12)); 
     gridBagConstraints = new GridBagConstraints(); 
     gridBagConstraints.gridy = 0; 
     gridBagConstraints.gridx = 0; 
     topPanel.add(titleLabel, gridBagConstraints); 

     JPanel bottomPanel = new JPanel(); 
     contentPane.add(bottomPanel, BorderLayout.SOUTH); 

     JSplitPane splitPane = new JSplitPane(); 
     splitPane.setResizeWeight(0.75); 
     splitPane.setOneTouchExpandable(true); 
     splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT); 
     contentPane.add(splitPane, BorderLayout.CENTER); 

     JPanel scrollPanel = new JPanel(); 
     scrollPanel.setBackground(Color.WHITE); 
     scrollPanel.setLayout(new GridBagLayout()); 

     JScrollPane scrollPane = new JScrollPane(); 
     scrollPane.setViewportView(scrollPanel); 
     scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); 
     splitPane.setLeftComponent(scrollPane); 

     final JTextPane textPane = new JTextPane(); 
     splitPane.setRightComponent(textPane); 
     textPane.setFont(new Font("Courier New", Font.PLAIN, 11)); 

     for (int i = 0; i < 25; i++) 
     { 
      addLogEntry(scrollPanel, textPane, row, 
        "2013-03-11 15:40:19,123 INFO com.mycompany.business.logic.ImportantProcess", 
        "Something of which you need to be aware happened " + i + "."); 
      row++; 
     } 

     JPanel fillPanel = new JPanel(); 
     fillPanel.setBackground(Color.LIGHT_GRAY); 
     gridBagConstraints = new GridBagConstraints(); 
     gridBagConstraints.gridx = 0; 
     gridBagConstraints.gridy = row; 
     gridBagConstraints.gridwidth = 2; 
     gridBagConstraints.weighty = 1D; 
     gridBagConstraints.fill = GridBagConstraints.BOTH; 
     scrollPanel.add(fillPanel, gridBagConstraints); 
    } 

    private void addLogEntry(final JPanel scrollPanel, final JTextPane textPane, final int row, final String logText, 
      final String messageText) 
    { 
     GridBagConstraints gridBagConstraints = null; 

     JPanel entryPanel = new JPanel(); 
     entryPanel.setBackground(TRANSPARENT); 
     entryPanel.setLayout(new GridBagLayout()); 
     gridBagConstraints = new GridBagConstraints(); 
     gridBagConstraints.gridx = 0; 
     gridBagConstraints.gridy = row; 
     gridBagConstraints.anchor = GridBagConstraints.NORTHWEST; 
     gridBagConstraints.weightx = 1D; 
     gridBagConstraints.weighty = 0D; 
     gridBagConstraints.fill = GridBagConstraints.HORIZONTAL; 
     scrollPanel.add(entryPanel, gridBagConstraints); 

     JPanel logLinePanel = new JPanel(); 
     logLinePanel.setBackground(TRANSPARENT); 
     logLinePanel.setFocusable(true); 
     logLinePanel.setLayout(new GridBagLayout()); 
     gridBagConstraints = new GridBagConstraints(); 
     gridBagConstraints.gridx = 0; 
     gridBagConstraints.gridy = 0; 
     gridBagConstraints.weightx = 1D; 
     gridBagConstraints.fill = GridBagConstraints.HORIZONTAL; 
     gridBagConstraints.anchor = GridBagConstraints.NORTHWEST; 
     entryPanel.add(logLinePanel, gridBagConstraints); 

     JLabel logLineLevelLabel = new JLabel(" "); 
     logLineLevelLabel.setOpaque(true); 
     logLineLevelLabel.setBackground(new Color(0, 128, 0)); 
     logLineLevelLabel.setFont(new Font("Courier New", Font.BOLD, 11)); 
     gridBagConstraints = new GridBagConstraints(); 
     gridBagConstraints.gridx = 0; 
     gridBagConstraints.gridy = 0; 
     gridBagConstraints.anchor = GridBagConstraints.NORTHWEST; 
     logLinePanel.add(logLineLevelLabel, gridBagConstraints); 

     JTextField logLineText = new JTextField(logText); 
     logLineText.setEditable(false); 
     logLineText.setBackground(TRANSPARENT); 
     logLineText.setBorder(null); 
     logLineText.setFont(new Font("Courier New", Font.PLAIN, 11)); 
     gridBagConstraints = new GridBagConstraints(); 
     gridBagConstraints.gridx = 1; 
     gridBagConstraints.gridy = 0; 
     gridBagConstraints.weightx = 1D; 
     gridBagConstraints.fill = GridBagConstraints.HORIZONTAL; 
     gridBagConstraints.anchor = GridBagConstraints.NORTHWEST; 
     logLinePanel.add(logLineText, gridBagConstraints); 

     JPanel messageLinePanel = new JPanel(); 
     messageLinePanel.setBackground(TRANSPARENT); 
     messageLinePanel.setLayout(new GridBagLayout()); 
     gridBagConstraints = new GridBagConstraints(); 
     gridBagConstraints.gridx = 0; 
     gridBagConstraints.gridy = 1; 
     gridBagConstraints.weightx = 1D; 
     gridBagConstraints.fill = GridBagConstraints.HORIZONTAL; 
     gridBagConstraints.anchor = GridBagConstraints.NORTHWEST; 
     entryPanel.add(messageLinePanel, gridBagConstraints); 

     JLabel hasMoreMessageLineLabel = new JLabel("+ "); 
     hasMoreMessageLineLabel.setFont(new Font("Courier New", Font.BOLD, 11)); 
     gridBagConstraints = new GridBagConstraints(); 
     gridBagConstraints.gridx = 0; 
     gridBagConstraints.gridy = 0; 
     gridBagConstraints.anchor = GridBagConstraints.NORTHWEST; 
     messageLinePanel.add(hasMoreMessageLineLabel, gridBagConstraints); 

     JLabel messageLineLabel = new JLabel(messageText); 
     messageLineLabel.setBackground(TRANSPARENT); 
     messageLineLabel.setFocusable(true); 
     messageLineLabel.setFont(new Font("Courier New", Font.PLAIN, 11)); 
     gridBagConstraints = new GridBagConstraints(); 
     gridBagConstraints.gridx = 1; 
     gridBagConstraints.gridy = 0; 
     gridBagConstraints.weightx = 1D; 
     gridBagConstraints.fill = GridBagConstraints.HORIZONTAL; 
     gridBagConstraints.anchor = GridBagConstraints.NORTHWEST; 
     messageLinePanel.add(messageLineLabel, gridBagConstraints); 

     entryPanel.addMouseListener(new MouseAdapter() { 
      @Override 
      public void mouseEntered(MouseEvent e) 
      { 
       final JPanel containerPanel = (JPanel) e.getComponent(); 
       final JPanel messagePanel = (JPanel) containerPanel.getComponent(1); 
       final JLabel messageLabel = (JLabel) messagePanel.getComponent(1); 
       String text = messageLabel.getText(); 
       textPane.setText(text); 
      } 

      @Override 
      public void mouseExited(MouseEvent e) 
      { 
       textPane.setText(""); 
      } 
     }); 
    } 
} 
+0

忘了提,Windows 7的专业版。 – Steve11235 2013-03-12 15:00:56

回答

3
private static final Color TRANSPARENT = new Color(255, 255, 255, 0); 

我想那是你的问题。使用透明色设置背景时要小心。了解为什么这是一个问题,请参见Backgrounds With Transparency

在你的情况(因为你使用完全透明),你可以使用:

setOpaque(false); 
+0

这是有道理的。让我尝试改变它。如果有效,我会“接受”你的答案。谢谢! – Steve11235 2013-03-12 16:50:36

+0

哇..Camickr你有惊人的摆动动力学概念..虽然我从来没有尝试OP在他的代码中完成的方式,但是今天我开始了解你的新概念...... !!! +1的.. – 2013-03-12 16:50:42

0

男人,这很奇怪。

对于需要更多信息的用户:滚动文本面板时,滚动面板内的面板内容不会保持其完整性;字母部分的字母在原稿向下滚动时留下,使其看起来有点“融化”。换句话说,“损坏”并不意味着破坏文本,它是构成图像的像素被破坏。

我会尝试在没有GridBag的情况下在滚动面板内制作面板。我对GridBag并不熟悉,我觉得它很不愉快,并且迄今为止的管理大部分都没有它。但你的内部面板的结构看起来很简单,你也不需要它:看起来你有一个图像左上角,然后是文本行,然后可能是另一个图像,然后是另一行文本。制作两个JPanel,每行一个,可以使用流布局和水平方向,只需将图像和文本粘贴在那里,然后水平布局并将两个JPanel粘贴到面板中,现在您可以添加一个面板滚动面板。

我不知道这是否会解决问题,但GridBag是唯一从你的代码看起来不同于我之前完成的事情没有这种行为。 GridBag很容易找出它可能会出错的地方。

+0

加上一个用于描述发生的情况。结果看起来很融化。 – Steve11235 2013-03-12 16:49:46