程序循环并转换所有相关文件时会弹出一个框,要求用户稍候

问题描述 投票:0回答:2

我正在编写一个转换文件的小程序,我想弹出一个框,要求用户在程序循环并转换所有相关文件时等待,但我遇到了一个小问题。弹出的框应该有一个 JLabel 和一个 JButton,当用户“等待”时,我想显示一条消息,表示请稍候,并禁用“确定”JButton,然后当其完成时,我想设置文本JLabel 的信息让他们知道它已成功转换他们的文件,并统计已转换的文件数量。 (我写了一个名为alert的方法,它设置标签的文本并启用按钮。)问题是,当程序运行时,框是空的,标签和按钮不可见,当它完成时,标签出现与我想要的最终文本和按钮显示为已启用。我不确定到底发生了什么,我尝试多次更改 JLabel 和 JButton 的修饰符,但我似乎无法让它正常工作。这是弹出框的代码,任何帮助都非常有用。

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class PleaseWait extends javax.swing.JFrame{

    private static final int height = 125;
    private static final int width = 350;
    final static JLabel converting = new JLabel("Please Wait while I convert your files");
    private static JButton OK = new JButton("OK");



    public PleaseWait(){
        // creates the main window //
        JFrame mainWindow = new JFrame();
        mainWindow.setTitle("Chill For A Sec");
        mainWindow.setSize(width, height);
        mainWindow.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

        // creates the layouts//
        JPanel mainLayout = new JPanel(new BorderLayout());
        JPanel textLayout = new JPanel(new FlowLayout());
        JPanel buttonLayout = new JPanel(new FlowLayout());

        // Sets Text //
        converting.setText("Please wait while I convert your files");

        // disables button //
        OK.setEnabled(false);

        // adds to the layouts //
        textLayout.add(converting);
        buttonLayout.add(OK);
        mainLayout.add(textLayout, BorderLayout.CENTER);
        mainLayout.add(buttonLayout, BorderLayout.SOUTH);

        // adds to the frame //
        mainWindow.add(mainLayout);

        // sets everything visible //
        mainWindow.setVisible(true);

    }

    public static void alert(){
        OK.setEnabled(true);
        String total = String.valueOf(Convert.result());
        converting.setText("Sucsess! " + total + " files Converted");
    }

}
java swing user-interface jbutton jlabel
2个回答
1
投票

好吧,问题就在这里。您正在扩展 JFrame 。这意味着你的类是一个 JFrame。

当您创建

PleaseWait
框架时,您无需对其执行任何操作。这是您看到的空盒子。相反,您在构造函数中创建了一个 different JFrame。删除您的
mainWindow
,而只使用
this
。现在,所有组件都将添加到您的
PleaseWait
对象中。这应该可以解决您的空白框问题。


1
投票

您需要一个应用程序来首先创建框架。这是此类应用的一个简单示例。

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

public class Application {
  boolean packFrame = false;

  //Construct the application
  public Application() {
    PleaseWait frame = new PleaseWait();
    //Validate frames that have preset sizes
    //Pack frames that have useful preferred size info, e.g. from their layout
    if (packFrame) {
      frame.pack();
    }
    else {
      frame.validate();
    }
    //Center the window
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = frame.getSize();
    if (frameSize.height > screenSize.height) {
      frameSize.height = screenSize.height;
    }
    if (frameSize.width > screenSize.width) {
      frameSize.width = screenSize.width;
    }
    frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
    frame.setVisible(true);

    frame.convert();

  }

  //Main method
  public static void main(String[] args) {
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }
    catch(Exception e) {
      e.printStackTrace();
    }
    new Application();
  }
}

您必须稍微修改框架才能将控件添加到内容窗格。您可以在框架创建后做一些工作,然后调用警报。

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class PleaseWait extends JFrame {

  private static final int height = 125;
  private static final int width = 350;
  final static JLabel converting = new JLabel();
  private static JButton OK = new JButton("OK");
  BorderLayout borderLayout1 = new BorderLayout();
  JPanel contentPane;
  int count;

  public PleaseWait(){
    contentPane = (JPanel)this.getContentPane();
    contentPane.setLayout(borderLayout1);
    this.setSize(new Dimension(width, height));
    this.setTitle("Chill For A Sec");
    this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

    // creates the layouts//
    JPanel mainLayout = new JPanel(new BorderLayout());
    JPanel textLayout = new JPanel(new FlowLayout());
    JPanel buttonLayout = new JPanel(new FlowLayout());

    // Sets Text //
    converting.setText("Please wait while I convert your files");

    // disables button //
    OK.setEnabled(false);
    OK.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e) {
        System.exit(0);
      }
    });

    // adds to the layouts //
    textLayout.add(converting);
    buttonLayout.add(OK);
    mainLayout.add(textLayout, BorderLayout.CENTER);
    mainLayout.add(buttonLayout, BorderLayout.SOUTH);

    // adds to the frame //
    contentPane.add(mainLayout);
  }

  public void convert(){
    count = 0;
    for (int i = 0; i <10; i++){
      System.out.println("Copy "+i);
      try {
        Thread.sleep(1000);
      } catch (InterruptedException e) {
      }
      count++;
    }
    alert();

  }
  public void alert(){
    OK.setEnabled(true);
//        String total = String.valueOf(Convert.result());
    converting.setText("Sucsess! " + count + " files Converted");
  }

}
© www.soinside.com 2019 - 2024. All rights reserved.