gpt4 book ai didi

java - 如何在启动画面上显示 jbutton

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

如何将按钮添加到启动画面

嗨,我必须创建 java splashscreen在飞溅过程中,它会停止/暂停并显示确定按钮。每次编译 netbean 时,我都会运行启动画面。

public class Main
{
static SplashScreen mySplash; // instantiated by JVM we use it to
// get graphics
static Graphics2D splashGraphics; // graphics context for overlay of the
// splash image
static Rectangle2D.Double splashTextArea; // area where we draw the text

static Rectangle2D.Double splashProgressArea; // area where we draw the progress bar

static Font font; // used to draw our text

public static void main(String[] args)
{
splashInit(); // initialize splash overlay drawing parameters

}

//create button here
private static void splashInit()
{

//do coding here for mannipulating splash screen
//put ok button here

}
}

我们怎么可能把按钮放到闪屏上?通常我只能把JButton放在JFrame或者JPanel上。是否可以在启动画面等图像上放置按钮?

引用:Splashscreen beginner netbean

最佳答案

"Can you kind enough to instruct me on how to make jdialog as splash screen? "

在下面的示例中,这是我所做的事情。

  • 创建一个JDialog 类并确保它是未修饰的

    public class SplashDialog extends JDialog {
    ....
    setUndecorated(true);
  • 给它一个背景图片

    JLabel background = new JLabel(createImage());
    background.setLayout(new BorderLayout());
    setContentPane(background);
  • 将带有JButtonJPanel 添加到背景。但是将 JPanel 设置为不可见,也只是为了添加一些样式,给 JPanel 一点透明度,所以当您确实将它设置为可见时,你仍然可以看到背景图像

    final JPanel panel = new JPanel(new GridBagLayout());
    panel.setVisible(false);
    panel.setBackground(new Color(0, 0, 0, 150));
    JButton okBut = new JButton("OK");
    panel.add(okBut);
    background.add(panel);
  • 使用 javax.swing.Timer 设置按钮出现的延迟。

    Timer timer = new Timer(5000, new ActionListener(){
    public void actionPerformed(ActionEvent e) {
    panel.setVisible(true);
    }
    });
    timer.setRepeats(false);
    timer.start();
  • 确保框架不可见,但是当按下按钮时,框架变得可见并且 JDialog 处理

    okBut.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) {
    dispose();
    parent.setVisible(true);
    }
    });

现在你有了一个简单的斜杠屏幕

初始启动

enter image description here

5 秒后飞溅

enter image description here

import java.awt.*;
import java.awt.event.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.*;
import javax.swing.*;

public class SplashDialogDemo {

public SplashDialogDemo() {
JPanel panel = new JPanel() {
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
};
panel.setBackground(Color.BLACK);

JFrame frame = new JFrame();
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBackground(Color.BLACK);
frame.pack();
frame.setLocationRelativeTo(null);

SplashDialog splash = new SplashDialog(frame, true);
}

public class SplashDialog extends JDialog {

public SplashDialog(final JFrame parent, boolean modal) {
super(parent, modal);

JLabel background = new JLabel(createImage());
background.setLayout(new BorderLayout());
setContentPane(background);

final JPanel panel = new JPanel(new GridBagLayout());
panel.setVisible(false);
panel.setBackground(new Color(0, 0, 0, 150));
JButton okBut = new JButton("OK");
panel.add(okBut);
background.add(panel);

okBut.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
parent.setVisible(true);
}
});

Timer timer = new Timer(5000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
panel.setVisible(true);
}
});
timer.setRepeats(false);
timer.start();

addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});

setUndecorated(true);
pack();
setLocationRelativeTo(parent);
setVisible(true);
}

private ImageIcon createImage() {
ImageIcon icon = null;
try {
URL url = new URL("http://www.iconsdb.com/icons/download/black/stackoverflow-2-256.png");
icon = new ImageIcon(url);
} catch (MalformedURLException ex) {
Logger.getLogger(SplashDialogDemo.class.getName()).log(Level.SEVERE, null, ex);
}
return icon;
}
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
Logger.getLogger(SplashDialogDemo.class.getName()).log(Level.SEVERE, null, ex);
}
new SplashDialogDemo();
}
});
}
}

关于java - 如何在启动画面上显示 jbutton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22623827/

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