简体   繁体   English

同时执行两个actionCommand?

[英]two actionCommand at the same time?

I have three JRadioButtons and 1 JButton . 我有三个JRadioButtons和1个JButton I'm having problem for my actionCommand . 我的actionCommand遇到问题。

When the user chose one of the JRadioButton (monthly, bi-weekly, weekly), and also clicked the JButton (Calculate), then starts the actionCommand if loop but I'm not sure how to do that. 当用户选择一个JRadioButton (每月,每两周,每周)之一,并且还单击JButton (计算)时,然后启动actionCommand if循环,但是我不确定如何执行此操作。

Also the textArea didn't get displayed properly. 另外, 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.");
            }
        }
    }
}

I prefer always use the inner classes for adding function to the particular button one by one. 我更喜欢始终使用内部类将功能逐一添加到特定按钮。 I modified the code. 我修改了代码。 Hope you will find it helpfully.....Though i don't have more reputation i couldn't ask any question in the comments. 希望您会发现它对您有帮助。..虽然我没有更高的声誉,但我无法在评论中提出任何问题。 Sorry about that. 对于那个很抱歉。

    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"))
        {

        }
    }
    }

The logic in assumingCorrectNumberFormats() method is incorrect,you are uisng while loop to check for button click and inside the while loop you are checking for the radio buttons click events.You need to separate checks for radio button and button.Something like: 假设CorrectNumberFormats()方法中的逻辑不正确,您使用uising while循环检查按钮单击,并在while循环中检查单选按钮单击事件。您需要单独检查单选按钮和button。类似:

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("");
        }
    }

You can add separate conditions for new RadioButtons. 您可以为新的RadioButton添加单独的条件。 Also reduce the number of rows in JtextArea,else change the LayoutManager accordingly. 还减少JtextArea中的行数,否则相应地更改LayoutManager。

output = new JTextArea(200, 60);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM