我想让我jProgressBar在期间更新它的价值HTTP File Upload.我是Java新手,我不确定我做的是正确的事情,这是我的代码:
private static final String Boundary = "--7d021a37605f0";
public void upload(URL url, File f) throws Exception
{
HttpURLConnection theUrlConnection = (HttpURLConnection) url.openConnection();
theUrlConnection.setDoOutput(true);
theUrlConnection.setDoInput(true);
theUrlConnection.setUseCaches(false);
theUrlConnection.setChunkedStreamingMode(1024);
theUrlConnection.setRequestProperty("Content-Type", "multipart/form-data; boundary="
+ Boundary);
DataOutputStream httpOut = new DataOutputStream(theUrlConnection.getOutputStream());
String str = "--" + Boundary + "\r\n"
+ "Content-Disposition: form-data;name=\"file1\"; filename=\"" + f.getName() + "\"\r\n"
+ "Content-Type: image/png\r\n"
+ "\r\n";
httpOut.write(str.getBytes());
FileInputStream uploadFileReader = new FileInputStream(f);
int numBytesToRead = 1024;
int availableBytesToRead;
jProgressBar1.setMaximum(uploadFileReader.available());
while ((availableBytesToRead = uploadFileReader.available()) …Run Code Online (Sandbox Code Playgroud) 如果我在doInBackground()中有一个属于其他类的方法,是否可以根据外部类的其他方法中发生的更改来设置setProgess()?
我需要一个摆动的真实进度条来显示一种情况下的温度和另一种情况下的电压.我怎么能做到这一点?
我可以将进度条设置为垂直模式吗?
我可以在只读模式下使用滑块控件吗?我想我可以简单地将滑块设置为当用户试图修改它时的实际值,但这看起来很笨拙.
有什么建议?
我想使用a JProgressBar和扩充它来打印它的当前值以及图形条.

我猜测最好的方法是覆盖paintComponent:
@Override protected void paintComponent(Graphics g) {
// Let component paint first
super.paintComponent(g);
// paint my contents next....
}
Run Code Online (Sandbox Code Playgroud)
但我不确定......有什么建议吗?
我有一个程序需要一些时间来创建 pdf 文件我想向用户显示进度
当我完成一个 pdf 文件时,我尝试调用进度条来更新它的状态:
ProgressDialog progress = new ProgressDialog(instance, numberOfInvoices);
progress.setVisible(true);
progress.setAlwaysOnTop(true);
for(int i = 0 ; i<numOfPdfs ; i++){
progress.change(i + 1);
}
Run Code Online (Sandbox Code Playgroud)
进度对话框如下所示:
public class ProgressDialog extends JDialog {
private JProgressBar progressBar;
public ProgressDialog(Window parent, int aantal) {
super(parent);
this.setPreferredSize(new Dimension(300, 150));
progressBar = new JProgressBar(0, aantal);
progressBar.setValue(0);
progressBar.setSize(new Dimension(280, 130));
this.add(progressBar, BorderLayout.CENTER);
this.pack();
this.setLocationRelativeTo(parent);
}
public void change(int aantal) {
if (aantal < progressBar.getMaximum() && aantal >= 0) {
progressBar.setValue(aantal);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我得到的只是一个空窗口(白色)
我在这个论坛上环顾四周,但踏板解决方案似乎太复杂了 …
我的代码如下不起作用,谁能告诉我为什么?还请更正我的代码,我是Java的新手.除此之外,我正在寻找"加载面板组件",类似ProgressMonitor但可能更具吸引力,并且动画更好.如果有人以前使用过这类东西,请建议我.
public class Main extends JFrame {
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main frame = new Main();
frame.setVisible(true);
ProgressMonitor pm = new ProgressMonitor(frame, "Loading...",
"waiting...",
0, 100000);
for (int i = 0 ; i < 100000 ; i ++){
pm.setProgress(i);
pm.setNote("Testing");
System.out.println(i);
Thread.sleep(1000);
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Main() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, …Run Code Online (Sandbox Code Playgroud) 所以我正在尝试使用进度条创建一个显示下载进度的下载程序.但我遇到问题,因为它实际上并没有更新进度条.基本上它保持白色,当它意味着蓝色.如果有人可以帮忙,代码如下.
JProgressBar progressBar = new JProgressBar(0, ia);
con.add(progressBar, BorderLayout.PAGE_START);
con.validate();
con.repaint();
progressBar = new JProgressBar(0, ia);
progressBar.setValue(0);
System.out.print("Downloading Files");
while ((count = in.read(data, 0, downloadSpeed)) != -1){
fout.write(data, 0, count);
if (count >= 2){
progressBar.setString("Downloading : " + ia + " @ " + count + "Kbs per second");
} else {
progressBar.setString("Downloading : " + ia + " @ " + count + "Kb per second");
}
progressBar.setValue(count);
con.add(progressBar, BorderLayout.PAGE_START);
try{
Thread.sleep(1000);
} catch (Exception e){}
}
Run Code Online (Sandbox Code Playgroud) 我有一个JFrame进度条和我的代码,它应该在后台运行它显示进度条中的进度.
我在Progressbar类中实现了runnable并启动了线程.但进度条框架没有显示完整..并且它被卡住并且在我的代码完全执行后显示已满,即在主线程关闭后.
我知道这是一个基本错误.
public class ProgressScriptUI extends JFrame implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
setTitle("Progressing to Generate DDL Scripts");
setBounds(400, 250, 850, 400);
getContentPane().setLayout(null);
JProgressBar progressBar= new JProgressBar(0,100);
progressBar.setBounds(200, 100, 500, 20);
add(progressBar);
setVisible(true);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
//I am calling the below code in some other class
ProgressScriptUI progress = new ProgressScriptUI();
Thread uiThread = new Thread(progress);
uiThread.start();
oracleValidateOLDorNEW.execute(); //Code that I need to call in back ground
Run Code Online (Sandbox Code Playgroud) java swing multithreading jprogressbar event-dispatch-thread
我不是新手,也是教授.在Java上.我正在尝试将progressBar添加到我的应用程序中,该应用程序使用isReachable()方法将ping发送到给定的ip范围.我该如何添加?我不知道任务和线程使用情况.我阅读了有关progressBar的java文档,但我无法添加.这是我的代码
final JButton btnScan = new JButton("Scan");
btnScan.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Runtime rt = Runtime.getRuntime();
String lastIpCheck = " ";
String ip = textField.getText();
String lastIp = textField_1.getText();
String parsedOutput= " ";
InetAddress inet;
boolean reachable;
while(!(lastIpCheck.equalsIgnoreCase(lastIp))) {
try {
inet = InetAddress.getByName(ip);
reachable = inet.isReachable(2500);
String output=null;
lastIpCheck = f.nextIpAddress(ip);
if(reachable) {
model.addElement(ip);
}
ip = lastIpCheck;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} …Run Code Online (Sandbox Code Playgroud) 我正在为程序添加一个功能,以将一些内容保存到文件中.进度条(在其自己的JFrame中)显示进度,但进度条仅显示在它读取的最后一个值上.我有一个由主线程更新的全局,表示已完成工作的百分比,另一个线程读取此全局并相应地更新进度条.现在,当它运行时,JFrame为空,然后活动完成,然后进度条显示自己的完整数量.如何让它随着时间的推移更新进度(并从头开始显示JProgressbar)?这是我的代码:
public class GenomeAnnotator{
private JProgressBar csvProgressBar;
private JFrame csvSaveLoadFrame; //for the progress bar
private Container csvCon;
private double csvPercentSaved; //% of work completed
public JFrame m_frame; //main program frame
....
public static void main(String[] args){
...
showGUI();
...
}
public void showGUI(){
...
JMenu file = new JMenu("File");
JMenu exptann = new JMenu("Export annotation..);
JMenuItem exptcsv = newJMenuItem("CSV format");
exptcsv.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
..determine output file + checks...
System.out.println("Writing to .csv file......");
csvSaveLoadFrame = …Run Code Online (Sandbox Code Playgroud) java ×10
jprogressbar ×10
swing ×9
swingworker ×2
file-upload ×1
java-io ×1
ping ×1
progress ×1