ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java – JScrollPane’laggy’滚动,包含许多组件

2019-07-29 00:12:11  阅读:137  来源: 互联网

标签:java performance swing jscrollpane


我有3045的表格!组件(1015个标签,1015个文本字段,1015个组合框).所有这些都在JPanel和JScrollPane中的JPanel中.问题是滚动是“滞后”.我的PC上有4GB RAM,所以我不认为这是问题所在.怎么了?
在我的任务管理器中,应用程序使用~100mb.

我的代码:

final JScrollPane scrollPane_1 = new JScrollPane();
final JPanel panel = new JPanel();
panel.setLayout(null);
scrollPane_1.setViewportView(panel);
int y =0;
for(int i=0; i<1015;i++)
    {

            JLabel length = new JLabel();
            length.setBounds(10, y, 350, 20);
            length.setFont(new Font("Tahoma", Font.BOLD, 11));
            length.setEnabled(false);
            panel.add(length);
            panel.revalidate();

            JComboBox combo = new JComboBox();
            combo.setModel(new DefaultComboBoxModel(new String[] {"=", "!="}));
            combo.setBounds(10, y + 20, 70, 20);
            panel.add(combo);
            panel.revalidate();

            JTextField text = new JTextField();
            text.setBounds(10 + 80, y + 20, 200, 20);
            panel.add(text);
            panel.revalidate();
}

编辑:我做了很多测试,我意识到只有当我使用组合框时才存在延迟,如果我使用例如文本字段而不是组合框,滚动是正常的…

解决方法:

使用布局管理器时,您的代码对我来说似乎并不那么迟钝.请测试一下:

import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;

import javax.swing.*;

public class Foo2 {
   protected static final int PREF_W = 400;
   protected static final int PREF_H = 400;

   public static void main(String[] args) {
      final JScrollPane scrollPane_1 = new JScrollPane() {
         @Override
         public Dimension getPreferredSize() {
            return new Dimension(PREF_W, PREF_H);
         }
      };
      final JPanel panel = new JPanel();
      panel.setLayout(new GridLayout(0, 1));
      scrollPane_1.setViewportView(panel);
      for (int i = 0; i < 1015; i++) {
         JPanel innerPanel = new JPanel();
         innerPanel.setLayout(new BoxLayout(innerPanel, BoxLayout.LINE_AXIS));

         JLabel length = new JLabel("foo");
         length.setFont(new Font("Tahoma", Font.BOLD, 11));
         length.setEnabled(false);
         innerPanel.add(length);
         innerPanel.add(Box.createHorizontalGlue());

         JComboBox<String> combo = new JComboBox<String>();
         combo.setPrototypeDisplayValue("              ");
         combo.setModel(new DefaultComboBoxModel<String>(new String[] { "=", "!=" }));
         combo.setMaximumSize(combo.getPreferredSize());
         innerPanel.add(combo);

         JTextField text = new JTextField(10);
         JPanel textWrapper = new JPanel();
         textWrapper.add(text);
         innerPanel.add(textWrapper);

         panel.add(innerPanel);
      }
      JFrame frame = new JFrame();
      frame.add(scrollPane_1);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }
}

编辑

相反,使用JTable怎么样?

import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

public class Foo3 {
   protected static final int PREF_W = 400;
   protected static final int PREF_H = 400;

   public static void main(String[] args) {
      final JScrollPane scrollPane_1 = new JScrollPane() {
         @Override
         public Dimension getPreferredSize() {
            return new Dimension(PREF_W, PREF_H);
         }
      };
      String[] columnNames = {"Foo", "Bar", "Baz"};
      String[][] data = new String[1015][3];
      for (int i = 0; i < data.length; i++) {
         data[i] = new String[]{"foo", "==", ""};
      }
      DefaultTableModel model = new DefaultTableModel(data, columnNames){
         @Override
         public boolean isCellEditable(int row, int column) {
            return (column != 0);
         }
      };
      JTable table = new JTable(model);
      table.getColumnModel().getColumn(1).setCellEditor(new DefaultCellEditor(
            new JComboBox<String>(new String[]{"==", "!="})));
      final JPanel panel = new JPanel();
      panel.setLayout(new GridLayout(0, 1));
      scrollPane_1.setViewportView(table);

      JFrame frame = new JFrame();
      frame.add(scrollPane_1);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }
}

标签:java,performance,swing,jscrollpane
来源: https://codeday.me/bug/20190728/1567100.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有