我正在学校学习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();
}
}
答案 0 :(得分:2)
编写自己的OutputStream
实现,添加写入文本区域的任何字节。然后将其换成PrintStream
并将其传递给System.setOut()
:
System.setOut(new PrintStream(myTextAreaOutputStream));
答案 1 :(得分:0)
您可以使用Java Swing轻松创建表单并实现其功能。
看一下本教程:http://docs.oracle.com/javase/tutorial/uiswing/start/
使用Netbeans http://docs.oracle.com/javase/tutorial/uiswing/learn/settingup.html
从基础项目开始这很直截了当。布置控件,然后实现该功能。我认为他们的Celsius转换器示例将通过一些修改让您非常接近完成项目。