2012-07-21 38 views
2

這是什麼導致我無法在此代碼中使用'strAppName'變量?我現在在這裏評論顯示/*strAppName*/; JFrame frame = new JFrame(/*strAppName*/);我該如何訪問本地.properties變量? Java Swing正確的變量使用

當我運行System.out.println(strAppName);時,它顯示在Eclipse控制檯中並帶有應用程序名稱。謝謝!

package base; 

import java.awt.BorderLayout; 
import java.awt.FlowLayout; 
import java.io.*; 
import java.util.*; 

import javax.swing.JFrame; 
import javax.swing.JOptionPane; 
import javax.swing.SwingUtilities; 

public class StickyNotes extends JFrame { 

    private static final long serialVersionUID = 1L; 

    public final static String SAVE_CHANGES = "Save changes?"; 
    public final static String TITLE_AYS = "Are You Sure?"; 

    private void createStickyNotesGUI() { 

     Properties configProperties = new Properties(); 
     try { 

      FileInputStream fileInputStream = new FileInputStream("resources/config.properties"); 
      configProperties.load(fileInputStream);   
      String strAppName = configProperties.getProperty("appName"); 
      //System.out.println(strAppName); 
      fileInputStream.close(); // better in finally block ?? /* http://en.wikipedia.org/wiki/.properties */ 
     } catch (Exception ex){ 
      //TODO 
      System.out.println("Exception: " + ex); 
     } 

/*  LoadPropertiesExample config = new LoadPropertiesExample(); 
     config.loadProps2(); 
     config.sayHello();*/ 

     // Create and set up the window. 
     JFrame frame = new JFrame(/*strAppName*/); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setLayout(new FlowLayout(FlowLayout.LEFT)); 

     // Add Main Menu 
     MainMenuBar mainMenu = new MainMenuBar(); 
     frame.setJMenuBar(mainMenu.createMainMenuBar(frame)); 

     // Add Content Pane // can I pass a layout object? 
     frame.setContentPane(ContentPaneCreator.createContentPane()); 
     // contentPane.add(scrollPane, BorderLayout.CENTER); 

     // Add Tool Bar 
     ToolBarCreator toolBar = new ToolBarCreator(); 
     frame.getContentPane().add(toolBar.createToolBar(), BorderLayout.NORTH); 

     // Add Label 
     frame.getContentPane().add(
       LabelCreator.createLabel(frame, 
         "use Swing and JavaFX together."), BorderLayout.NORTH); 

     // Display the window. 
     frame.setExtendedState(JFrame.MAXIMIZED_BOTH); 
     // TODO 
     /*set configuration to remember frame size*/ 

     frame.setVisible(true); 
    } 

    public void doExit(JFrame frame) { 
     boolean fDirty = true; 
     if (fDirty) 
      switch (JOptionPane.showConfirmDialog(StickyNotes.this, SAVE_CHANGES, 
        TITLE_AYS, JOptionPane.YES_NO_OPTION)) { 
      case JOptionPane.YES_OPTION: 
       // if (doSave()) 
       frame.dispose(); 
       break; 
      case JOptionPane.NO_OPTION: 
       frame.dispose(); 
      } 
     else 
      frame.dispose(); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       new StickyNotes().createStickyNotesGUI(); 
      } 
     }); 
    } 
} 

回答

4

你不能在你的JFrame構造函數中使用strAppName,因爲它超出範圍。你在你的try/catch塊中聲明它。

試試這個:

String strAppName = null; 
    try { 

     FileInputStream fileInputStream = new FileInputStream("resources/config.properties"); 
     configProperties.load(fileInputStream);   
     strAppName = configProperties.getProperty("appName"); 
     //System.out.println(strAppName); 
     fileInputStream.close(); // better in finally block ?? /* http://en.wikipedia.org/wiki/.properties */ 
    } catch (Exception ex){ 
     //TODO 
     System.out.println("Exception: " + ex); 
    } 
+0

沒錯,1+。對於@jojo,是的,使用finally塊關閉您的Stream。如果發生異常,但是您希望應用程序關閉嗎?或者也許使用JFrame的替代構造函數? – 2012-07-21 16:06:42