Java : ProgressBar does not display when click on the button

标签 java jframe jbutton jprogressbar

在我这些天开发的应用程序中,需要在单击“转换”按钮时显示进度栏,以下是我的代码,

public class Main extends JFrame{

private JPanel panel2 = null;
private JPanel panel2 = null;

JProgressBar progressBar = null;
JButton button = null;

Main f1 = null;
Main f = null;

public static void main(String[] args) {

try
{
       UIManager.setLookAndFeel(new SyntheticaBlackEyeLookAndFeel());

}
catch (Exception e)
{
   e.printStackTrace();
}
}
public void convert(){

            f = new Main();

    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(700, 400);
    f.setLocationRelativeTo(null);
    f.setTitle("Fromat Conversion");

    panel1 = new JPanel();
    panel1.setLayout(new BorderLayout());
    panel1.setForeground(Color.white);

            button = new JButton();
    button.setText("Convert");

    panel1.add(panel3, BorderLayout.SOUTH);

            f.setContentPane(panel1);
    f.setVisible(true);

            f1 =  new Main();
            f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f1.setSize(457, 100);
        f1.setTitle("Conversion Progress");
        f1.setLocationRelativeTo(null);

        panel2 = new JPanel();
        panel2.setLayout(new BorderLayout());
        panel2.setForeground(Color.white);

            progressBar = new JProgressBar();
        progressBar.setValue(35);
        progressBar.setStringPainted(true);

        panel2.add(label3, BorderLayout.WEST);
        panel2.add(progressBar, BorderLayout.SOUTH);

            f1.setContentPane(panel2);
        button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e){

                f1.setVisible(true);
  }
  });

  }

  }

但是当我单击按钮时,进度条框架不显示。

有人知道这是为什么吗?

谢谢

最佳答案

您的代码无法编译。

这实际上会编译并显示进度条。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Pipo extends JFrame{

private static JPanel panel1 = null;
private static JPanel panel2 = null;

static JProgressBar progressBar = null;
static JButton button = null;

static Pipo f1 = null;
static Pipo f = null;

public static void main(String[] args) {

    f = new Pipo();

    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(700, 400);
    f.setLocationRelativeTo(null);
    f.setTitle("Fromat Conversion");

    panel1 = new JPanel();
    panel1.setLayout(new BorderLayout());
    panel1.setForeground(Color.white);

    button = new JButton();
    button.setText("Convert");

    panel1.add(button, BorderLayout.SOUTH);

    f.setContentPane(panel1);
    f.setVisible(true);

    f1 =  new Pipo();

    f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f1.setSize(457, 100);
    f1.setTitle("Conversion Progress");
    f1.setLocationRelativeTo(null);

    panel2 = new JPanel();
    panel2.setLayout(new BorderLayout());
    panel2.setForeground(Color.white);

    progressBar = new JProgressBar();
    progressBar.setValue(35);
    progressBar.setStringPainted(true);

    panel2.add(progressBar, BorderLayout.SOUTH);

    f1.setContentPane(panel2);

    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e){
            f1.setVisible(true);
        }
  });

  }
}

关于Java : ProgressBar does not display when click on the button,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7035399/

相关文章:

java - 替换java中的所有单词

java - MigLayout 在窗口调整大小后根据百分比更新单元格大小

Java : Random Position of JButton

java - DouglasPeuckerSimplifier 用法

java - Spring Cloud 契约(Contract) : Multiple controllers

java - 如何捕获 Neo4j TransientException : LockClient

java - 为什么我的 Jlabels 或 Jpanels 没有显示?

java - 获取从一个 JFrame 到另一个带有 textArea 的弹出 JFrame 的设置文本

java - 如何使一个类中的按钮影响另一个类中的文本区域?

java - 重置按钮数组后无法更改java按钮颜色