2

我正在编写一个转换文件的小程序,我想弹出一个框,要求用户在程序循环并转换所有相关文件时请稍候,但我遇到了一个小问题。弹出的框应该有一个 JLabel 和一个 JButton,而用户正在“等待”我想显示一条消息说请稍候,以及一个禁用的“OK”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");
    }

}
4

2 回答 2

1

好的,这就是问题所在。您正在扩展 JFrame 。这意味着您的课程是 JFrame。

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

于 2012-08-09T19:26:53.020 回答
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();
  }
}

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

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");
  }

}
于 2012-08-09T21:21:16.757 回答