0

我正在尝试为 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");
}
}
4

0 回答 0