1

我对设置边界的“速度”有疑问。我有一个带有多个 JTextPane 的显示器(大约 450 个,需要什么),它们经常更新(取决于用户输入)。这是设置边框功能:

    private void setBorder(int top, int left, int bottom, int right, Color color)
    {
        Args.checkForNull(color);
        this.setBorder(BorderFactory.createMatteBorder(top, left, bottom, right, color));
    }

你能给我一些提示,如何提高边界变化的速度?我的意思是这部分:

this.setBorder(BorderFactory.createMatteBorder(top, left, bottom, right, color));

就像是:

tmp = this.getStyledDocument();
        this.setDocument(blank);
        if(onOff){
            tmp.setParagraphAttributes(0, tmp.getLength(), underlinedAttr, false);
        }
        else{
            tmp.setParagraphAttributes(0, tmp.getLength(), notUnderlinedAttr, false);
        }

        this.setDocument(tmp);

谢谢!

4

1 回答 1

3

这对我来说运行得很快,所以你的问题不太可能出现在 setBorder() 中。再次更好地衡量,可能的问题是您在单独的事件中更新边框或具有非常复杂的布局。可能是您的显卡(驱动程序)不好,您可以尝试查看运行是否-Dsun.java2d.d3d=false更好。

import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.Random;
import javax.swing.*;

public class TestBorderSpeed {
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                int amount = 22;

                final JPanel panel = new JPanel(new GridLayout(amount, amount));
                for (int row = 0; row < amount; row++) {
                    for (int column = 0; column < amount; column++) {
                        JTextPane pane = new JTextPane();
                        pane.setText("Row " + row + "; Column " + column);
                        panel.add(pane);
                    }
                }

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                frame.getContentPane().add(panel);
                frame.getContentPane().add(new JButton(
                  new AbstractAction("Change borders") {

                    private final Random random = new Random();
                    private final Color[] colors =
                      { Color.RED, Color.GREEN, Color.BLUE };

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        for (Component component : panel.getComponents()) {
                            ((JComponent) component).setBorder(
                              BorderFactory.createMatteBorder(
                                    random.nextInt(3) + 1, random.nextInt(3) + 1,
                                    random.nextInt(3) + 1, random.nextInt(3) + 1,
                                    colors[random.nextInt(colors.length)]));
                        }
                    }
                }), BorderLayout.PAGE_END);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}
于 2012-10-08T11:18:02.097 回答