当前位置:编程学习 > JAVA >>

JDK 6 Splash

我用http://blog.sina.com.cn/s/blog_4b6047bc010009f0.html最底下的例子做了个登录闪屏,
但我想在Jpanel上添加个Toggle按钮,点击开始时,才会让进度条显示。
可是启动后,那个Button无法点击,Windows 7一直显示蓝色转动的小圈。

SplashPanel.java
public void keyPressed(KeyEvent e) {
         if(e.getKeyCode()==13)
         System.out.println("enter pressed");
         stoppro = true; 
         }

主程序:
SplashPanel panel=new SplashPanel();
    
        SplashManager manager=new SplashManager(panel);
        panel.setMessage("Start to download updating...");
        manager.repaint();
        for(int i=0;i<100;i++){
             panel.setProgress(i);
             manager.repaint();
            try{Thread.sleep(100);}catch(Exception e){}
        }
        manager.closeSplash(); --------------------编程问答-------------------- 楼主能不能把代码贴全一点,我们也好帮你解决啊 --------------------编程问答-------------------- 转动的小圈表明程序已经在运行,不过处于等待状态 --------------------编程问答-------------------- 想在SplashPanel启动时,点击Jpanel上的开始来控制Splash画面和ProgressBar的停止和运行。
主程序:

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
SeaMonitorMainApp window = new SeaMonitorMainApp();

window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

public SeaMonitorMainApp() {
initialize();
}
private void initialize() {
  try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());} catch (Exception ex){}
        //New an instance of the progress panel.
        SplashPanel panel=new SplashPanel();
        //Encapsulate the panel into splash manager.
        SplashManager manager=new SplashManager(panel);
        //Set some indication message.
        panel.setMessage("Start to download updating...");
        //Note every time you update panel, you have to manually call SplashManager.repaint to update the screen.
        manager.repaint();
        for(int i=0;i<100;i++){
            //Do some time-consuming work and signal the progress to splash manager.
            panel.setProgress(i);
            //Note every time you update panel, you have to manually call SplashManager.repaint to update the screen.
            manager.repaint();
            try{Thread.sleep(100);}catch(Exception e){}
        }
        manager.closeSplash();

frame = new JFrame();
frame.setFont(new Font("微软雅黑", Font.PLAIN, 9));
frame.setTitle("抽签分组程序");
frame.setBounds(200, 200, 620, 480);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Toolkit toolkit = Toolkit.getDefaultToolkit();
int x = (int) (toolkit.getScreenSize().getWidth() - frame.getWidth()) / 2;
int y = (int) (toolkit.getScreenSize().getHeight() - frame.getHeight()) / 2;
frame.setLocation(x, y);

// frame.dispose();
menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);

JMenu menu = new JMenu("名单");
menuBar.add(menu);

JMenuItem menuItem = new JMenuItem("录入名单");
menuItem.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
teamnamelogging();
}
});
   //***** 后面代码省略




SplashPanel

import java.awt.Color;
import java.awt.Font;
import java.util.StringTokenizer;
import javax.swing.JToggleButton;
import javax.swing.GroupLayout.Alignment;
import javax.swing.GroupLayout;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

/**
 *
 * @author  William Chen
 */
public class SplashPanel extends javax.swing.JPanel{
    
    /** Creates new form SplashPanel */
    public SplashPanel() {
        initComponents();
    }
    
