Search code examples
javaswingjframeawtjbutton

JAVA JButton in a different class refuses to activate when pressed


I'm failing to understand why my yankee and whiskey JButtons aren't working. Right now I only want them to close the program when romeo is greater than 1 and sierra is greater than 1.

import java.awt.*;
import java.lang.*;
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.*;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import java.util.Scanner;

public class AlphaMenu extends JFrame /*implements actionPerformed*/
{

    private GraphicsDevice gamma;
    public JButton charlie, zulu, yankee, xray;
    public JFrame beta;
    public JPanel delta, echo, foxtrot, golf, hotel;
    public JTextArea whiskey, victor;
    public BorderLayout uniform;
    public ImageIcon bg;
    public JLabel tango;
    public int sierra, romeo;
    public Integer quebec, papa;
    public ActionEvent oscar;
    public ActionEvent november;

    public AlphaMenu()
    {
        //Initialization of Objects
        charlie = new JButton("EXIT");
        zulu = new JButton("Enter Time");
        yankee = new JButton("Enter Amount of Money");
        xray = new JButton("Calculate");
        sierra = 0;
        romeo = 0;
        quebec = new Integer(0);
        papa = new Integer(0);
        whiskey = new JTextArea(2, 15);
        victor = new JTextArea(2, 15);
        bg = new ImageIcon("background.gif");
        beta = new JFrame();
        delta = new JPanel();
        echo = new JPanel();
        foxtrot = new JPanel();
        golf = new JPanel();
        hotel = new JPanel();
        uniform = new BorderLayout();
        ImageIcon bg = new ImageIcon("background.gif");
        tango = new JLabel("");

        tango.setIcon(bg);

        //Modification of panels
        beta.add(delta, uniform.PAGE_END);
        beta.add(golf, uniform.PAGE_START);
        beta.add(echo, uniform.LINE_START);
        beta.add(foxtrot, uniform.LINE_END);
        beta.add(hotel, uniform.CENTER);

        golf.add(tango);

        //Modification of JButton charlie & adding of JButtons
        charlie.setPreferredSize(new Dimension(100, 50));
        delta.add(charlie);
        charlie.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                System.exit(0);
            }
        });
        echo.add(whiskey);
        echo.add(yankee);
        foxtrot.add(victor);
        foxtrot.add(zulu);


        //Modification of JFrame beta
        beta.setUndecorated(true);
        beta.setExtendedState(JFrame.MAXIMIZED_BOTH);
        beta.setResizable(false);
        beta.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        beta.setVisible(true);
    }

    public void buttonSetup() throws NumberFormatException
    {
        //Modification of JButton yankee & JTextArea whiskey & int sierra
        romeo = quebec.parseInt(whiskey.getText());
        yankee.setPreferredSize(new Dimension(300, 50));
        yankee.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent oscar)
            {
                System.exit(0);
            }
        });

        //Modification of JButton zulu & JTextArea victor & int romeo
        sierra = papa.parseInt(victor.getText());
        zulu.setPreferredSize(new Dimension(300, 50));
        zulu.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent november)
            {
                System.exit(0);
            }
        });

    }

    public void actionPerformed(ActionEvent e)
    {
    }

    public static void main(String[] args)
    {
        new AlphaMenu();
    }
}

Solution

  • So, you have two JTextArea (JTextField would probably be better) and a button. you want some buttons to execute exit when the text of both textareas is an integer greater than 1.

    seems that your buttonSetup() function isn't called anywhere.

    Anyway, I'd create an ActionListener that reads the texts, converts to integer, tests your condition and executes exit(). This ActionListener should be added to all the buttons you want to perform the action

    final ActionListener al = new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            try {
                final int intRomeo = Integer.parseInt(romeo.getText());
                final int intSierra = Integer.parseInt(sierra .getText());
    
                if (intRomeo > 1 && intSierra > 1) {
                    // whatever you want to do
                    System.exit(0); 
                }
            } catch (/*NumberFormat*/ Exception e) {
                // ...not integers
            }
        };
    }
    whiskey.addActionListener(al);
    yankee.addActionListener(al);
    

    I have to add: the variable names you are using are really bad. Consider choosing something more significative.