2010-04-29 50 views
2

我已经从Java(Swing)中获取了一个从数据库中加载大量数据的表单。我想在程序实际加载时显示进度条。Java中的进度条

我该怎么办?

的代码如下:

import java.awt.*; 
import javax.swing.*; 
import java.awt.event.*; 
import java.sql.*; 
import java.applet.*; 
import java.util.*; 
import java.awt.Color; 

public class bookshow extends JFrame implements MouseListener, ActionListener { 

    JPanel p1, p2, p3; 
    JLabel l1, l2, l3, l4; 
    ImageIcon image; 
    JButton b[] = new JButton[84]; 
    JButton btnocc, btnbook, btnsel; 
    ResultSet rs; 
    private JMenuBar menuBar = new JMenuBar(); 
    private JMenuItem exit, home, save; 
    private JMenu fileMenu, gotoMenu; 
    public int cnt = 0, x = 150, y = 90, i, j, put = 0, k = 0, avail = 1, point, x1, y1, count = 0, quan; 
    public static int prev_click = -1, xpos, ypos; 
    public static int prev[] = new int[5]; 
    public static int pos[][] = new int[5][2]; 
    public String movname, movdate, movtime; 

    public bookshow() { 
     avail = 1; 

     try { 

      String sql = "Select * from Pass"; 
      String url = "jdbc:odbc:tbs"; 
      Connection con = DriverManager.getConnection(url); 
      Statement s = con.createStatement(); 
      rs = s.executeQuery(sql); 
      while (rs.next()) { 
       movname = rs.getString("movname"); 
       movdate = rs.getString("movdate"); 
       movtime = rs.getString("movtime"); 
       quan = Integer.parseInt(rs.getString("Quantity")); 
      } 
     } catch (Exception ex) { 
      System.out.println(ex); 
     } 

     try { 

      String sql = "Delete from Pass"; 
      String url = "jdbc:odbc:tbs"; 
      Connection con = DriverManager.getConnection(url); 
      Statement s = con.createStatement(); 
      s.executeUpdate(sql); 
      con.close(); 
     } catch (Exception ex) { 
      System.out.println(ex); 
     } 

     setTitle("Select Ticket"); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 

     p1 = new JPanel(); 
     getContentPane().add(p1); 
     p1.setLayout(null); 
     p1.setBackground(Color.white); 

     setJMenuBar(menuBar); 

     fileMenu = new JMenu("File"); 
     menuBar.add(fileMenu); 

     exit = new JMenuItem("Exit"); 
     exit.addActionListener(this); 
     fileMenu.add(exit); 
     exit.setAccelerator(KeyStroke.getKeyStroke('E', Event.CTRL_MASK)); 

     save = new JMenuItem("Save"); 
     save.addActionListener(this); 
     fileMenu.add(save); 
     save.setAccelerator(KeyStroke.getKeyStroke('S', Event.CTRL_MASK)); 

     gotoMenu = new JMenu("Go To"); 
     menuBar.add(gotoMenu); 

     home = new JMenuItem("Home"); 
     //exit.addActionListener(this); 
     gotoMenu.add(home); 
     home.setAccelerator(KeyStroke.getKeyStroke('H', Event.CTRL_MASK)); 

     p2 = new JPanel(); 
     p2.setLayout(null); 
     p2.setBackground(Color.lightGray); 
     p2.setBounds(100, 50, 500, 20); 
     p1.add(p2); 

     l1 = new JLabel("GOLD"); 
     l1.setBounds(240, 0, 100, 20); 
     p2.add(l1); 
     l1.setBackground(Color.red); 

     try { 

      String sql = "Select * from Book"; 
      String url = "jdbc:odbc:tbs"; 
      Connection con = DriverManager.getConnection(url); 
      Statement s = con.createStatement(); 
      rs = s.executeQuery(sql); 
      while (rs.next()) { 

       String getname = rs.getString("MovName"); 
       String getdate = rs.getString("MovDate"); 
       String gettime = rs.getString("MovTime"); 

       if (movname.equalsIgnoreCase(getname) && movdate.equals(getdate) && movtime.equals(gettime)) { 
        put = 1; 
        break; 
       } 
      } 
     } catch (Exception ex) { 
      System.out.println(ex); 
     } 

     for (i = 1; i <= 4; i++) { 

      for (j = 1; j <= 14; j++) { 
       point = 0; 
       b[cnt] = new JButton(); 
       b[cnt].setBounds(x, y, 20, 20); 
       b[cnt].setBackground(Color.white); 
       p1.add(b[cnt]); 

       if (put == 0) { 
        try { 

         String sql = "Insert into Book values('" + movname + "','" + movtime + "','" + movdate + "'," + x + "," + y + ",1)"; 
         String url = "jdbc:odbc:tbs"; 
         Connection con = DriverManager.getConnection(url); 
         Statement s = con.createStatement(); 
         s.executeUpdate(sql); 
         con.close(); 
        } catch (Exception ex) { 
         System.out.println(ex); 
        } 
       } else { 

        try { 

         String sql = "Select * from Book"; 
         String url = "jdbc:odbc:tbs"; 
         Connection con = DriverManager.getConnection(url); 
         Statement s = con.createStatement(); 
         rs = s.executeQuery(sql); 
         while (rs.next()) { 

          String getname = rs.getString("MovName"); 
          String getdate = rs.getString("MovDate"); 
          String gettime = rs.getString("MovTime"); 
          avail = Integer.parseInt(rs.getString("Avail")); 

          if (movname.equalsIgnoreCase(getname) && movdate.equals(getdate) && movtime.equals(gettime)) { 
           k++; 
           if (avail == 0 && k == (cnt + 1)) { 
            b[cnt].setBackground(Color.red); 
            point = 1; 
            b[cnt].setEnabled(false); 
           } 
          } 
         } 
        } catch (Exception ex) { 
         System.out.println(ex); 
        } 
       } 

       if (point == 0) { 
        b[cnt].addMouseListener(this); 
       } 

       cnt++; 
       x = x + 30; 
       k = 0; 

      } 

      x = 150; 

      y = y + 40; 
     } 

     y = 300; 
     x = 150; 

     p3 = new JPanel(); 
     p3.setLayout(null); 
     p3.setBackground(Color.lightGray); 
     p3.setBounds(100, 260, 500, 20); 
     p1.add(p3); 

     l2 = new JLabel("ECONOMY"); 
     l2.setBounds(220, 0, 100, 20); 
     p3.add(l2); 
     l2.setBackground(Color.red); 

     for (i = 1; i <= 2; i++) { 

      for (j = 1; j <= 14; j++) { 
       point = 0; 

       b[cnt] = new JButton(); 
       b[cnt].setBounds(x, y, 20, 20); 
       b[cnt].setBackground(Color.white); 
       p1.add(b[cnt]); 

       if (put == 0) { 
        try { 
         String sql = "Insert into Book values('" + movname + "','" + movtime + "','" + movdate + "'," + x + "," + y + ",1)"; 
         String url = "jdbc:odbc:tbs"; 
         Connection con = DriverManager.getConnection(url); 
         Statement s = con.createStatement(); 
         s.executeUpdate(sql); 
         con.close(); 
        } catch (Exception ex) { 
         System.out.println(ex); 
        } 
       } else { 

        try { 

         String sql = "Select * from Book"; 
         String url = "jdbc:odbc:tbs"; 
         Connection con = DriverManager.getConnection(url); 
         Statement s = con.createStatement(); 
         rs = s.executeQuery(sql); 
         while (rs.next()) { 

          String getname = rs.getString("MovName"); 
          String getdate = rs.getString("MovDate"); 
          String gettime = rs.getString("MovTime"); 
          avail = Integer.parseInt(rs.getString("Avail")); 

          if (movname.equalsIgnoreCase(getname) && movdate.equals(getdate) && movtime.equals(gettime)) { 
           k++; 
           if (avail == 0 && k == (cnt + 1)) { 
            b[cnt].setBackground(Color.red); 
            point = 1; 
            b[cnt].setEnabled(false); 
           } 
          } 
         } 
        } catch (Exception ex) { 
         System.out.println(ex); 
        } 
       } 

       if (point == 0) { 
        b[cnt].addMouseListener(this); 
       } 

       cnt++; 
       x = x + 30; 
       k = 0; 
      } 
      x = 150; 

      y = y + 40; 
     } 

     image = new ImageIcon("screen.jpg"); 

     l3 = new JLabel(image); 
     l3.setBounds(70, y + 20, 600, 54); 
     p1.add(l3); 

     l4 = new JLabel("Seat to Occupy"); 
     l4.setBounds(140, 490, 100, 15); 
     p1.add(l4); 

     btnocc = new JButton(); 
     btnocc.setBounds(260, 490, 15, 15); 
     btnocc.setBackground(Color.white); 
     p1.add(btnocc); 

     l4 = new JLabel("Booked seat"); 
     l4.setBounds(320, 490, 80, 15); 
     p1.add(l4); 

     btnbook = new JButton(); 
     btnbook.setBounds(410, 490, 15, 15); 
     btnbook.setBackground(Color.red); 
     p1.add(btnbook); 

     l4 = new JLabel("Current selection"); 
     l4.setBounds(460, 490, 100, 15); 
     p1.add(l4); 

     btnsel = new JButton(); 
     btnsel.setBounds(570, 490, 15, 15); 
     btnsel.setBackground(Color.green); 
     p1.add(btnsel); 

     setSize(770, 650); 
     setVisible(true); 
    } 

