比 Box 更高效的布局

发布于 2024-11-19 08:07:36 字数 537 浏览 2 评论 0原文

我有一些非常旧的代码,它使用 Box 来列出一些信息。我这样创建它:

Box patterns = Box.createVerticalBox();

非常(非常)经常,添加新项目并删除旧项目,例如:

label = new JLabel("xyz");
patterns.add(label);

后来

patterns.remove(label);

每当添加某些内容或删除某些内容时,我都必须重新绘制它,所以我打电话:

patterns.revalidate();
patterns.repaint();

问题是,因为这种情况经常发生它让用户界面变得混乱。我认为我需要更好的实施才能提高效率。

我知道我可以在后台维护一个活动项目列表,然后间歇性地更新实际的 UI(批量更新),但是......

有人可以建议一种更有效的替代方法吗?

I've got some very old code which uses a Box to list some information. I create it like so:

Box patterns = Box.createVerticalBox();

Very (very) often, new items are added and old items are removed eg:

label = new JLabel("xyz");
patterns.add(label);

and later

patterns.remove(label);

whenever something is added ore removed I have to have it repaint, so I call:

patterns.revalidate();
patterns.repaint();

Problem is, since this happens very often it chokes up the UI. I think I need a better implementation in order to make it more efficient.

I know I could maintain a list of the active items in the background and then intermittently update the actual UI (batch update) but...

Can someone suggest a more efficient alternative approach?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自此以后,行同陌路 2024-11-26 08:07:36

Why don't you just use a JList and implement a cell renderer?

Or more flexibility with a JTable and implement a table cell renderer (returns a Component instead)?

征棹 2024-11-26 08:07:36

基于此示例,以下代码执行 16 个标签在 10 赫兹。

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;

/** @see https://stackoverflow.com/questions/6605554 */
public class ImageLabelPanel extends Box implements ActionListener {

    private static final int N = 16;
    private final List<JLabel> list = new ArrayList<JLabel>();
    private final Timer timer = new Timer(100, this);

    ImageLabelPanel() {
        super(BoxLayout.Y_AXIS);
        BufferedImage bi = null;
        try {
            bi = ImageIO.read(new File("image.jpg"));
        } catch (IOException e) {
            e.printStackTrace(System.err);
        }
        for (int r = 0; r < N; r++) {
            int w = bi.getWidth();
            int h = bi.getHeight() / N;
            BufferedImage b = bi.getSubimage(0, r * h, w, h);
            list.add(new JLabel(new ImageIcon(b)));
        }
        createPane();
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setVisible(true);
        timer.start();
    }

    private void createPane() {
        this.removeAll();
        for (JLabel label : list) {
            add(label);
        }
        this.revalidate();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Collections.shuffle(list);
        createPane();
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new ImageLabelPanel();
            }
        });
    }
}

Based on this example, the following code loafs doing 16 labels at 10 Hz.

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;

/** @see https://stackoverflow.com/questions/6605554 */
public class ImageLabelPanel extends Box implements ActionListener {

    private static final int N = 16;
    private final List<JLabel> list = new ArrayList<JLabel>();
    private final Timer timer = new Timer(100, this);

    ImageLabelPanel() {
        super(BoxLayout.Y_AXIS);
        BufferedImage bi = null;
        try {
            bi = ImageIO.read(new File("image.jpg"));
        } catch (IOException e) {
            e.printStackTrace(System.err);
        }
        for (int r = 0; r < N; r++) {
            int w = bi.getWidth();
            int h = bi.getHeight() / N;
            BufferedImage b = bi.getSubimage(0, r * h, w, h);
            list.add(new JLabel(new ImageIcon(b)));
        }
        createPane();
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setVisible(true);
        timer.start();
    }

    private void createPane() {
        this.removeAll();
        for (JLabel label : list) {
            add(label);
        }
        this.revalidate();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Collections.shuffle(list);
        createPane();
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new ImageLabelPanel();
            }
        });
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文