更改使用我的程序的人看到的文本的文本大小?

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

很高兴来到这个非常有用的网站。我的 Java 程序有问题,可能很容易修复,也可能无法修复。

您知道当您 运行 在 NetBeans 中打开一个程序时,它如何显示 NetBeans 应用程序中的输出吗?我正在尝试创建一个程序,允许任何将它放在计算机上的人执行它,即使他们没有安装像 NetBeans 或 Eclipse 这样的 IDE。当有人执行我的程序时,我希望它显示与我在 NetBeans 中 运行 时相同的内容,具有相同的输出和所有内容。该程序不使用 GUI 或类似的东西。我设法使用 "Clean and build project" 选项创建了一个可执行的 .jar 文件,并创建了一个成功执行该程序的 .bat 文件。这应该可以实现我允许任何人 运行 它的目标。当我启动 .bat 文件时,它可以工作,并显示一个白色文本黑色背景屏幕,运行程序与在 NetBeans 中 运行 完全一样。

问题是当我运行程序(带有.bat文件)时,文本太小...我试过到处找对于这个问题的解决方案,但我只能找到关于如何使事情与 GUI 或其他比我的程序需要的更复杂的事情一起工作的讨论。如果有必要,我愿意使用 GUI 的东西,但我认为这不会有帮助,因为 GUI 是什么。以我的理解,GUI不是一件大事,而是由程序员各自制作的更小的部分(例如弹出输入提示和滚动条)组成的用户界面。我不需要任何花哨的滚动条等,我只需要我的程序像在 NetBeans 中 运行 时那样执行(很确定这称为控制台),我需要更改文本大小执行时的程序文本。

非常感谢任何帮助,即使您不确定它是否有效。如果答案需要冗长的解释而你不想解释,那没关系;告诉我我需要学习什么才能解决这个问题,如果有必要我可以研究它。

我刚刚创建了一个。尝试使用这个并告诉我们它是否有帮助。

EDIT 添加了一个 JTextField 来读取数据。它比前一个代码更高级,因为它使用并发。我试图让它变得简单,这些是你可以使用的功能:

  • MyConsole ():构造函数。创建并显示控制台
  • print (String s):打印 s 字符串
  • println (String s) 打印 s 字符串并添加新行
  • read ():让你等到用户输入并按下 Enter
  • closeConsole (): 关闭控制台

代码如下:

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

这里是如何使用它(一个例子)。它只是询问你的年龄并根据你的输入写一些东西:

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

这是一张图片: