2

如何创建一个看起来像 JPanel/JLabel 的 JFrame?

我需要一个启动屏幕,当我的程序在显示主窗口之前进行一些计算时会显示它(我知道 Java 中有一个启动屏幕选项,但我需要在加载类之后显示,而不是在加载期间显示)。

我怎样才能创造它?

4

2 回答 2

4

像这样创建一个未装饰的JFrame

JFrame frame = new JFrame();
frame.setUndecorated(true);

但是,我建议你使用它,java.awt.SplashScreen因为它是一段已经在很多情况下使用和测试过的代码,所以它肯定比你能写的更稳定。

于 2012-11-07T10:36:58.813 回答
4

您不需要 JFrame。这是一个使用 JWindow 的示例。

单击 按钮显示 Spash 屏幕,并通过单击 spash 屏幕将其隐藏。

闪屏

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Properties;

import javax.swing.*;

public class Splashing {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame("Splashing");
                JButton splash = new JButton("Splash");
                JPanel orangePanel = new JPanel();
                orangePanel.setBackground(Color.orange);
                frame.getContentPane().add(orangePanel, BorderLayout.CENTER);
                frame.getContentPane().add(splash, BorderLayout.SOUTH);
                splash.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        Splash s = new Splash();
                        s.setVisible(true);
                    }
                });
                frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                frame.setMinimumSize(new Dimension(800, 450));
                frame.setLocationRelativeTo(null); // Center
                frame.pack();
                frame.setVisible(true);
            }
        });
    }

    static class Splash extends JWindow {

        private final Properties sysProps = System.getProperties();

        public Splash() {
            super();
            this.setAlwaysOnTop(true);
            initListeners();
            initGUI();
            this.setVisible(false);
        }

        void initListeners() {
            this.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    Splash.this.setVisible(false);
                    Splash.this.dispose();
                }
            });
        }

        void initGUI() {
            final Dimension preferredSize = new Dimension(480, 360);
            setPreferredSize(preferredSize);
            setMinimumSize(preferredSize);
            setMaximumSize(preferredSize);

            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            this.setLocation((screenSize.width - 480) >> 1, (screenSize.height - 360) >> 1);
            this.pack();
            repaint();
        }

        public void paint(Graphics g) {
            Graphics2D g2 = (Graphics2D) g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
            // TODO Add an appropriate image here
            //g2.drawImage(someImage, 0, 0, this);
            drawAboutText2(g2);
        }

        void drawAboutText2(Graphics2D g2) {
            g2.setFont(new Font("Verdana", Font.PLAIN, 10));
            g2.setColor(new Color(128, 130, 132));
            g2.drawString("All rights reserved", 20, 310);
            g2.drawString("Author: To be decided", 20, 325);
            g2.drawString("http://www.mywebsite.com", 20, 340);

            g2.drawString(getBuild(), 260, 295);
            g2.drawString(getJDK(), 260, 310);
            g2.drawString(getVendor(), 260, 325);
            g2.setColor(new Color(241, 101, 56)); // orange

            g2.drawString(getLicensee(), 260, 340);
        }

        private String getBuild() {
            return "Build# 427 on 15. June 2006";
        }

        private String getJDK() {
            return "JDK: " + sysProps.getProperty("java.version");
        }

        private String getVendor() {
            return "Vendor: " + sysProps.getProperty("java.vendor");
        }

        private String getLicensee() {
            return "Licensee: " + "John Doe";
        }
    }
}
于 2012-11-07T10:56:43.523 回答