我有三个JRadioButtons
和一个JButton
。我的actionCommand
有问题。
当用户选择其中一个JRadioButton
(每月,每两周,每周),然后点击JButton
(计算),然后启动actionCommand
if循环,但我和#39;我不知道该怎么做。
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.");
}
}
}
}
答案 0 :(得分:0)
我更喜欢一直使用内部类来逐个添加特定按钮的功能。我修改了代码。希望你会发现它有帮助.....虽然我没有更多的声誉,我不能在评论中提出任何问题。对不起。
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));
final JRadioButton monthlyButton = new JRadioButton ("Pay by monthly");
monthlyButton.addActionListener(this);
jRadiobuttonPanel.add(monthlyButton);
pane.add(jRadiobuttonPanel);
final JRadioButton biweeklyButton = new JRadioButton ("Pay by bi-weekly");
biweeklyButton.addActionListener(this);
jRadiobuttonPanel.add(biweeklyButton);
pane.add(jRadiobuttonPanel);
final 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(new ActionListener(){
public void actionPerformed(ActionEvent evt){
if (monthlyButton.isSelected())
{
output.setText("Monthly Button");
}
else if (weeklyButton.isSelected())
{
output.setText("Weekly Button");
}
else if (biweeklyButton.isSelected())
{
output.setText("Bi weeklybutton");
}
else if (monthlyButton.isSelected())
{
monthlyPayments.setText("");
principalValue.setText("");
annualInterestRate.setText("");
output.setText("");
}
else
{
//enterYourNumber.setText("Unexpected error.");
}
}
});
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(new ActionListener(){
public void actionPerformed(ActionEvent evt){
monthlyPayments.setText(null);
principalValue.setText(null);
annualInterestRate.setText(null);
}
});
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"))
{
}
}
}
答案 1 :(得分:0)
assumeCorrectNumberFormats()方法中的逻辑是不正确的,你在循环中检查按钮点击并在while循环内检查单选按钮单击事件。你需要单独检查单选按钮和按钮。像:
public void assumingCorrectNumberFormats(ActionEvent e) {
String actionCommand = e.getActionCommand();
if (actionCommand.equals("Calculate")) {
System.out.println("inside calculate");
String strPrincipal = principalValue.getText();
String strAnnualInterest = annualInterestRate.getText();
// do calculation
Integer result = Integer.parseInt(strPrincipal) * Integer.parseInt(strAnnualInterest);
System.out.println("result=" + result);
// set the result
output.setText(String.valueOf(result));
}
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("");
}
}
您可以为新的RadioButtons添加单独的条件。 还要减少JtextArea中的行数,否则相应地更改LayoutManager。
output = new JTextArea(200, 60);