我想在java中创建一个调查。 我想同时从多个TextFields和ComboBoxes更新TextArea,每个TextArea在新行上单独显示。我只知道如何为一个TextField做到这一点。 感谢您的支持。
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
public class LoanCalculator2 extends JFrame
{
private String [] array1={"1", "2", "3", "4", "5"};
private String [] array2={"Jan", "Feb", "Mar", "Apr", "May"};
private String [] array3={"2000", "2001", "2003", "2004", "2005"};
private String [] array4={"Employed", "Self-Employed", "Unemployed"};
private JLabel l1=new JLabel("Interest Rate %");//labels
private JLabel l2=new JLabel("Number Of Years");
private JLabel l3=new JLabel("Loan Amount");
private JLabel l4=new JLabel("Interest Accrural Period");
private JLabel l5=new JLabel("Total Payment");
private JLabel l6=new JLabel("Date");
private JLabel l7=new JLabel("Firstname");
private JLabel l8=new JLabel("Surname");
private JLabel l9=new JLabel("Age");
private JLabel l10=new JLabel("Address");
private JLabel l11=new JLabel("Job Status");
private JTextField t1=new JTextField();//TextFields
private JTextField t2=new JTextField();
private JTextField t3=new JTextField();
private JTextField t4=new JTextField();
private JTextField t5=new JTextField();
private JTextField t6=new JTextField();//firstname
private JTextField t7=new JTextField();//surname
private JTextField t8=new JTextField();//age
private JTextField t9=new JTextField();//address
JTextArea textArea = new JTextArea(5, 20);
private JComboBox cb1=new JComboBox(array1);
private JComboBox cb2=new JComboBox(array2);
private JComboBox cb3=new JComboBox(array3);
private JComboBox cb4=new JComboBox(array4);
private JPanel p_base=new JPanel();//Panels
private JPanel p_top=new JPanel();
private JPanel p_bottom=new JPanel();
private JPanel p_1=new JPanel();
private JPanel p_2=new JPanel();
private JPanel p_3=new JPanel();
private JPanel p_date=new JPanel();
private JPanel p_left=new JPanel();
private JPanel p_right=new JPanel();
private ButtonGroup group=new ButtonGroup();//Button Group
boolean state=false;
private JRadioButton b1=new JRadioButton("Monthly", state);//RadioButtons
private JRadioButton b2=new JRadioButton("Quarterly", state);
private JRadioButton b3=new JRadioButton("Half Yearly", state);
private JRadioButton b4=new JRadioButton("Annually", state);
private JButton bt1=new JButton("Compute Payment");//Compute Button
private JButton bt2=new JButton("Reset");//Reset button
private JButton bt3=new JButton("Verify");//Verify Button
private TitledBorder p2=new TitledBorder("Compute");//TitleBorder
private TitledBorder p1=new TitledBorder("Enter Loan amount, Interest rate and Years");
private TitledBorder p3=new TitledBorder("Interest Accrural Period");
private TitledBorder p4=new TitledBorder("Loan Data");
private TitledBorder p5=new TitledBorder("Customer Details");
GridLayout grid=new GridLayout(4, 1);//ButtonGroup GridLayout
private Dimension size1=new Dimension(160, 200);
private Dimension size2=new Dimension(300, 200);
public LoanCalculator2()//constructor
{
frameDimensions();
setTitle("Loan Application");
setLayout(new FlowLayout());
p_base.setLayout(new BorderLayout(5, 5));
p_top.setLayout(new BorderLayout(5, 5));
// p_bottom.setLayout();
p_1.setLayout(new GridLayout(5, 2, 5, 5));
p_1.setBorder(p1);
p_1.setPreferredSize(size2);
p_1.add(l1); p_1.add(t1);
p_1.add(l2); p_1.add(t2);
p_1.add(l3); p_1.add(t3);
p_1.add(l4); p_1.add(t4);
p_1.add(l5); p_1.add(t5);
t4.setEditable(false);
t5.setEditable(false);
p_2.setLayout(new GridLayout(1,2,5,5));
p_2.setBorder(p2);
p_2.add(bt2);
p_2.add(bt1);
p_3.setLayout(grid);
p_3.setBorder(p3);
p_3.setPreferredSize(size1);
p_3.add(b1);
p_3.add(b2);
p_3.add(b3);
p_3.add(b4);
group.add(b1);
group.add(b2);
group.add(b3);
group.add(b4);
p_top.add(p_1, BorderLayout.CENTER);
p_top.add(p_2, BorderLayout.SOUTH);
p_top.add(p_3, BorderLayout.WEST);
p_top.setBorder(p4);
p_bottom.setBorder(p5);
p_bottom.setLayout(new BorderLayout());
p_left.setLayout(new GridLayout(6,2,5,5));
p_date.setLayout(new FlowLayout());
p_date.add(cb1);
p_date.add(cb2);
p_date.add(cb3);
p_left.add(l6); p_left.add(p_date);
p_left.add(l7); p_left.add(t6);
p_left.add(l8); p_left.add(t7);
p_left.add(l9); p_left.add(t8);
p_left.add(l10); p_left.add(t9);
p_left.add(l11); p_left.add(cb4);
p_right.add(textArea, BorderLayout.CENTER);
p_right.add(bt3, BorderLayout.SOUTH);
p_bottom.add(p_left, BorderLayout.WEST);
p_bottom.add(p_right, BorderLayout.CENTER);
p_base.add(p_top, BorderLayout.NORTH);
p_base.add(p_bottom, BorderLayout.SOUTH);
add(p_base);//add base panel to frame
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setEditable(false);
TheListener listener=new TheListener();
bt1.addActionListener(listener);//compute
bt2.addActionListener(listener);//reset
bt3.addActionListener(listener);//verify
}//end constructor
private void Month()
{
l4.setText("1 Month Payment");
double i=Double.parseDouble(t1.getText());
double r=i/1200;
double A=Double.parseDouble(t3.getText());
double n=Double.parseDouble(t2.getText());
double N=n*12;
double v1=Math.pow((1+r),-N);
double v2=(1-v1);
double v3=(r*A)/v2;
DecimalFormat df=new DecimalFormat("###.##");
t4.setText(""+df.format(v3));
double v4=v3*N;
t5.setText(""+df.format(v4));
}
private void Quarter()
{
l4.setText("3 Month Payment");
double i=Double.parseDouble(t1.getText());
double r=i/400;
double A=Double.parseDouble(t3.getText());
double n=Double.parseDouble(t2.getText());
double N=n*4;
double v1=Math.pow((1+r),-N);
double v2=(1-v1);
double v3=(r*A)/v2;
DecimalFormat df=new DecimalFormat("###.##");
t4.setText(""+df.format(v3));
double v4=v3*N;
t5.setText(""+df.format(v4));
}
private void Half()
{
l4.setText("6 Month Payment");
double i=Double.parseDouble(t1.getText());
double r=i/200;
double A=Double.parseDouble(t3.getText());
double n=Double.parseDouble(t2.getText());
double N=n*2;
double v1=Math.pow((1+r),-N);
double v2=(1-v1);
double v3=(r*A)/v2;
DecimalFormat df=new DecimalFormat("###.##");
t4.setText(""+df.format(v3));
double v4=v3*N;
t5.setText(""+df.format(v4));
}
private void Year()
{
l4.setText("12 Month Payment");
double i=Double.parseDouble(t1.getText());
double r=i/100;
double A=Double.parseDouble(t3.getText());
double n=Double.parseDouble(t2.getText());
double N=n;
double v1=Math.pow((1+r),-N);
double v2=(1-v1);
double v3=(r*A)/v2;
DecimalFormat df=new DecimalFormat("###.##");
t4.setText(""+df.format(v3));
double v4=v3*N;
t5.setText(""+df.format(v4));
}
private void Reset()
{
t1.setText("");
t2.setText("");
t3.setText("");
t4.setText("");
t5.setText("");
l4.setText("Interest Accrural Period");
group.clearSelection();
frameDimensions();
}
private void frameDimensions()
{
setVisible(true);
setSize(480,300);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private class TheListener implements ActionListener //Listener Class
{
public void actionPerformed(ActionEvent event) //Event method
{
Object target=event.getSource();
if(target==bt1)
{
if(b1.isSelected())
{
Month();
}
else if(b2.isSelected())
{
Quarter();
}
else if(b3.isSelected())
{
Half();
}
else if(b4.isSelected())
{
Year();
}
else
{
t4.setText("Error");
t5.setText("Error");
}
if(t1.getText().equals(""))
{
t4.setText("No Data");
t5.setText("No Data");
}
if(t2.getText().equals(""))
{
t4.setText("No Data");
t5.setText("No Data");
}
if(t3.getText().equals(""))
{
t4.setText("No Data");
t5.setText("No Data");
}
}
if(target==bt2)
{
Reset();
}
if(target==bt3)
{
textArea.setText(t6.getText()+t7.getText());
}
}
}
}
答案 0 :(得分:0)
您必须制作某种缓冲区来存储其中的所有数据。以StringBuilder为例。之后,只需将整个缓冲区附加到文本区域
答案 1 :(得分:0)
使用JTextArea.append
而不是JTextArea.setText
。不要忘记在每行的末尾添加一个新行(\n
)
您可能还希望将JTextArea
放在JScrollPane
中,或者当您向文本区域添加更多文字时,它会增长,从而在您的用户界面中占用更多空间,例如......
p_right.add(new JScrollPane(textArea), BorderLayout.CENTER);