    /** 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.
     */
    // <editor-fold defaultstate="collapsed" desc=" 生成的代码 ">                          
    private void initComponents() {
        lblMessage = new javax.swing.JLabel();
        pBar = new javax.swing.JProgressBar();

        lblMessage.setText("\u53d1\u73b0\u65b0\u7248\u672c\u7684\u8f6f\u4ef6\uff0c\u6b63\u5728\u5347\u7ea7\u4e2d\uff0c\u8bf7\u7a0d\u5019...");
        
        JToggleButton toggleButton = new JToggleButton("\u5F00\u59CB");
   
        toggleButton.addMouseListener(new MouseAdapter() {
         @Override
         public void mousePressed(MouseEvent e) {        
         stoppro = true;        
         } 
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        layout.setHorizontalGroup(
         layout.createParallelGroup(Alignment.TRAILING)
         .addGroup(layout.createSequentialGroup()
         .addContainerGap()
         .addGroup(layout.createParallelGroup(Alignment.LEADING)
         .addGroup(layout.createSequentialGroup()
         .addGroup(layout.createParallelGroup(Alignment.TRAILING)
         .addComponent(lblMessage, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 430, Short.MAX_VALUE)
         .addComponent(pBar, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 430, Short.MAX_VALUE))
         .addContainerGap())
         .addGroup(Alignment.TRAILING, layout.createSequentialGroup()
         .addComponent(toggleButton)
         .addGap(26))))
        );
        layout.setVerticalGroup(
         layout.createParallelGroup(Alignment.TRAILING)
         .addGroup(layout.createSequentialGroup()
         .addContainerGap(188, Short.MAX_VALUE)
         .addComponent(toggleButton)
         .addGap(44)
         .addComponent(lblMessage, GroupLayout.PREFERRED_SIZE, 15, GroupLayout.PREFERRED_SIZE)
         .addPreferredGap(ComponentPlacement.RELATED)
         .addComponent(pBar, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
         .addContainerGap())
        );
        this.setLayout(layout);
    }// </editor-fold>                        
    
    public void setMessage(String info) {
        lblMessage.setText(info);
    }

    public void setProgress(int percent) {
        pBar.setValue(percent);
    }
    
    public int getProgress() {
        return pBar.getValue();
    }  
    
    public boolean getStoppro() {
return stoppro;
}

    public javax.swing.JLabel getLblMessage() {
return lblMessage;
}

public void setLblMessage(javax.swing.JLabel lblMessage) {
this.lblMessage = lblMessage;
}

public javax.swing.JProgressBar getpBar() {
return pBar;
}

public void setpBar(javax.swing.JProgressBar pBar) {
this.pBar = pBar;
}
private boolean stoppro = false;
   private javax.swing.JLabel lblMessage;
private javax.swing.JProgressBar pBar;    
}


--------------------编程问答-------------------- SplashManager类

public class SplashManager{
    //This transparent color is used for wipe out background for repainting.
    private static Color transparentColor=new Color(0,0,0,0);
    //Graphics object obtained from splash screen
    private Graphics2D graphics;
    //The Swing component you are about to render on splash screen.
    private JComponent component;
    //The splash instance obtained from system.
    private SplashScreen splash=SplashScreen.getSplashScreen();
    //The splash bounds, which is identical to that of splash image.
    
    private Rectangle bounds;
    /** Creates a new instance of SplashManager */
    public SplashManager(JComponent comp) {
        if(splash==null)
            throw new IllegalArgumentException("Splash Screen not set!");
        //Initialize these fields
        bounds=splash.getBounds();
        graphics=splash.createGraphics();
        //Set the background to transparent so that later you can refresh your rendering.
        graphics.setBackground(transparentColor);
        component=comp;
        //Note you must disable the double-buffer feature of the component,
        //or else double-buffer image might
        //overwrite the splash image background.
        component.setDoubleBuffered(false);
        //Note this container component must be transparent so that the image is not covered.
        component.setOpaque(false);
        //Resize the container component to the exact size of the splash image.
        component.setSize(splash.getSize());
        //This is crucial. For a container usually use layout manager to layout their components.
        //Without swing system, you have to manually layout the children components.
        LayoutManager layout=component.getLayout();
        layout.layoutContainer(component);
       
    }
    /**
     * This method is counterpart of swing component's repaint.
     * The difference is that you have to call this method manually without
     * swing event system.
     */
    public void repaint(){
        //First only when splash is visible
        if(splash.isVisible()){
            //Wipe out background, note this is only to wipe out
            //the background of overlay buffered image, not that you
            //wipe out the splash image.
            graphics.clearRect(0, 0, bounds.width, bounds.height);
            //Delegate painting to swing component's painting method
            component.paint(graphics);
            //Again, you have to manually update the splash. All the above
            //work is down in background image, not directly to the screen.
            //update method flush these changes to the screen.
            splash.update();
            
        }
    }
    /**
     * Close the splash
     */
    public void closeSplash(){
        if(splash.isVisible())
            splash.close();
    }
}

--------------------编程问答-------------------- 源代码贴了出来,怎么没有人帮我顶呢。
补充:Java ,  Java SE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,