第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

創(chuàng)建多個(gè) JButton(數(shù)百個(gè))會(huì)在創(chuàng)建它們時(shí)產(chǎn)生巨大的延遲

創(chuàng)建多個(gè) JButton(數(shù)百個(gè))會(huì)在創(chuàng)建它們時(shí)產(chǎn)生巨大的延遲

有只小跳蛙 2022-10-12 09:59:41
當(dāng)我創(chuàng)建 JButtons 時(shí),我得到了很大的延遲。這是我如何創(chuàng)建按鈕的一些示例代碼:import javax.imageio.ImageIO;import javax.swing.*;import java.awt.*;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;public class Scratch {public static void main(String[] args) {    Runnable r = () -> {        JOptionPane.showMessageDialog(                null, new Scratch().getUI(new TileSet("Content/Graphics/tileSets/12x12x3 - tileSet.png", 12, 12, 3)));        JOptionPane.showMessageDialog(                null, new Scratch().getUI(new TileSet("Content/Graphics/tileSets/16x16x0 - tileSetItems.png", 12, 12, 3)));        JOptionPane.showMessageDialog(                null, new Scratch().getUI(new TileSet("Content/Graphics/tileSets/29x18x1 - roguelikeDungeon_transparent.png", 12, 12, 3)));    };    SwingUtilities.invokeLater(r);}public final JComponent getUI(TileSet tileSet) {    JPanel ui = new JPanel();    JPanel tilePanel = new JPanel();    tilePanel.setLayout(new GridLayout(12, 12, 5, 5));    long t1 = System.currentTimeMillis();    TileButton tileMenuButtons[] = new TileButton[tileSet.tileSet.length];    long tot = 0;    for (int i = 0; i < tileMenuButtons.length; i++) {        long t2 = System.currentTimeMillis();        tileMenuButtons[i] = new TileButton(i,tileSet);        long t3 = System.currentTimeMillis();        tot += (t3-t2);        System.out.println(String.format("It took : "+ tot +"ms for loading "+i+ ". Button "));        tilePanel.add(tileMenuButtons[i]);    }    long t4 = System.currentTimeMillis();    JScrollPane scrollPane = new JScrollPane();    scrollPane.getVerticalScrollBar().setUnitIncrement(16);    scrollPane.setOpaque(true);    scrollPane.setViewportView(tilePanel);    ui.add(scrollPane);    System.out.println(String.format("It took in total : "+ (t4-t1) +"ms for loading "+tileMenuButtons.length+ " TileButtons"));    return ui;}我可以過(guò)濾問(wèn)題是由我的按鈕類引起的,但我不明白在哪里以及為什么?
查看完整描述

2 回答

?
繁星coding

TA貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超4個(gè)贊

這是一個(gè)從 100 個(gè)按鈕到6,400個(gè)按鈕添加到 GUI 的 MCVE / SSCCE ,每個(gè)按鈕都有自己的圖標(biāo)。


這里的典型輸出:


It took 14 milliseconds for 100 buttons.

It took 110 milliseconds for 1600 buttons.

It took 138 milliseconds for 6400 buttons.

它可能看起來(lái)像一個(gè)框架。

http://img1.sycdn.imooc.com//63461fbd00013cd105370408.jpg

import java.awt.*;

import java.awt.image.*;

import java.io.IOException;

import java.net.*;

import javax.swing.*;

import javax.imageio.*;


public class LotsOfButtons {


    public final JComponent getUI(int pts) {

        JComponent ui = new JPanel(new GridLayout(0, pts));

        try {

            BufferedImage image = ImageIO.read(new URL(

                    "https://i.stack.imgur.com/OVOg3.jpg"));


            int wT = image.getWidth() / pts;

            int hT = image.getHeight() / pts;

            Insets insets = new Insets(0, 0, 0, 0);


            long t1 = System.currentTimeMillis();

            for (int jj = 0; jj < pts; jj++) {

                for (int ii = 0; ii < pts; ii++) {

                    int x = ii * wT;

                    int y = jj * hT;

                    JButton b = new JButton(new ImageIcon(

                            image.getSubimage(x, y, wT, hT)));

                    b.setMargin(insets);

                    ui.add(b);

                }

            }

            long t2 = System.currentTimeMillis();


            System.out.println(String.format(

                    "It took %1s milliseconds for %1s buttons.",

                    (t2 - t1), pts*pts));

        } catch (IOException ex) {

            ex.printStackTrace();

        }

        return ui;

    }


    public static void main(String[] args) {

        Runnable r = () -> {

            JOptionPane.showMessageDialog(

                    null, new LotsOfButtons().getUI(10));

            JOptionPane.showMessageDialog(

                    null, new LotsOfButtons().getUI(40));

            JOptionPane.showMessageDialog(

                    null, new LotsOfButtons().getUI(80));

        };

        SwingUtilities.invokeLater(r);

    }

}

因此,鑒于“(> 30秒)”14 milliseconds附近沒(méi)有任何地方,我猜您會(huì)這樣做..不同。除非該源(以上)可以幫助您解決問(wèn)題,否則我建議準(zhǔn)備并發(fā)布一個(gè)熱鏈接到圖像的 MCVE / SSCCE,就像上面的源代碼一樣,將是解決問(wèn)題的最佳舉措。


查看完整回答
反對(duì) 回復(fù) 2022-10-12
?
UYOU

TA貢獻(xiàn)1878條經(jīng)驗(yàn) 獲得超4個(gè)贊

您的問(wèn)題可能出在 TileButton 類中:


class TileButton extends JButton {

    private int id;

    private TileSet ts = new TileSet("Content/Graphics/tileSets/12x12x3 - tileSet.png", 12, 12, 3);

    private int size = 50;

    public TileButton(int id, TileSet tileSet) {

        super();

        this.ts = tileSet;

        this.id = id;

        loadImage(id);

    }

為每個(gè) TileButton 創(chuàng)建新的 TileSet。此圖塊集從文件中讀取 - 這可能會(huì)導(dǎo)致相當(dāng)大的延遲。然后你忽略這個(gè)瓦片集并使用在構(gòu)造函數(shù)中傳遞的瓦片集。


因此,您不應(yīng)該每次都創(chuàng)建新的 TileSet:


class TileButton extends JButton {

    private int id;

    private final TileSet ts;

    private int size = 50;

    public TileButton(int id, TileSet tileSet) {

        super();

        this.ts = tileSet;

        this.id = id;

        loadImage(id);

    }


查看完整回答
反對(duì) 回復(fù) 2022-10-12
  • 2 回答
  • 0 關(guān)注
  • 102 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)