开发者

A good way to have all my messages in Java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
开发者_如何学C

We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.

Closed 4 years ago.

Improve this question

i'm looking for a small framework to have all my messages stored in a common way. I'll give an example for better understanding.

In a part of my code, in a particular JFrame i've an alert something like this:

JOptionPane.showMessageDialog(null, "Error, you must provide an integer value", "ERROR", JOptionPane.ERROR_MESSAGE);

So, this string: "Error, you must provide an integer value". I would like to have it in a particular "log", or something like that, so i can do something like this:

JOptionPane.showMessageDialog(null, Messages.getMessage(Messages.INTEGER_VALUE), "ERROR", JOptionPane.ERROR_MESSAGE);

Hard to explain, hope you can help me.

Thanks!


Sounds like you need a ResourceBundle. It allows you to maintain locale-sensitive user-displayable messages keyed against a code.

It's not an external framework, it's part of the JavaSE API.


I think something like cal10n might be the type of framework you're looking for.

http://cal10n.qos.ch/


Or... you can write your own mini messaging utility, like this:-

public class MessageUtil {
    enum Message {
        ERROR_INTEGER_REQUIRED("Error", "Error, you must provide an integer value"), 
        ERROR_STRING_REQUIRED("Error", "Error, you must provide a string value"), 
        ERROR_BLA_BLA("Error", "Error, you are doomed"),
        INFO_DATA_SAVED("Note", "Data is successfully saved");

        String  title;
        String  msg;

        private Message(String title, String msg) {
            this.title = title;
            this.msg = msg;
        }
    }

    public static void display(Message message) {
        JOptionPane.showMessageDialog(null, message.msg, message.title, JOptionPane.ERROR_MESSAGE);
    }
}

Then, you can do something like this:-

MessageUtil.display(ERROR_INTEGER_REQUIRED); 


Create a static class called "Messages"

Inside this, have a method called getMessage which accepts an integer, and return the correct error message corresponding to the code.


Sounds more like you want a lookup table for your error messages. You could get fancy and actually make a class to do this for you or do something easy:

String errors[] = {"Some error","Some other error"};
JOptionPane.showMessageDialog(null,errors[0],"ERROR", JOptionPane.ERROR_MESSAGE);

or

Map<String,String> errors = new HashMap<String,String>();
errors.put("PROVIDE_INT","Error, you must provide an integer value");
JOptionPane.showMessageDialog(null,errors.get("PROVIDE_INT"),"ERROR", JOptionPane.ERROR_MESSAGE);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