我正在使用以下代码来显示启动画面。以下在Java 8中运行良好。但在Java 9中,我观察到闪屏不再位于中心,而是位于左上方。
dialog = new JDialog();
dialog.setModal(false);
dialog.setUndecorated(true);
dialog.getRootPane().setOpaque(false);
dialog.getContentPane().setBackground(new Color(0, 0, 0, 0));
dialog.setBackground(new Color(0, 0, 0, 0));
dialog.setResizable(false);
...
dialog.pack();
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);
我确认问题发生在
dialog.setBackground
但不知道为什么。我尝试使用JFrame并遇到了同样的问题。知道如何在Java 9中修复它吗?
编辑:
以下是完整的代码:
package test;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Image;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.util.Properties;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
public class SplashScreen
{
private static JDialog dialog;
private String fImage;
static JProgressBar progressBar = new JProgressBar();
public SplashScreen(String image)
{
fImage = image;
}
// -------------------------------------------------------------------------------------------
// hideSplashScreen
// -------------------------------------------------------------------------------------------
public void hideSplashScreen()
{
dialog.setVisible(false);
dialog.dispose();
}
// -------------------------------------------------------------------------------------------
// showSplashScreen
// -------------------------------------------------------------------------------------------
public static void showSplashScreen() throws MalformedURLException
{
dialog = new JDialog();
dialog.setModal(false);
dialog.setUndecorated(true);
dialog.getRootPane().setOpaque(false);
dialog.getContentPane().setBackground(new Color(0, 0, 0, 0));
dialog.setBackground(new Color(0, 0, 0, 0));
dialog.setResizable(false);
// Scaling down
Image image = new ImageIcon("C:\\SplashScreen.gif").getImage().getScaledInstance(700, 700, java.awt.Image.SCALE_SMOOTH);
JLabel background = new JLabel(new ImageIcon(image));
background.setLayout(new BorderLayout());
dialog.add(background);
dialog.add(progressBar, BorderLayout.SOUTH);
progressBar.setStringPainted(true);
progressBar.setForeground(new Color(0, 146, 189));
dialog.pack();
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);
}
// -------------------------------------------------------------------------------------------
// readVersion
// -------------------------------------------------------------------------------------------
private String readVersion()
{
String version = "";
Properties prop = new Properties();
try
{
InputStream in = SplashScreen.class.getClassLoader().getResourceAsStream("sciome/workbench/resources/version.properties");
prop.load(in);
in.close();
version = prop.getProperty("version");
}
catch (IOException e)
{
System.err.println(e.getMessage());
}
return version;
}
// ---------------------------------------------------------------------------------
// setProgBarVisible
// ---------------------------------------------------------------------------------
public void setProgBarVisible(boolean bVisible)
{
progressBar.setVisible(bVisible);
}
// ---------------------------------------------------------------------------------
// updateProgBar
// ---------------------------------------------------------------------------------
public void updateProgBar(String strDetails, int nProgress)
{
progressBar.setValue(nProgress);
progressBar.setString(strDetails + " (" + nProgress + "% complete).");
}
public static void main(String[] args)
{
try
{
showSplashScreen();
}
catch (MalformedURLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
我使用的是jdk-9.0.1。请确保在测试上述代码时,将类路径更改为jdk-9.0.1以及将编译和构建设置更改为Java 9