我在eclipse中使用Window Builder来创建一个项目。我正在尝试添加启动画面。我收到错误类型进度栏无法解决

时间:2017-04-26 16:27:14

标签: java eclipse swing

我在eclipse中使用Window Builder进行GUI项目。我正在尝试添加一个带有进度条的启动画面,在加载到100%后进入主页。我得到错误类型进度条无法在运行时解析()

import java.awt.EventQueue;    
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import java.awt.Color;
import javax.swing.UIManager;
import javax.swing.JProgressBar;
import javax.swing.ProgressMonitor;

import java.awt.SystemColor;
import java.sql.Connection;

import javax.swing.border.LineBorder;
import javax.swing.plaf.ProgressBarUI;

@SuppressWarnings("serial")
public class Loading extends JFrame implements Runnable{

    Connection conn;
    int s=0;
    Thread th;



    private JPanel contentPane;



    /**
     * Create the frame.
     */
    public Loading() {

        super("Loading");
        th = new Thread((Runnable) this);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 540, 363);
        contentPane = new JPanel();
        contentPane.setBorder(new LineBorder(new Color(250, 128, 114), 2));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel lblSmartLibrary = new JLabel("# Library");
        lblSmartLibrary.setForeground(UIManager.getColor("RadioButtonMenuItem.selectionBackground"));
        lblSmartLibrary.setFont(new Font("Times New Roman", Font.PLAIN, 20));
        lblSmartLibrary.setBounds(225, 43, 144, 24);
        contentPane.add(lblSmartLibrary);

        JProgressBar progressBar = new JProgressBar();
        progressBar.setValue(0);
        progressBar.setStringPainted(true);
        progressBar.setForeground(new Color(135, 206, 250));
        progressBar.setBackground(SystemColor.window);
        progressBar.setBounds(190, 151, 150, 32);
        contentPane.add(progressBar);

        JLabel lblPleaseWait = new JLabel("Please wait...");
        lblPleaseWait.setBounds(225, 182, 88, 16);
        contentPane.add(lblPleaseWait);

    }

    public void setupLoading(){
        setVisible(false);
        th.start();
    }

    public void run(){
        try{
            for (int i=1; i<= 200; i++){
                s = s + 1;
           //The error is Here.Type progressBar cannot be resolved                  
                int m = progressBar.getMaximum();
                int v= progressBar.getValue();
                if(v<m){
                    progressBar.setValue(progressBar.getValue()+1);

                }else{
                    i=201;
                    setVisible(false);
                    Home ob = new Home();
                    ob.setVisible(true);
                }Thread.sleep(50);
            }
        }catch (Exception e){
            JOptionPane.showMessageDialog(null, e);
        }

    }

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                 try {
                    Loading frame = new Loading();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

2 个答案:

答案 0 :(得分:0)

没有名为progressBar [在可见范围内]的变量或字段。因此,语法的下一个可能的解释是它是对名为progressBar的类的一些静态方法的调用。如果那个类不存在,那就是你得到的错误。

答案 1 :(得分:0)

将我的代码更改为以下内容并且有效。

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.UIManager;
import javax.swing.border.LineBorder;
import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import java.sql.Connection;

import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
import javax.swing.SwingConstants;
import java.awt.Window.Type;


public class Loading extends JFrame{

    Connection conn;
    private Task task;
    private JProgressBar progressBar;


    private JPanel contentPane;


    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Loading frame = new Loading();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Loading() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JPanel panel = new JPanel();
        panel.setBorder(new LineBorder(new Color(153, 204, 255), 3));
        panel.setForeground(UIManager.getColor("ProgressBar.background"));
        panel.setBounds(30, 26, 391, 220);
        contentPane.add(panel);
        panel.setLayout(null);

        JLabel lblLibrary = new JLabel("Loading");
        lblLibrary.setHorizontalAlignment(SwingConstants.CENTER);
        lblLibrary.setFont(new Font("Lucida Grande", Font.PLAIN, 20));
        lblLibrary.setBounds(146, 23, 107, 45);
        panel.add(lblLibrary);

        progressBar = new JProgressBar(0,100);
        lblLibrary.setLabelFor(progressBar);
        progressBar.setStringPainted(true);
        progressBar.setValue(0);
        progressBar.setForeground(new Color(0, 51, 255));
        progressBar.setBounds(120, 80, 146, 20);
        panel.add(progressBar);

        JLabel lblPleaseWait = new JLabel("Please wait...");
        lblPleaseWait.setBounds(146, 109, 81, 16);
        panel.add(lblPleaseWait);
    }

    public void setUpLoading(){
        setVisible(false);
        task = new Task();
        task.start();
        ;
    }


    private class Task extends Thread {    
        public Task(){
          }
          public void run(){
             for(int i =0; i<= 100; i+=1){
                final int progress = i;

                SwingUtilities.invokeLater(new Runnable() {
                   public void run() {
                      progressBar.setValue(progress); 
                   }
                });
                   if(progress==100){
                       setVisible(false);
                       Home ob = new Home();
                       ob.setVisible(true);
                   }
                try {
                   Thread.sleep(100);
                } catch (InterruptedException e) {}
             }
          }
       }
    }