我正在尝试为Java WAVPlayer应用程序创建自定义加载屏幕。初始JFrame包含一个自定义的JPanel,它显示一个圆形的加载栏。酒吧的进度是在其自己的线程LoadThread
中控制和更新的。在此线程的结尾,我希望当前的JFrame关闭。
我知道我不能只在this.dispose()
中使用LoadThread
。我尝试使用实例布尔变量来控制何时应该处置JFrame,这显然是失败的。我尝试使用多个线程来控制不同的进程,但是很快就变得太费解了(并且没有产生我想要的结果)。我阅读了Java的SplashScreen类,这可能是最简单的方法。我已经为此工作了几天,我真的只想弄清楚我要完成的工作是否可行/必要。
以下:自定义面板类LoadPanel
,JFrame形式LoadingFrame
,主类WAVPlayer
public class LoadPanel extends JPanel{
int progress=0;
public void UpdateProgress(int progressVal){
this.progress=progressVal;
}
@Override
public void paint(Graphics g){
super.paint(g);
Graphics2D g2=(Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON); // cleans gfx lines
g2.translate(this.getWidth()/2,this.getHeight()/2);
g2.rotate(Math.toRadians(270));
Arc2D.Float arc=new Arc2D.Float(Arc2D.PIE);
Ellipse2D circle=new Ellipse2D.Float(0,0,110,110);
arc.setFrameFromCenter(new Point(0,0),new Point(120,120));
circle.setFrameFromCenter(new Point(0,0),new Point(110,110));
arc.setAngleStart(1);
arc.setAngleExtent(-progress*3.6); // 360/100=3.6
g2.setColor(Color.BLUE); // creating progress circles
g2.draw(arc);
g2.fill(arc);
g2.setColor(Color.BLACK);
g2.draw(circle);
g2.fill(circle);
}
}
public class LoadingFrame extends javax.swing.JFrame {
private Thread LoadThread;
public LoadingFrame() {
setUndecorated(true);
setBackground(new Color(0,0,0,0));
initComponents();
Application.getApplication().setDockIconImage(new ImageIcon(getClass()
.getResource("/waveicons/WAVE_ICON32.png")).getImage());
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
testButton = new javax.swing.JButton();
jp_progress = new wavplayer.LoadPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
testButton.setText("testButton");
testButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
testButtonActionPerformed(evt);
}
});
jp_progress.setBackground(new Color(0,0,0,0));
javax.swing.GroupLayout jp_progressLayout = new javax.swing.GroupLayout(jp_progress);
jp_progress.setLayout(jp_progressLayout);
jp_progressLayout.setHorizontalGroup(
jp_progressLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
jp_progressLayout.setVerticalGroup(
jp_progressLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jp_progress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(testButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(133, 133, 133)
.addComponent(testButton)
.addContainerGap(138, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jp_progress, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>
private void testButtonActionPerformed(java.awt.event.ActionEvent evt) {
testButton.setEnabled(false);
LoadThread=new Thread(new Runnable(){
@Override
public void run(){
for (int num = 1; num <= 100; num++) {
jp_progress.UpdateProgress(num);
jp_progress.repaint();
try {
Thread.sleep(50);
} catch (InterruptedException ex) {
Logger.getLogger(LoadingFrame.class.getName())
.log(Level.SEVERE, null, ex);
}
}
testButton.setEnabled(true);
}
});
LoadThread.start();
}
// Variables declaration - do not modify
private wavplayer.LoadPanel jp_progress;
private javax.swing.JButton testButton;
// End of variables declaration
}
public class WAVPlayer {
public static void main(String[] args) {
LoadingFrame frame=new LoadingFrame();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.pack();
System.out.println("Im here at the end of main");
}
}