当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Displaying double array values in a GUI JTextArea

问题描述:

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.text.DecimalFormat;

public class DollarStoreBennettJordan extends JApplet implements ActionListener

{

JFrame frame = new JFrame();

JLabel n1 = new JLabel("Enter sales person number(1-4): ");

JTextField tf1 = new JTextField(5);

JLabel n2 = new JLabel("Enter product number(1-5): ");

JTextField tf2 = new JTextField(5);

JLabel n3 = new JLabel("Enter sales amount(0.01-0.99): ");

JTextField tf3 = new JTextField(5);

JTextArea ta = new JTextArea(8,58);

JButton b1 = new JButton("Submit");

JButton b2 = new JButton("Clear");

double[][] sales = {

{ 0.00, 0.00, 0.00, 0.00, 0.00 },

{ 0.00, 0.00, 0.00, 0.00, 0.00 },

{ 0.00, 0.00, 0.00, 0.00, 0.00 },

{ 0.00, 0.00, 0.00, 0.00, 0.00 }

};

double[] product_totals = new double [sales.length];

int total;

public void init()

{

setLayout(new FlowLayout(FlowLayout.CENTER));

add(n1);

add(tf1);

add(n2);

add(tf2);

add(n3);

add(tf3);

add(ta);

add(b1);

add(b2);

ta.setEditable(false);

ta.setFont(new Font("Courier", Font.BOLD, 10));

b1.addActionListener(this);

b2.addActionListener(this);

}

private boolean emptyFields()

{

boolean result;

if(tf1.getText().equals("")) //check tf1

{

result = true;

tf1.requestFocus();

showStatus("Please enter a value for salesperson number");

}

else

{

result = false;

}

if(tf2.getText().equals("")) //check tf2

{

result = true;

tf2.requestFocus();

showStatus("Please enter an item number");

}

else

{

result = false;

}

if(tf3.getText().equals("")) //check tf3

{

result = true;

tf3.requestFocus();

showStatus("Please enter a valid price");

}

else

{

result = false;

}

return result;

}

public void actionPerformed(ActionEvent e)

{

showStatus("");

if (e.getSource() == b2)

{

tf1.setText("");

tf2.setText("");

tf3.setText("");

tf1.requestFocus();

}

else if (e.getSource() == b1)

{

emptyFields();

drawSummary();

}

}

private void drawSummary()

{

ta.setText("");

DecimalFormat money = new DecimalFormat("0.00");

for (int r = 0; r < sales.length; r++)

{

total = 0;

for (int c = 0; c < sales[0].length; c++)

{

total += sales[r][c];

}

product_totals[r] = total;

}

for (int i = 0; i < sales.length; i++)

{

}

}

private void drawSeparators(char separator)

{

}

}

Hey there, I was hoping someone could help me with this. I'm having problems getting the array sales to display in the JTextArea ta. When complete, the applet is supposed to take input for sales person, item, and price and use the input to update the array. I'm still a beginner so Im just not sure how I could display the array in the text area and then update it with input and where to go from there. Thanks, any help is appreciated.

分享给朋友:
您可能感兴趣的文章:
随机阅读: