我的jFrame没有出现

时间:2013-09-17 03:19:20

标签: java swing jframe

贝娄是我的代码。有些原因我的jFrame不会出现。以前有人出场吗?剂量与它有关         java.awt.EventQueue.invokeLater(new Runnable() 我是java的新手,而且伸出到InvokeLater的GUI似乎会阻止它加载,因为我看不到任何可以告诉它稍后加载的东西。

    package accountBuilderGUI;


import java.io.File;
import java.io.IOException;

import jxl.Cell;
import jxl.CellType;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;


public class accountBuilderGUI extends javax.swing.JFrame {

/**
 * Creates new form accountBuilderGUI
 */
public accountBuilderGUI() {
    initComponents();
}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
public class ReadExcel {

private String inputFile;

public void setInputFile(String inputFile) {
this.inputFile = inputFile;
}

public void read() throws IOException  {
File inputWorkbook = new File(inputFile);
Workbook w;
try {
  w = Workbook.getWorkbook(inputWorkbook);
  // Get the first sheet
  Sheet sheet = w.getSheet(0);
  // Loop over first 10 column and lines

  for (int j = 0; j < sheet.getColumns(); j++) {
    for (int i = 0; i < sheet.getRows(); i++) {
      Cell cell = sheet.getCell(j, i);
      CellType type = cell.getType();
      if (type == CellType.LABEL) {
        System.out.println("I got a label "
            + cell.getContents());
      }

      if (type == CellType.NUMBER) {
        System.out.println("I got a number "
            + cell.getContents());
      }

    }
  }
} catch (BiffException e) {
  e.printStackTrace();
}
}
}
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    welcomeMessage = new javax.swing.JLabel();
    excelFilePath = new javax.swing.JTextField();
    Start = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setTitle("accountBuilderGUI");

    welcomeMessage.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
    welcomeMessage.setText("Welcome to Blackburns time clock account creater!");

    excelFilePath.setText("Please enter the file path to the excell document.");
    excelFilePath.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            excelFilePathActionPerformed(evt);
        }
    });

    Start.setText("Start");
    Start.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            StartActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(excelFilePath, javax.swing.GroupLayout.PREFERRED_SIZE, 461, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createSequentialGroup()
                    .addGap(193, 193, 193)
                    .addComponent(Start))
                .addGroup(layout.createSequentialGroup()
                    .addGap(38, 38, 38)
                    .addComponent(welcomeMessage)))
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(welcomeMessage)
            .addGap(18, 18, 18)
            .addComponent(excelFilePath, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(18, 18, 18)
            .addComponent(Start)
            .addContainerGap(28, Short.MAX_VALUE))
    );

    pack();
}// </editor-fold>                        

private void excelFilePathActionPerformed(java.awt.event.ActionEvent evt) {                                              

}                                             

private void StartActionPerformed(java.awt.event.ActionEvent evt) {                                      
     String filePathToExcel = excelFilePath.getText();
     ReadExcel test = new ReadExcel();
     test.setInputFile(filePathToExcel);

     try{
     test.read();
     }catch(IOException e){
     e.printStackTrace();
     }
}                                     

public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(accountBuilderGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(accountBuilderGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(accountBuilderGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(accountBuilderGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new accountBuilderGUI().setVisible(true);
        }
    });
}
// Variables declaration - do not modify                     
private javax.swing.JButton Start;
private javax.swing.JTextField excelFilePath;
private javax.swing.JLabel welcomeMessage;
// End of variables declaration                   
}

1 个答案:

答案 0 :(得分:1)

这在我的Windows 7 / Java 7计算机上运行良好。我下载了jxl库并将其添加到我的构建路径中,并按预期运行窗口。

根据EventQueue documentation

  

public static void invokeLater(Runnable runnable)
  导致runnable在系统的调度线程中调用其run方法   EventQueue中。这将在处理完所有待处理事件后发生。

所以你不需要“以后”调用它;它将自动运行,并按Runnables添加到事件队列的顺序运行。