让用户输入一串将用作gui标题的文本 [英] Have the user input a string of text that will be used as a gui title

查看:70
本文介绍了让用户输入一串将用作gui标题的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个需要它的问题,以便当用户将文本输入控制台时,该文本将用作应用程序的标题。是否需要创建一种新方法或者我现在应该创建一种新方法?



我尝试过的方法:



I'm having an issue where I need to have it so that when a user inputs text into the console, that text will be used as the title of the app. Would a new method need to be created or would I do it within what I have right now?

What I have tried:

package guiapp;

/**
 *
 * @author stephenwessels
 */
import java.util.Scanner;
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class GUIApp extends JFrame
{

    public GUIApp()
    {
        
        String str = new String();
        System.out.println("Enter the name of the GUI: ");
        
        
        this.setVisible(true);
        this.setSize(300,400);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle(str);
    }
    
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) 
    {
        GUIApp gui = new GUIApp();
    }
    
}

推荐答案

请先阅读我对该问题的评论。 />


从用户那里获取输入字符串的方法很少。检查一下:如何从控制台读取输入 - Java [< a href =https://www.mkyong.com/java/how-to-read-input-from-console-java/\"target =_ blanktitle =New Window> ^ ]



注意:如果使用Eclipse,则无法从系统获取 console 实例.console(),因为Eclipse在后台运行你的代码。
Please, read my comment to the question first.

There's few ways to get input string from user. Check this: How to read input from console – Java[^]

Note: If you use Eclipse, you can't get console instance from System.console(), because Eclipse runs your code in a background.


这篇关于让用户输入一串将用作gui标题的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