gpt4 book ai didi

java - 如何将控制台输出重定向到 GUI 表单?

转载 作者:行者123 更新时间:2023-11-30 09:16:51 36 4
gpt4 key购买 nike

我在学校上 Java 编程课,我们一直在类里面做一个项目 - 处理控制台应用程序。在接下来的一周,我们将继续致力于使用 JAVA 创建 GUI 应用程序,我对如何处理我的一个项目有了一个想法。

我想将控制台输出重定向到 GUI 内的文本区域。但我不知道这是否可能,或者如何去做。是否可能,如果可以,有人可以帮助我。我正在尝试设计我的表格,使其看起来像收银机(收据在收银机的一侧)。在这种情况下,收据将是重定向的控制台输出。

如有任何帮助,我们将不胜感激。

这是我的源代码:

package autoshop_invoice;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.NumberFormat;
import java.util.logging.Level;
import java.util.logging.Logger;

public class AutoShop_Invoice
{

public static void total_sales() throws IOException
{
String text = "";
String part_number = "";
int num_items = 0;
double price = 0.0;
double tax = 0.0;
double total_sale = 0.0;

//Prompt user for part number and store value in variable part_number.
System.out.println("Enter Part Number then hit enter.");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
try
{
part_number = in.readLine();
} catch (IOException ex)
{
Logger.getLogger(AutoShop_Invoice.class.getName()).log(Level.SEVERE, null, ex);
}

//Prompt user to enter number of items sold and store value in variable num_items.
System.out.println("Enter Number of Items sold then hit enter.");
in = new BufferedReader(new InputStreamReader(System.in));
text = in.readLine();
num_items = Integer.parseInt(text);

//Prompt user to enter Price per Item and store value in variable num_items.
System.out.println("Enter Price per Item then hit enter.");
in = new BufferedReader(new InputStreamReader(System.in));
text = in.readLine();
price = Double.parseDouble(text);

//Display the total sale WITH tax calculated.
total_sale = num_items * price;
tax = total_sale * .06;
total_sale = total_sale + tax;
//DecimalFormat df = new DecimalFormat("#.##");
NumberFormat nf = NumberFormat.getCurrencyInstance(); //Get the current system locale currency
System.out.println();
System.out.println("***********************************");
System.out.print("Part Number: " + part_number + " "); //Display the Part Number being sold
System.out.println("QTY: " + num_items); //Display the quantity of items sold
System.out.println("Sub-Total is: " + nf.format(total_sale)); //Display sub-total rounded to 2 decimal points
System.out.println("MD Sales Tax due (6%): " + nf.format(tax)); //Display the calculated sales tax
//Display grand-total rounded to 2 decimal points and formatted with the locale currency
//System.out.println("Grand-Total sales is: " + df.format(nf.format(total_sale + tax)));
System.out.println("Grand-Total sales is: " + nf.format(total_sale + tax));
System.out.println("***********************************");
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException
{
// TODO code application logic here
total_sales();
}
}

最佳答案

自己写OutputStream添加任何写入文本区域的字节的实现。然后将其包装在 PrintStream 中并将其传递给 System.setOut() :

System.setOut(new PrintStream(myTextAreaOutputStream));

关于java - 如何将控制台输出重定向到 GUI 表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19255211/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com