import java.awt.*;
import java.net.*;
import javax.swing.*;/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 *//**
 *
 * @author Administrator
 */
public class JSplashWindow extends JWindow implements Runnable{
    Thread spashThread=null;
    private JProgressBar progress;
    public JSplashWindow(){
       setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
        JPanel splash=new JPanel(new BorderLayout());
        URL url=getClass().getResource("/images/logol.jpg");
        if(url!=null){
            splash.add(new JLabel(new ImageIcon(url)), BorderLayout.SOUTH);
           pack();
        }
       progress=new JProgressBar(1,100);
        progress.setStringPainted(true);
        progress.setBorderPainted(false);
        progress.setString("Application is starting up...");
        progress.setBackground(Color.green);
        splash.add(progress,BorderLayout.NORTH);
         setContentPane(splash);
        Dimension screen=getToolkit().getScreenSize();
       pack();
        setLocation((screen.width-getSize().width)/2,(screen.height-getSize().height/2));
    }
    public void start(){
        this.toFront();
        Thread splashThread = new Thread(this);
        splashThread.start();
    }
            public void run() {
       show();
       try{
          for(int i=0;i<100;i++){
              Thread.sleep(50);
              progress.setValue(progress.getValue()+1);
          }
           
       }
       catch(Exception ex){
           ex.printStackTrace();
       }
       this.dispose();
    }
    static void showFrame(String title){
        JFrame frame=new JFrame(title);
     frame.setSize(500,500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize=frame.getSize();
    if(frameSize.height>screenSize.height){
        frameSize.height=screenSize.height;
    }
    if(frameSize.width>screenSize.width){
        frameSize.width=screenSize.width;
    }
    frame.setLocation((screenSize.width-frameSize.width)/2,(screenSize.height-frameSize.height)/2);
    try{
        Thread.sleep(5000);
    }
    catch(Exception ex){
        ex.printStackTrace();
    }
    frame.setVisible(true);
    }
    public static void main(String[] args){
        JSplashWindow splash=new JSplashWindow();
        splash.start();
        showFrame("Demo splash window");    }}为啥程序启动时显示扉页,图片logol.jpg总是显示一半在屏幕下方,另一半看不到,能不能把jpg放到jpnel下显示,请问怎么修改,谢谢

解决方案 »

  1.   

    帮你改例如,改的地方见红色部分,没有问题,我跑过程序啦!import java.awt.*;
    import java.net.*;
    import javax.swing.*;
    public class JSplashWindow extends JWindow implements Runnable {

    Thread spashThread = null;
    private JProgressBar progress; public JSplashWindow() {

    setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
    JPanel splash = new JPanel(new BorderLayout());

    URL url = this.getClass().getResource("./images/1.jpg");

    if (url != null) {

    splash.add(new JLabel(new ImageIcon(url)), BorderLayout.SOUTH);
    pack();
    }
    else
    {
    System.exit(1);
    }

    progress = new JProgressBar(1, 100);
    progress.setStringPainted(true);
    progress.setBorderPainted(false);
    progress.setString("Application is starting up...");
    progress.setBackground(Color.green);
    splash.add(progress, BorderLayout.NORTH);

    setContentPane(splash);
    pack();

    setLocation(this.getToolkit().getScreenSize().width / 2
    - this.getWidth() / 2, this.getToolkit().getScreenSize().height
    / 2 - this.getHeight() / 2);


    } public void start() {

    this.toFront();
    Thread splashThread = new Thread(this);
    splashThread.start();
    } public void run() {

    show();
    try {
    for (int i = 0; i < 100; i++) {
    Thread.sleep(50);
    progress.setValue(progress.getValue() + 1);
    } } catch (Exception ex) {
    ex.printStackTrace();
    }
    this.dispose();
    }


    /*

    static void showFrame(String title) {
    JFrame frame = new JFrame(title);
    frame.setSize(500, 500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = frame.getSize();
    if (frameSize.height > screenSize.height) {
    frameSize.height = screenSize.height;
    }
    if (frameSize.width > screenSize.width) {
    frameSize.width = screenSize.width;
    }
    frame.setLocation((screenSize.width - frameSize.width) / 2,
    (screenSize.height - frameSize.height) / 2);
    try {
    Thread.sleep(5000);
    } catch (Exception ex) {
    ex.printStackTrace();
    }
    frame.setVisible(true);
    }
    */ public static void main(String[] args) {

    JSplashWindow splash = new JSplashWindow();
    splash.start();

    //showFrame("Demo splash window"); }}