- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 Java (Swing) 表单从数据库加载大量数据。我想在程序实际加载时显示进度条。
我该怎么做?
代码如下:
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();
}
}
最佳答案
Java progress bar tutorial应该有帮助。
另一个示例可以在 SwingWorker 中找到java 文档条目。
不是调用 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());
}
}
});
关于java - Java 中的进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2738608/
我想做的是让 JTextPane 在 JPanel 中占用尽可能多的空间。对于我使用的 UpdateInfoPanel: public class UpdateInfoPanel extends JP
我在 JPanel 中有一个 JTextArea,我想将其与 JScrollPane 一起使用。我正在使用 GridBagLayout。当我运行它时,框架似乎为 JScrollPane 腾出了空间,但
我想在 xcode 中实现以下功能。 我有一个 View Controller 。在这个 UIViewController 中,我有一个 UITabBar。它们下面是一个 UIView。将 UITab
有谁知道Firebird 2.5有没有类似于SQL中“STUFF”函数的功能? 我有一个包含父用户记录的表,另一个表包含与父相关的子用户记录。我希望能够提取用户拥有的“ROLES”的逗号分隔字符串,而
我想使用 JSON 作为 mirth channel 的输入和输出,例如详细信息保存在数据库中或创建 HL7 消息。 简而言之,输入为 JSON 解析它并输出为任何格式。 最佳答案 var objec
通常我会使用 R 并执行 merge.by,但这个文件似乎太大了,部门中的任何一台计算机都无法处理它! (任何从事遗传学工作的人的附加信息)本质上,插补似乎删除了 snp ID 的 rs 数字,我只剩
我有一个以前可能被问过的问题,但我很难找到正确的描述。我希望有人能帮助我。 在下面的代码中,我设置了varprice,我想添加javascript变量accu_id以通过rails在我的数据库中查找记
我有一个简单的 SVG 文件,在 Firefox 中可以正常查看 - 它的一些包装文本使用 foreignObject 包含一些 HTML - 文本包装在 div 中:
所以我正在为学校编写一个 Ruby 程序,如果某个值是 1 或 3,则将 bool 值更改为 true,如果是 0 或 2,则更改为 false。由于我有 Java 背景,所以我认为这段代码应该有效:
我做了什么: 我在这些账户之间创建了 VPC 对等连接 互联网网关也连接到每个 VPC 还配置了路由表(以允许来自双方的流量) 情况1: 当这两个 VPC 在同一个账户中时,我成功测试了从另一个 La
我有一个名为 contacts 的表: user_id contact_id 10294 10295 10294 10293 10293 10294 102
我正在使用 Magento 中的新模板。为避免重复代码,我想为每个产品预览使用相同的子模板。 特别是我做了这样一个展示: $products = Mage::getModel('catalog/pro
“for”是否总是检查协议(protocol)中定义的每个函数中第一个参数的类型? 编辑(改写): 当协议(protocol)方法只有一个参数时,根据该单个参数的类型(直接或任意)找到实现。当协议(p
我想从我的 PHP 代码中调用 JavaScript 函数。我通过使用以下方法实现了这一点: echo ' drawChart($id); '; 这工作正常,但我想从我的 PHP 代码中获取数据,我使
这个问题已经有答案了: Event binding on dynamically created elements? (23 个回答) 已关闭 5 年前。 我有一个动态表单,我想在其中附加一些其他 h
我正在尝试找到一种解决方案,以在 componentDidMount 中的映射项上使用 setState。 我正在使用 GraphQL连同 Gatsby返回许多 data 项目,但要求在特定的 pat
我在 ScrollView 中有一个 View 。只要用户按住该 View ,我想每 80 毫秒调用一次方法。这是我已经实现的: final Runnable vibrate = new Runnab
我用 jni 开发了一个 android 应用程序。我在 GetStringUTFChars 的 dvmDecodeIndirectRef 中得到了一个 dvmabort。我只中止了一次。 为什么会这
当我到达我的 Activity 时,我调用 FragmentPagerAdapter 来处理我的不同选项卡。在我的一个选项卡中,我想显示一个 RecyclerView,但他从未出现过,有了断点,我看到
当我按下 Activity 中的按钮时,会弹出一个 DialogFragment。在对话框 fragment 中,有一个看起来像普通 ListView 的 RecyclerView。 我想要的行为是当
我是一名优秀的程序员,十分优秀!