    public void mousePressed(MouseEvent e) { 
    } 

    public void mouseReleased(MouseEvent e) { 
    } 

    public void mouseEntered(MouseEvent e) { 
     Cursor mycursor = new Cursor(Cursor.HAND_CURSOR); 
     setCursor(mycursor); 

    } 

    public void mouseExited(MouseEvent e) { 
     Cursor mycursor = new Cursor(Cursor.DEFAULT_CURSOR); 
     setCursor(mycursor); 
    } 

    public void mouseClicked(MouseEvent e) { 
     int row, col, ex; 

     for (i = 0; i < cnt; i++) { 
      if (e.getSource().equals(b[i])) { 

       if (count < quan) { 
        prev[count] = i; 
        b[prev[count]].setBackground(Color.green); 

        row = i/14; 
        col = i % 14; 

        x1 = 150 + (30 * col); 

        if (row >= 0 && row <= 3) { 
         y1 = 90 + (40 * row); 
        } else { 
         y1 = 300 + (40 * (row - 4)); 
        } 

        pos[count][0] = x1; 
        pos[count][1] = y1; 

        count++; 
       } else { 
        ex = prev[0]; 
        b[ex].setBackground(Color.white); 

        for (int j = 0; j <= (count - 1); j++) { 
         prev[j] = prev[j + 1]; 
         pos[j][0] = pos[j + 1][0]; 
         pos[j][1] = pos[j + 1][1]; 
        } 

        prev[count - 1] = i; 

        row = i/14; 
        col = i % 14; 

        x1 = 150 + (30 * col); 

        if (row >= 0 && row <= 3) { 
         y1 = 90 + (40 * row); 
        } else { 
         y1 = 300 + (40 * (row - 4)); 
        } 

        pos[count - 1][0] = x1; 
        pos[count - 1][1] = y1; 

        for (int j = 0; j <= (count - 1); j++) { 
         b[prev[j]].setBackground(Color.green); 
        } 
        count = quan; 
       } 

      } 
     } 
    } 

    public void actionPerformed(ActionEvent e) { 

     if (e.getSource() == save) { 
      if (count != quan) { 
       JOptionPane.showMessageDialog(null, "Please select the exact numbers of seat", "Error", JOptionPane.ERROR_MESSAGE); 
      } else { 

       for (i = 0; i < count; i++) { 

        try { 

         String sql = "Update Book set Avail=0 where MovName='" + movname + "' AND MovDate='" + movdate + "' AND MovTime='" + movtime + "' AND X=" + pos[i][0] + " AND y=" + pos[i][1] + " "; 
         String url = "jdbc:odbc:tbs"; 
         Connection con = DriverManager.getConnection(url); 
         Statement s = con.createStatement(); 
         s.executeUpdate(sql); 
         con.close(); 
        } catch (Exception ex) { 
         System.out.println(ex); 
        } 

        setVisible(false); 
        bookmain bm = new bookmain(); 
       } 
      } 
     } 
    } 

    public static void main(String args[]) { 
     bookshow mm = new bookshow(); 
    } 
} 
+1

重新格式化的代码;如果不正确请回复。 – trashgod 2010-04-29 16:32:45

回答

1

的JProgressBar是你的选择

+1

你能解释我该怎么做吗,因为我无法使用JProgressBar – user161004 2010-04-29 15:35:02

+1

查看下面的链接戈登 – Daniil 2010-04-29 15:36:01

8

Java progress bar tutorial应该有所帮助。

另一个例子可以在SwingWorker java docs条目中找到。

而不是打电话nextPrimeNumber()它可能在你的情况processNextBook()

为了弄清楚进展情况,您可以使用类似的想法,以便将变量变为代码中有意义的变量,例如。 setProgress(100 * booksProcessed.size()/books.size());

代码波纹管应该给你基本的想法如何实现你的期望。

class PrimeNumbersTask extends 
     SwingWorker<List<Integer>, Integer> { 
    PrimeNumbersTask(JTextArea textArea, int numbersToFind) { 
     //initialize 
    } 

    @Override 
    public List<Integer> doInBackground() { 
     while (! enough && ! isCancelled()) { 
       number = nextPrimeNumber(); 
       publish(number); 
       setProgress(100 * numbers.size()/numbersToFind); 
      } 
     } 
     return numbers; 
    } 

    @Override 
    protected void process(List<Integer> chunks) { 
     for (int number : chunks) { 
      textArea.append(number + "\n"); 
     } 
    } 
} 

JTextArea textArea = new JTextArea(); 
final JProgressBar progressBar = new JProgressBar(0, 100); 
PrimeNumbersTask task = new PrimeNumbersTask(textArea, N); 
task.addPropertyChangeListener(
    new PropertyChangeListener() { 
     public void propertyChange(PropertyChangeEvent evt) { 
      if ("progress".equals(evt.getPropertyName())) { 
       progressBar.setValue((Integer)evt.getNewValue()); 
      } 
     } 
    }); 
+0

我用上面的页面。我对任务参数有疑问,我应该如何使用以获得程序执行的长度 – user161004 2010-04-29 15:37:41

+2

任务长度可以是任意数字。在大多数情况下,它将被设置为100,最小值为0,因此可以用百分比来衡量进度。 – Gordon 2010-04-29 15:51:46

+0

对不起,能否更准确地解释一下,因为我无法使用JProgressbar – user161004 2010-04-29 15:57:57