【发布时间】:2020-02-09 19:43:49
【问题描述】:
我正在使用 JFrame 制作游戏。在开始之前,我希望一张图片显示五秒钟,然后开始游戏的其余部分。游戏在调用 GUI 构造函数时开始:
public GUI() {
JFrame frame = new JFrame("Razor Fin"); //initialize JFrame
frame.getContentPane().setBackground(Color.BLACK);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1275,760);
frame.setResizable(false);
frame.setLayout(new FlowLayout());
frame.add(Box.createRigidArea(new Dimension(110,100)));
frame.setVisible(true);
try {dispLogo(frame);} catch (Exception e) {e.printStackTrace();}
jta = new JTextArea(30,90); //initializing the output field. The constructor takes in lines of text to set size, not pixel measurements.
jta.setEditable(false);
jta.setBackground(c);
frame.add(new JScrollPane(jta));
DefaultCaret caret = (DefaultCaret)jta.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
frame.add(Box.createRigidArea(new Dimension(110,100)));
jta2 = new JTextField();
jta2.setBackground(c);
jta2.setForeground(Color.WHITE);
jta2.setPreferredSize(new Dimension(500,20));
jta2.setHorizontalAlignment((int) JTextField.CENTER_ALIGNMENT);
frame.add(new JScrollPane(jta2));
jta2.addActionListener(this);
frame.setVisible(true);
}
GUI 构造函数在创建文本框之前调用 dispLogo:
static void dispLogo(JFrame frame) throws InterruptedException
{
ImageIcon image = new ImageIcon("logo.png");
JLabel imageLabel = new JLabel(image);
frame.add(imageLabel);
imageLabel.setVisible(true);
TimeUnit.SECONDS.sleep(5);
imageLabel.setVisible(false);
}
不是显示图像,而是黑屏。有人知道如何解决吗?
【问题讨论】: