Jansen Jansen - 6 months ago 10
Java Question

Changing text size of the text someone using my program sees?

Glad to be on this very helpful website. I have a problem with my Java program that will probably either be an easy fix, or impossible to fix.

You know how when you run a program that's open in NetBeans, it shows the output within the NetBeans application? I am trying to create a program that allows anybody who puts it on their computer to execute it, even if they have not installed an IDE like NetBeans or Eclipse. And when somebody executes my program, I want it to show the same thing as when I run it in NetBeans, with the same output and everything. The program doesn't use a GUI or anything like that. I managed to create an executable .jar file with the "Clean and build project" option, and I made a .bat file that successfully executes the program. This should achieve my goal of allowing anyone to run it. When I start up the .bat file, it works, and shows a white-text-black-background screen that runs the program exactly as it ran while in NetBeans.

The problem is that when I run the program (with the .bat file), the text is too small... I've tried looking everywhere for a solution to this, but I could only find discussion about how to make things work with GUIs, or other more complicated things than what my program needs. I am willing to work with GUI stuff if it is necessary, but I don't think it will help, due to what a GUI is. From my understanding, a GUI is not one big thing, but is a user interface composed of smaller parts (such as pop-up input prompts and scroll bars) that are each made by the programmer. I don't need any fancy scroll bars etc., I just need my program to execute like it does when ran in NetBeans (pretty sure this is called the console), and I need to change the text size of the program text when it executes.

I greatly appreciate any help, even if you aren't sure if it will work or not. If the answer requires a lengthy explanation and you don't feel like explaining, that's okay; just tell me what I'd have to learn to figure this out and I can research it if necessary.

Answer

I just created one. Try using this one and tell us if it helped or not.

EDIT Added a JTextField to read data. It is more advanced code than the previous one, since it uses concurrency. I tried to make it simple, these are the functions you can use:

  • MyConsole (): Constructor. Create and show the console
  • print (String s): Print the s String
  • println (String s) Print the s String and add a new line
  • read (): Makes you wait untill the user types and presses Enter
  • closeConsole (): Closes the console

Here is the code:

public class MyConsole implements ActionListener {

    private JFrame frame;
    private JTextArea myText;
    private JTextField userText;
    private String readText;
    private Object sync;        

    /*
     * Main and only constructor
     */
    public MyConsole() {
        // Synchronization object
        sync = new Object();

        // Create a window to display the console
        frame = new JFrame("My Console");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 200);
        frame.setLocationRelativeTo(null);
        frame.setResizable(true);
        frame.setContentPane(createUI());
        frame.setVisible(true);
    }

    /*
     * Creates user interface
     */
    private Container createUI() {
        // Create a Panel to add all objects
        JPanel panel = new JPanel (new BorderLayout());

        // Create and set the console 
        myText = new JTextArea();
        myText.setEditable(false);
        myText.setAutoscrolls(true);
        myText.setBackground(Color.LIGHT_GRAY);

        // This will auto scroll the right bar when text is added
        DefaultCaret caret = (DefaultCaret) myText.getCaret();
        caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);

        // Create the input for the user
        userText = new JTextField();
        userText.addActionListener(this);

        panel.add(new JScrollPane(myText), BorderLayout.CENTER);
        panel.add(userText, BorderLayout.SOUTH);

        return panel;
    }

    /*
     * Waits until a value is typed in the console and returns it
     */
    public String read(){
        print("==>");

        synchronized (sync) {
            try {
                sync.wait();
            } catch (InterruptedException e) {
                return readText = "";
            }
        }   
        return readText;
    }

    /*
     * Prints s
     */
    public synchronized void print(String s){
        // Add the "s" string to the console and 
        myText.append(s);
    }

    /*
     * Prints s and a new line
     */
    public synchronized void println(String s){
        this.print(s + "\r\n");
    }

    /*
     * Close the console
     */
    public void closeConsole(){
        frame.dispose();
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        // Check if the input is empty
        if ( !userText.getText().equals("") ){
            readText = userText.getText();          
            println(" " + readText);
            userText.setText("");
            synchronized (sync) {
                sync.notify();
            }
        }           
    }
}

Here is how to use it (an example). It just asks your age and writes something depending on your input:

public static void main(String[] args) {
    MyConsole console = new MyConsole();

    console.println("Hello! (Type \"0\" to exit)");

    int age = 1;

    do{
        console.println("How old are you ?");
        String read = console.read();
        try {
            age = Integer.valueOf(read);

            if ( age >= 18){
                console.println("Wow! " + age + " ? You are an adult already!");
            }else if ( age > 0 ){
                console.println("Oh! " + age + " ? You are such a young boy!");
            }else if (age == 0){
                console.println("Bye bye!");
            }else{
                console.println("You can't be " + age + " years old!");
            }
        }catch (Exception e) {
            console.println("Did you write any number there ?");
        }
    } while ( age != 0 );

    console.closeConsole();
}

And here is a image:

Custom console

Comments