如何结合GUI类和扫描仪类? [英] How to combine the GUI class and the scanner class?

查看:35
本文介绍了如何结合GUI类和扫描仪类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java的新手,我正在尝试创建一个程序,该程序首先要求用户输入一些数字,然后输出音量,面积等.我也想显示一个矩形,我不知道该怎么做,因为我的程序运行良好,它不会显示矩形.我能做些什么?

I'm new in java and well I'm trying to create a program that will first ask the user to for some number and will output a volume, area and so on. Also I want to display a rectangle I don't know how that can be done because my program runs fine it just won't display the rectangle. What can I do?

package testchap3;

import java.util.*;
import javax.swing.JApplet;
import java.awt.*;

public class Chapter_3 extends JApplet
{
    public void paint(Graphics page)
    {
        page.drawRect(50,50,60,60);
    }

    public static void main(String[] args)
    {
        int lenght,width,height,volume,Area,Perimeter;

        Scanner scan = new Scanner(System.in);

        System.out.println("What is the lenght:");
        lenght = scan.nextInt();

        System.out.println("What is the height:");
        height = scan.nextInt();

        System.out.println("What is the width:");
        width= scan.nextInt();

        volume = (lenght*height*width);
        Area= volume/height;
        Perimeter= lenght+width+lenght+width;   

        System.out.println("Your volume is:"+volume);
        System.out.println("Your Area is:"+ Area);
        System.out.println("Your perimeter is:"+Perimeter);

     }
}

推荐答案

您的问题:

如何组合GUI类和扫描仪类?

How to combine the GUI class and the scanner class?

简短的回答:不要.

更长的答案:至少不要尝试将GUI与绑定到System.in/console的Scanner结合使用,因为这导致尝试以两种截然相反的方式来获取用户输入:线性控制台输入与事件驱动GUI交互.相反,为什么不让用户以事件驱动的方式通过GUI输入信息呢?否则,您将失去使用GUI的所有优势,并冒用GUI的事件线程进行控制台输入的风险.

Longer answer: At least don't try to combine a GUI with a Scanner tied to System.in/console as that leads to the attempt to mesh two diametrically opposed ways of getting user input: linear console input vs event-driven GUI interaction. Instead, why not let users enter information via the GUI in an event-driven manner? Else you lose all the advantages of using a GUI in the first place and risk tying up your GUI's event thread for console input.

在您的情况下,我有三个JTextFields或JFormattedTextFields或JSpinners,用户可以在其中输入数据,然后在按下JButton并启动其操作后,将结果显示在另一个文本组件或JLabel中

In your case, I'd have three JTextFields or JFormattedTextFields or JSpinners that the user can enter in data, and then I'd display the results in another text component or JLabel, after a JButton has been pressed and its Action initiated.

其他问题:您的GUI是JApplet,但是您已经给了它一个main方法并运行它的main方法,该方法永远不会起作用并且不会显示GUI.请阅读GUI教程,您可以在此处找到.而是显示一个JFrame而不是一个applet,重写一个JPanel并绘制其paintComponent方法(按照教程),然后再次删除所有的Scanner代码.

Other problems: your GUI is a JApplet, and yet you've given it a main method and run its main method which is never going to work and won't display the GUI. Please read the GUI tutorials which you can find here. Instead display a JFrame, not an applet, override a JPanel and draw in its paintComponent method (as per the tutorials), and again get rid of all the Scanner code.

这篇关于如何结合GUI类和扫描仪类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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