最简单的对话/消息框适合初学者?

时间:2015-12-04 10:48:43

标签: java dialog

我不确定如何做到这一点。我在java类的介绍中它要求我们使用一个消息框(而不仅仅是system.out.println)我记得我们导入了一些东西,这是一个很容易的改变,但我无法找到它的任何注释。

此外,我在网站和本网站上发现的所有示例都超出了本课程的范围。

如果格式不正确,我提前道歉,这是我第一次在这里发帖。

TLDR:试图改变

    System.out.print("Enter renter name: ");
    renterName = input.next();

显示在消息框中而不是在Eclipse控制台中

我知道我们导入了一些东西(导入Scanner的方式相同),但是我找到的每个例子都是创建自己的对话框方法,这些方法超出了我的知识范围,而且这个类。

完整的代码如下:

import java.util.Scanner;

public class RentYourVideo {

  public static void main(String[] args) {

    int numberOfRentals, finalBill;

    VideoRental rental = new VideoRental(); //runs constructor
    Scanner input = new Scanner(System.in);
    String renterName;

    System.out.print("Enter renter name: ");
    renterName = input.next();

    System.out.print("Enter number of videos to rent: ");
    numberOfRentals = input.nextInt();

    rental.setRentalFee();  //needs to set rental fee to $5 according to assignment
    rental.calculateBill(numberOfRentals);  //from prev input
    finalBill = rental.getFinalBill();

    System.out.println(renterName + " your total bill for " +numberOfRentals+ " videos is $" +finalBill);

    input.close();
  }
}

//更改所有优惠&输出到对话/消息框!!!!

public class VideoRental {

  private int rentalFee, finalBill, numberOfRentals;

  public VideoRental() {    //constructor method
    rentalFee = 0;
    finalBill = 0;
  }

  public void setRentalFee() {  //set method
    rentalFee = 5;
  }     //the assignment claims this must set rentalFee = 5

  public void calculateBill(int inRented) {
    numberOfRentals = inRented;
    finalBill = rentalFee * numberOfRentals;
  }

  public int getFinalBill() {
    return finalBill;
  }

}

1 个答案:

答案 0 :(得分:1)

检查出来:

String name = JOptionPane.showInputDialog(null, "Enter name here:");

http://docs.oracle.com/javase/7/docs/api/javax/swing/JOptionPane.html

import javax.swing.JOptionPane;

[...]

public static void main(String[] args) {

    int numberOfRentals, finalBill;

    VideoRental rental = new VideoRental(); //runs constructor

    String renterName;
    renterName = JOptionPane.showInputDialog(null, "Enter renter name: ");

    numberOfRentals = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter number of videos to rent: "));

    rental.setRentalFee();  //needs to set rental fee to $5 according to assignment
    rental.calculateBill(numberOfRentals);  //from prev input
    finalBill = rental.getFinalBill();

    JOptionPane.showMessageDialog(null, renterName + " your total bill for " +numberOfRentals+ " videos is $" +finalBill);
  }