java - 如何访问本地 .properties 变量? Java Swing 变量的正确使用

标签 java swing variables try-catch

是什么导致我无法在此代码中使用“strAppName”变量?我现在对其进行了评论,在此处显示 /*strAppName*/JFrame 框架 = new JFrame(/*strAppName*/);

当我运行 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();
            }
        });
    }
}

最佳答案

您不能在 JFrame 构造函数中使用 strAppName,因为它超出了范围。您可以在 try/catch block 中声明它。

试试这个:

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

关于java - 如何访问本地 .properties 变量? Java Swing 变量的正确使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11593670/

相关文章:

java - 如何从jmenubar弹出jframe

java - 如何限制Joptionpane消息对话框条件提示一次?

python - 为什么我的变量会在 python 的嵌套 for 循环中被覆盖?

java - Spring 行为模拟

java - 当点击从 WorkManager 发送的通知时,使用导航组件启动特定 fragment

Java:我可以使用Redis db创建优先级队列并根据数据集中键的值设置优先级吗

c# - 使用 C#(或 Java)实现日志查看器 GUI

java - 用 JTS 画圆点

CSS 变量赋值

javascript - 变量运算符可能吗?