我有一个错误的代码,我知道问题出在哪里,但我不知道如何实施解决方案。我想制作一个程序,单击按钮即可在实习生 PDF 阅读器中打开 PDF。到目前为止效果很好,但由于它加载了几秒钟,我想使用 JWindow 实现一个显示徽标的启动画面。但由于我的 PDF 阅读器也使用 GUI,它们似乎冲突,因为 JWindow 没有出现。我知道官方的 SplashScreen 类被推荐为一个更好的解决方案,但我找不到关于如何使用它的简单教程,所以我选择了 JWindow 版本。有人可以帮我实现此代码的解决方案吗?
帮助将不胜感激
加载屏幕类
package pdf;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JWindow;
public class loadingScreen extends JWindow {
BorderLayout bl = new BorderLayout();
JLabel imageLabel = new JLabel();
JPanel barPanel = new JPanel();
FlowLayout barPanelLayout = new FlowLayout();
JProgressBar progress = new JProgressBar();
ImageIcon icon;
public loadingScreen(ImageIcon icon) {
this.icon = icon;
try {
lsInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
public void lsInit() throws Exception{
this.imageLabel.setIcon(this.icon);
this.getContentPane().setLayout(bl);
barPanel.setLayout(this.barPanelLayout);
barPanel.setBackground(Color.BLACK);
this.getContentPane().add(this.imageLabel,BorderLayout.CENTER);
//this.getContentPane().add(this.barPanel, BorderLayout.SOUTH);
//barPanel.add(this.progress,null);
this.setSize(new Dimension(500,500));
this.setVisible(true);
this.pack();
}
public void setScreenVisible(boolean b) {
setVisible(b);
}
}
使用加载屏幕的 PDF 阅读器
package pdf;
import java.awt.SplashScreen;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import org.apache.pdfbox.pdfviewer.PDFPagePanel;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
public class PDFViewer {
int currentPageNo = 0;
loadingScreen screen;
public PDFViewer(String path) {
File pdf_path = new File(path);
try {
splashScreenInit();
PDDocument inputPdf = PDDocument.load(pdf_path);
List<PDPage> allPgs = inputPdf.getDocumentCatalog().getAllPages();
PDPage testPage = (PDPage)allPgs.get(0);
JFrame testFrame = new JFrame();
testFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
testFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
try {
inputPdf.close();
testFrame.setVisible(false);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
PDFPagePanel pdfPanel = new PDFPagePanel();
pdfPanel.setPage(testPage);
testFrame.add(pdfPanel);
testFrame.setBounds(0, 0, pdfPanel.getWidth(), pdfPanel.getHeight()+50);
testFrame.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_DOWN ) {
if(currentPageNo < allPgs.size()-1) {
currentPageNo++;
PDPage currentPage = (PDPage)allPgs.get(currentPageNo);
pdfPanel.setPage(currentPage);
testFrame.add(pdfPanel);
testFrame.invalidate();
testFrame.validate();
testFrame.repaint();
}
}
if(e.getKeyCode() == KeyEvent.VK_UP) {
if(currentPageNo>0) {
currentPageNo--;
PDPage currentPage = (PDPage)allPgs.get(currentPageNo);
pdfPanel.setPage(currentPage);
testFrame.add(pdfPanel);
testFrame.invalidate();
testFrame.validate();
testFrame.repaint();
}
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
});
splashScreenDestruct();
testFrame.setVisible(true);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void splashScreenInit() {
ImageIcon logo = new ImageIcon("resources/BASF_logo_logotype.png");//Splashscrreen
screen = new loadingScreen(logo);
//screen.setLocationRelativeTo(null);
//screen.setScreenVisible(true);
//screen.setProgressMax(100);
}
public void splashScreenDestruct() {
screen.setScreenVisible(false);
screen = null;
}
}