【发布时间】:2023-03-28 05:32:01
【问题描述】:
滞后可能不是准确的术语,但是当我将图像添加到按钮时,整个框架都不会显示,至少一开始不会。重新调整窗口大小会按预期显示所有按钮。
如果没有图像,这个问题就不会发生,所以我猜测一些中间检索->加载步骤正在发生,直到发生这种情况,我才得到这个“错误”。所以我的问题是,假设这是正确的,我能做些什么来规避/纠正它,以便一切都按预期立即显示?
代码如下:
/* 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
【问题讨论】: