Pregunta

I am trying to have the number the user inputs into the frame either multiply by 2 or divide by 3 depending on which button they decide to click. I am having an hard time with working out the logic to do this. I know this needs to take place in the actionperformed method.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class Quiz4 extends JFrame ActionListener
{

 // Global Variable Declarations
 // Our list input fields
 private JLabel valueLabel = new JLabel("Enter a value between 1 and 20: ");
 private JTextField valueField = new JTextField(25);

 // create action buttons
 private JButton multiButton = new JButton("x2");
 private JButton divideButton = new JButton("/3");

 private JScrollPane displayScrollPane;
 private JTextArea display = new JTextArea(10,5);

// input number
private BufferedReader infirst;
// output number
private NumberWriter outNum;

 public Quiz4()
{
     //super("List Difference Tool");
     getContentPane().setLayout( new BorderLayout() );

     // create our input panel
     JPanel inputPanel = new JPanel(new GridLayout(1,1));
     inputPanel.add(valueLabel);
     inputPanel.add(valueField);

     getContentPane().add(inputPanel,"Center");

     // create and populate our diffPanel
     JPanel diffPanel = new JPanel(new GridLayout(1,2,1,1));
     diffPanel.add(multiButton);
     diffPanel.add(divideButton);
     getContentPane().add(diffPanel, "South");

    //diffButton.addActionListener(this);

} // Quiz4()

public void actionPerformed(ActionEvent ae)
{

} // actionPerformed()

public static void main(String args[])
{
     Quiz4 f = new Quiz4();
     f.setSize(1200, 200);
     f.setVisible(true);
     f.addWindowListener(new WindowAdapter()
 { // Quit the application
     public void windowClosing(WindowEvent e)
     {
     System.exit(0);
 }
 });
 } // main()
} // end of class
¿Fue útil?

Solución

Here's something simpler, but it essentially does what you want out of your program. I added an ActionListener to each of the buttons to handle what I want, which was to respond to what was typed into the textbox. I just attach the ActionListener to the button, and then in the actionPerformed method, I define what I want to happen.

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class Quizx extends JFrame {
private JPanel panel;
private JTextField textfield;
private JLabel ansLabel;

public Quizx() {
    panel = new JPanel(new FlowLayout());
    this.getContentPane().add(panel);
    addLabel();
    addTextField();
    addButtons();
    addAnswerLabel();
    this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    this.setTitle("Quiz 4");
    this.setSize(220, 150);
    this.setLocationRelativeTo(null);
    this.setResizable(false);
    this.setVisible(true);
}

private void addTextField() {
    textfield = new JTextField();
    textfield.setColumns(9);
    panel.add(textfield);
}

private void addButtons() {
    JButton multButton = new JButton("x2");
    JButton divButton = new JButton("/3");
    panel.add(multButton);
    panel.add(divButton);
    addMultListener(multButton);
    addDivListener(divButton);
}

private void addLabel() {
    JLabel valueLabel = new JLabel("Enter a value between 1 and 20: ");
    panel.add(valueLabel);
}

private void addAnswerLabel() {
    ansLabel = new JLabel();
    panel.add(ansLabel);
}

private void addMultListener(JButton button) {
    button.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            ansLabel.setText(String.valueOf(Integer.parseInt(textfield.getText().trim()) * 2));

        }
    });
}

private void addDivListener(JButton button) {
    button.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            ansLabel.setText(String.valueOf(Double.parseDouble(textfield.getText().trim()) /3));

        }
    });
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            new Quizx();
        }
    });
}
}

Hope that helps.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top