加载步骤-6ren">
gpt4 book ai didi

java - JButton 与 imageicon "lag"

转载 作者:行者123 更新时间:2023-11-30 09:07:06 26 4
gpt4 key购买 nike

滞后可能不是准确的术语,但当我将图像添加到按钮时,整个框架不会显示,至少一开始不会。重新调整窗口大小会按预期显示所有按钮。

如果没有图像,这个问题就不会发生,所以我猜测正在发生一些中间检索->加载步骤,直到发生这种情况,我才得到这个“错误”。所以我的问题是,假设这是正确的,我可以做些什么来规避/纠正它,以便一切都按预期立即显示?

代码如下:

/* Author: Luigi Vincent Exercise: 12.1 -
* Create a frame and set its layout to FlowLayout
* Create two panels and add them to the frame.
* Each panel contains three buttons. The panel uses FlowLayout
*/

import javax.swing.*;
import java.awt.*;

public class Ex_1 {
public static void main(String[] args) {
Ex1_Layout frame = new Ex1_Layout();
frame.setTitle("Exercise 12.1");
frame.setSize(450, 250);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

ImageIcon valk = new ImageIcon("C:/MyWork/Images/valk.gif");

JButton alpha = new JButton("Disgaea");
JButton beta = new JButton("Riviera: The Promised Land");
JButton gamma = new JButton("Yggdra Union");
JButton delta = new JButton("Dissidia");
JButton epsilon = new JButton("Valkyrie Profile", valk);
JButton zeta = new JButton("Ragnarok Online");

JPanel p1 = new JPanel();
JPanel p2 = new JPanel();

p1.add(alpha);
p1.add(beta);
p1.add(gamma);
p2.add(delta);
p2.add(epsilon);
p2.add(zeta);

frame.add(p1);
frame.add(p2);

} // End of Main
} // End of Ex_1

class Ex1_Layout extends JFrame {
public Ex1_Layout(){
setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0)); // Set FlowLayout, aligned left, horizontal and vertical gap of 0
}
} // End of Ex1_Layout

最佳答案

when I add an image to a button the entire frame doesn't display, at least not at first.

在添加JFrame中的所有组件后调用frame.setVisible(true);


几点:

  1. 使用 SwingUtilities.invokeLater() 确保 EDT正确初始化。

    阅读更多

  2. 除非您正在修改现有逻辑,否则不要扩展任何类。

    阅读更多

关于java - JButton 与 imageicon "lag",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24144188/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com