简体   繁体   English

我的jFrame没有出现

[英]My jFrame is not appearing

Bellow is my code. 贝娄是我的代码。 Fort some reason my jFrame will not appear. 出于某种原因,我的jFrame将不会出现。 Has anyone appearance this before? 有人出现过吗? Dose it have anything to do with java.awt.EventQueue.invokeLater(new Runnable() I'm new to java and the GUI reaching out to InvokeLater seems like it would prevent it from loading at all because I can't see anything that would then tell it to load later. 剂量与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                   
}

This runs fine on my Windows 7/Java 7 machine. 这在我的Windows 7 / Java 7计算机上运行良好。 I downloaded the jxl library and added it to my build path, and it runs the window as expected. 我下载了jxl库并将其添加到我的构建路径中,它按预期运行窗口。

Per the EventQueue documentation : 根据EventQueue文档

public static void invokeLater(Runnable runnable) 公共静态无效invokeLater(Runnable runnable)
Causes runnable to have its run method called in the dispatch thread of the system EventQueue. 使runnable在系统EventQueue的调度线程中调用其run方法。 This will happen after all pending events are processed. 这将在所有未决事件处理后发生。

So you don't need to call it "later"; 因此,您无需将其称为“稍后”; it will be run automatically and in the order that Runnables are added to the Event Queue. 它会自动运行,并且将Runnables添加到事件队列的顺序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM