Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 同时使用两个actionCommand?_Java_User Interface_Interface_Action - Fatal编程技术网

Java 同时使用两个actionCommand?

Java 同时使用两个actionCommand?,java,user-interface,interface,action,Java,User Interface,Interface,Action,我有三个jradiobutton和一个JButton。我的操作命令有问题 当用户选择一个JRadioButton(每月、每两周、每周一次),并单击JButton(计算),然后启动actionCommandif循环,但我不确定如何执行 另外,textArea没有正确显示 package Lab4; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.Scanner; public c

我有三个
jradiobutton
和一个
JButton
。我的
操作命令有问题

当用户选择一个
JRadioButton
(每月、每两周、每周一次),并单击
JButton
(计算),然后启动
actionCommand
if循环,但我不确定如何执行

另外,
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.");
            }
        }
    }
}

我更喜欢使用内部类来逐个向特定按钮添加函数。我修改了密码。希望你会觉得这很有帮助……虽然我没有更多的声誉,但我不能在评论中提出任何问题。很抱歉

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

        }
    }
    }

assumingCorrectNumberFormats()方法中的逻辑不正确,您正在使用while循环检查按钮单击,并且在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("");
        }
    }
您可以为新单选按钮添加单独的条件。 还要减少JtextArea中的行数,否则相应地更改LayoutManager

output = new JTextArea(200, 60);

我添加了更多的JRadioButton,这行吗?
code
calculateButton.addActionListener(新建ActionListener(){public void actionPerformed(ActionEvent e){if(monthlyButton.isSelected()){if(compoundDailyButton.isSelected()){output.setText(“hi”)}else if(compoundWeeklyButton.isSelected()){output.setText(“hi2”);}else if(compoundMonthlyButton.isSelected()){output.setText(“hi3”);}else if(compoundSemiannuallyButton.isSelected()){}else if(compoundAnnuallyButton.isSelected()){}它应该是有效的。将相同的编码放在方法执行的所有单选按钮操作上。