我有三个 JRadioButtons
和1 JButton
. 我的孩子有问题 actionCommand
.
当用户选择 JRadioButton
(每月、每两周、每周一次),并单击 JButton
(计算),然后开始 actionCommand
但我不知道怎么做。
还有 textArea
没有正确显示。
package Lab4;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Scanner;
public class BalanceCalculator extends JFrame implements ActionListener
{
public static final int NUMBER_OF_DIGITS = 30;
private JTextField monthlyPayments;
private JTextField principalValue;
private JTextField annualInterestRate;
private JTextArea output;
public static void main (String[] args)
{
BalanceCalculator aCalculator = new BalanceCalculator();
aCalculator.setVisible(true);
}
public BalanceCalculator()
{
super("Mortgage Calculator");
JFrame frame = new JFrame();
Container pane = frame.getContentPane();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(700, 500);
pane.setLayout (new GridLayout (13, 2, 2, 2));
JPanel textPanel1 = new JPanel();
textPanel1.setLayout(new FlowLayout(FlowLayout.LEFT));
JLabel label1 = new JLabel("Enter the number of payments: ");
textPanel1.add(label1);
pane.add(textPanel1);
monthlyPayments = new JTextField(NUMBER_OF_DIGITS);
textPanel1.add(monthlyPayments);
pane.add(textPanel1);
JPanel jRadiobuttonPanel = new JPanel();
jRadiobuttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
JRadioButton monthlyButton = new JRadioButton ("Pay by monthly");
monthlyButton.addActionListener(this);
jRadiobuttonPanel.add(monthlyButton);
pane.add(jRadiobuttonPanel);
JRadioButton biweeklyButton = new JRadioButton ("Pay by bi-weekly");
biweeklyButton.addActionListener(this);
jRadiobuttonPanel.add(biweeklyButton);
pane.add(jRadiobuttonPanel);
JRadioButton weeklyButton = new JRadioButton ("Pay by weekly");
weeklyButton.addActionListener(this);
jRadiobuttonPanel.add(weeklyButton);
pane.add(jRadiobuttonPanel);
ButtonGroup group = new ButtonGroup();
group.add(monthlyButton);
group.add(biweeklyButton);
group.add(weeklyButton);
monthlyButton.setSelected (true);
JPanel textPanel2 = new JPanel();
textPanel2.setLayout(new FlowLayout(FlowLayout.LEFT));
JLabel label2 = new JLabel("Enter the principal: ");
textPanel2.add(label2);
pane.add(textPanel2);
principalValue = new JTextField(NUMBER_OF_DIGITS);
textPanel2.add(principalValue);
pane.add(textPanel2);
JPanel textPanel3 = new JPanel();
textPanel3.setLayout(new FlowLayout(FlowLayout.LEFT));
JLabel label3 = new JLabel("Enter the annual interest rate: ");
textPanel3.add(label3);
pane.add(textPanel3);
annualInterestRate = new JTextField(NUMBER_OF_DIGITS);
textPanel3.add(annualInterestRate);
pane.add(textPanel3);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
JButton calculateButton = new JButton ("Calculate");
calculateButton.addActionListener(this);
buttonPanel.add(calculateButton);
pane.add(buttonPanel);
JLabel outputs = new JLabel("Data for your mortgage: ");
pane.add(outputs);
JPanel results = new JPanel();
output = new JTextArea(200, 300);
output.setLineWrap(true);
output.setEditable(false);
output.setVisible(true);
results.add(output);
pane.add(results);
JButton resetButton = new JButton ("Reset");
resetButton.addActionListener(this);
buttonPanel.add(resetButton);
pane.add(buttonPanel);
add(pane, BorderLayout.CENTER);
}
public void actionPerformed (ActionEvent e)
{
try
{
assumingCorrectNumberFormats(e);
}
catch (NumberFormatException e2)
{
monthlyPayments.setText("Error: Re-enter number please.");
}
}
public void assumingCorrectNumberFormats (ActionEvent e)
{
monthlyPayments.addActionListener (new ActionListener ()
{
public void actionPerformed (ActionEvent e) {
System.out.println("You entered: " + e.getActionCommand ());
}
});
String actionCommand = e.getActionCommand();
while (actionCommand.equals("Calculate"))
{
if (actionCommand.equals("Pay by monthly"))
{
output.setText(toString());
}
else if (actionCommand.equals("Pay by bi-weekly"))
{
output.setText(toString());
}
else if (actionCommand.equals("Pay by weekly"))
{
output.setText(toString());
}
else if (actionCommand.equals("Reset"))
{
monthlyPayments.setText("");
principalValue.setText("");
annualInterestRate.setText("");
output.setText("");
}
else
{
//enterYourNumber.setText("Unexpected error.");
}
}
}
}
2条答案
按热度按时间bybem2ql1#
我更喜欢总是使用内部类为特定按钮逐个添加函数。我修改了密码。希望你会发现它有帮助…..虽然我没有更多的声誉,但我不能在评论中提出任何问题。很抱歉。
kkih6yb82#
assumingcorrectnumberformats()方法中的逻辑不正确,您使用while循环检查按钮单击,在while循环中检查单选按钮单击事件。您需要分别检查单选按钮和按钮。例如:
您可以为新单选按钮添加单独的条件。同时减少jtextarea中的行数,否则相应地更改layoutmanager。