2015-09-06 33 views
0

我有一個簡單的帶有「Display」按鈕的窗體,點擊時讀取「content.txt」文件的內容並在隱藏的標籤中顯示文本並在按鈕被點擊時變得可見。當netbeans項目轉換爲可執行文件時,JLabel.setVisible不起作用

該項目作爲netbeans項目運行時工作。通過netbeans IDE,我構建並將項目打包成jar。當作爲jar被執行時,這個基本的功能是看不到的。

這裏是完整的代碼和「content.txt」駐留在src文件夾中。

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.IOException; 
import java.net.URISyntaxException; 
import java.net.URL; 

public class TestForm extends javax.swing.JFrame { 

    public TestForm() { 
     initComponents(); 
     jLabel1.setVisible(false); 
    } 

    @SuppressWarnings("unchecked") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code">       
    private void initComponents() { 

     jButton1 = new javax.swing.JButton(); 
     jLabel1 = new javax.swing.JLabel(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     jButton1.setText("Display"); 
     jButton1.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       jButton1ActionPerformed(evt); 
      } 
     }); 

     jLabel1.setBorder(new javax.swing.border.MatteBorder(null)); 

     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() 
         .addGap(155, 155, 155) 
         .addComponent(jButton1)) 
        .addGroup(layout.createSequentialGroup() 
         .addGap(119, 119, 119) 
         .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE))) 
       .addContainerGap(156, Short.MAX_VALUE)) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addGap(37, 37, 37) 
       .addComponent(jButton1) 
       .addGap(51, 51, 51) 
       .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addContainerGap(89, Short.MAX_VALUE)) 
     ); 

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

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

     try { 
      URL dir_url = ClassLoader.getSystemResource("content.txt"); 
      File file = new File(dir_url.toURI()); 

      BufferedReader reader = new BufferedReader(new FileReader(file)); 
      String line = null; 
      StringBuilder stringBuilder = new StringBuilder(); 
      String ls = System.getProperty("line.separator"); 

      while ((line = reader.readLine()) != null) { 
       stringBuilder.append(line); 
       stringBuilder.append(ls); 
      } 

      jLabel1.setText(stringBuilder.toString()); 

      jLabel1.setVisible(true); 
     } 
     catch(URISyntaxException ex){ 
      ex.printStackTrace(); 
     } 
     catch(FileNotFoundException ex){ 
      ex.printStackTrace(); 
     } 
     catch(IOException ex){ 
      ex.printStackTrace(); 
     } 

    }           

    /** 
    * @param args the command line arguments 
    */ 
    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(TestForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (InstantiationException ex) { 
      java.util.logging.Logger.getLogger(TestForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (IllegalAccessException ex) { 
      java.util.logging.Logger.getLogger(TestForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
      java.util.logging.Logger.getLogger(TestForm.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 TestForm().setVisible(true); 
      } 
     }); 
    } 

    // Variables declaration - do not modify      
    private javax.swing.JButton jButton1; 
    private javax.swing.JLabel jLabel1; 
    // End of variables declaration     
} 

當我運行可執行jar時,它不會給我一個錯誤,但帶有內容的標籤不會顯示。

當我使用命令行運行jar然後我得到以下異常。

Exception in thread "main" java.lang.UnsupportedClassVersionError: TestForm : Unsupported major.minor version 52.0 
    at java.lang.ClassLoader.defineClass1(Native Method) 
    at java.lang.ClassLoader.defineClass(ClassLoader.java:800) 
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) 
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:449) 
    at java.net.URLClassLoader.access$100(URLClassLoader.java:71) 
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361) 
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425) 
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358) 
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482) 
+0

三件事情,請嘗試再次調用'pack';嘗試調用'revalidate'和/或'repaint'和組合(更改順序) – MadProgrammer

+1

您是否曾嘗試從命令提示符運行JAR以查看它是否打印任何內容? – immibis

+0

@MadProgrammer:我嘗試了不同的組合。但沒有任何工作。 –

回答

1

的「java.lang.UnsupportedClassVersionError錯誤」,是由於這樣的事實,我的NetBeans是使用Java 8,我試圖在終端中運行其上的Java 7

運行一次,我解決了上述問題,並能夠在終端中運行jar文件我得到了「java.lang.IllegalArgumentException:URI不分層」,這是因爲我試圖訪問資源「content.txt」,這是在該jar文件。

我在代碼中對文本文件的內容進行了以下更改,並修復了問題。

InputStream stream = getClass().getResourceAsStream("content.txt"); 

BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); 
+0

祝賀解決這兩個問題。 +1用於回報。 :) –

相關問題