我这里有一个程序,用于计算二手车的成本,用户输入的价格和维修价格。每次我添加更多文本字段进行输入时,用户可以同时比较3辆汽车,程序在运行时看起来完全混乱。任何帮助将不胜感激。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
public class Chapter4Summ extends JPanel implements ActionListener
{
static String name, price, repair;
static double tax1, price1, repair1, total1;
static double totalcost;
DecimalFormat y = new DecimalFormat ("$#,###,###,###.00");
public int count;
public double sum;
public double max;
public JTextField input, input2, input3, input4, input5, input6, input7, input8, input9;
public JLabel countDisplay;
public JLabel sumDisplay;
public JLabel maxDisplay;
public JLabel averageDisplay;
public void updateStats ()
{
name = (input.getText ().trim ());
repair1 = Double.parseDouble (input3.getText ().trim ());
price1 = Double.parseDouble (input2.getText ().trim ());
tax1 = price1 * 0.13;
averageDisplay.setText ("" + y.format (tax1));
total1 = tax1 + repair1 + price1;
maxDisplay.setText ("" + y.format (total1));
}
public void reset ()
{
count = 0;
sum = 0;
input.setText ("");
input2.setText ("");
input3.setText ("");
countDisplay.setText ("0");
sumDisplay.setText ("0");
maxDisplay.setText ("");
averageDisplay.setText ("");
}
public JButton computeButton;
public JButton resetButton;
public Chapter4Summ ()
{
input = new JTextField (10);
input.addActionListener (this);
input2 = new JTextField (10);
input.addActionListener (this);
input3 = new JTextField (10);
input.addActionListener (this);
computeButton = new JButton ("Enter Information");
computeButton.addActionListener (this);
resetButton = new JButton ("Reset");
resetButton.addActionListener (this);
countDisplay = new JLabel ("0");
sumDisplay = new JLabel ("0");
maxDisplay = new JLabel ();
averageDisplay = new JLabel ();
countDisplay.setFont (new Font ("Serif", Font.BOLD, 16));
sumDisplay.setFont (new Font ("Serif", Font.BOLD, 16));
maxDisplay.setFont (new Font ("Serif", Font.BOLD, 16));
averageDisplay.setFont (new Font ("Serif", Font.BOLD, 16));
countDisplay.setForeground (Color.RED);
sumDisplay.setForeground (Color.RED);
maxDisplay.setForeground (Color.RED);
averageDisplay.setForeground (Color.RED);
setLayout (new GridLayout (6, 2, 3, 3));
add (new JLabel ("Car Name", JLabel.RIGHT));
add (input);
add (new JLabel ("Car Price ", JLabel.RIGHT));
add (input2);
add (new JLabel ("Repair Cost ", JLabel.RIGHT));
add (input3);
add (new JLabel ("Tax: ", JLabel.RIGHT));
add (averageDisplay);
add (new JLabel ("Total Cost: ", JLabel.RIGHT));
add (maxDisplay);
add (resetButton);
add (computeButton);
setBorder (BorderFactory.createCompoundBorder (
BorderFactory.createLineBorder (Color.BLACK, 2),
BorderFactory.createEmptyBorder (6, 6, 6, 6)
));
}
public void actionPerformed (ActionEvent evt)
{
if (evt.getSource () == resetButton)
reset ();
else
updateStats ();
input.selectAll ();
input.requestFocus ();
}
public static void main (String[] args)
{
JFrame window = new JFrame ("Mike's Car Comparision");
window.setContentPane (new Chapter4Summ ());
window.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
window.pack ();
window.setLocation (250, 100);
window.setVisible (true);
}
}
答案 0 :(得分:0)
使用JOptionPane的方法,例如showInputDialog。
String someData = JOptionPane.showInputDialog(null, "Input some data");
如果您将关闭打开的对话框,someData将 null 。
答案 1 :(得分:0)
使用不同的外行管理器,可以更好地控制组件的位置,可能类似于GridBagLayout
有关详细信息,请参阅Laying Out Components Within a Container
将“输入字段”放入自定义组件(基于JPanel
之类的内容)并将此组件添加到布局中,也可以使用选项#1 ...
使用JTable
有关详细信息,请参阅How to Use Tables