0

I have no idea how to do this and have spent 2 days researching the Java APIs and on these forums and I have found nothing, if someone could please tell me how to use action listener to do so that would be great, most everything I have found has been for JUST a button and not with a bunch of other stuff. Here is my code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JFrame;

public class Skeleton extends JFrame implements ActionListener{

    public static void addComponentsToPane(Container pane) {
    pane.setLayout(null);

    JButton b1 = new JButton("Login");
    JTextField field2 = new JTextField(2);
    JTextField field = new JTextField(1);

    pane.add(field);
    pane.add(field2);
    pane.add(b1);

    Insets insets = pane.getInsets();
    Dimension size = field.getMaximumSize();
    field.setBounds(25 + insets.left, 5 + insets.top,
                 200, 20);
    size = field2.getPreferredSize();
    field2.setBounds(25 + insets.left, 40 + insets.top,
                 200, 20);
    size = b1.getPreferredSize();
    b1.setBounds(75 + insets.left, 75 + insets.top, 100, 40);


}
    private static void createAndShowGUI() {


        JFrame frame = new JFrame("User Login"); // GUI gui = new GUI() as well
        // default value JFrame.HIDE_ON_CLOSE
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        addComponentsToPane(frame.getContentPane());

        //Create the menu bar.  Make it have a Blue background.
        JMenuBar blueMenuBar = new JMenuBar();
        blueMenuBar.setOpaque(true);
        blueMenuBar.setBackground(new Color(211, 221, 222));
        blueMenuBar.setPreferredSize(new Dimension(300, 20));        

        //Create a grey label to put in the content pane.
        JLabel greyLabel = new JLabel();
        greyLabel.setOpaque(true);
        greyLabel.setBackground(new Color(205, 209, 209));
        greyLabel.setPreferredSize(new Dimension(300, 400));

        //Adding a custom BorderLayout
        JPanel panel = new JPanel(new BorderLayout());
        Container contentPane = frame.getContentPane();
        //Set the menu bar and add the label to the content pane.
        frame.setJMenuBar(blueMenuBar);
        frame.getContentPane().add(greyLabel, BorderLayout.CENTER);    

        //Display the window.

        frame.setSize(300, 200);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub

    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Jackson Haile
  • 77
  • 1
  • 1
  • 7
  • 1
    So, what exactly do you want to do ? – mlwn Sep 17 '14 at 01:17
  • Java GUIs have to work on different OS', screen size, screen resolution etc. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Sep 17 '14 at 01:36
  • *"most everything I have found has been for JUST a button and not with a bunch of other stuff"* Get it to work that way first. Then look to incorporate the button into the complex GUI. If you're always searching for how to do simple things in a complicated example, you won't find much.. – Andrew Thompson Sep 17 '14 at 01:38
  • Further comments: 1) This code both extends frame and uses an instance of a frame. It is better to do (just) the latter. 2) Given it seems to be a log-in GUI, it would be better suited to being displayed in a modal `JDialog` or a `JOptionPane`.. – Andrew Thompson Sep 17 '14 at 01:41
  • BTW - *"`JButton` + `boolean`"* suggests to me `JCheckBox` or `JToggleButton` instead.. – Andrew Thompson Sep 17 '14 at 01:58
  • Are you just having a scoping isssue? Like you can reach any components from inside the `actionPerformed`? If you so declare your components on the class level instead of in a block level. – Paul Samsotha Sep 17 '14 at 02:52

1 Answers1

0

You simply add an ActionListener to the button:

boolean state = false;

JButton b1 = new JButton("Login");
b1.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
        // some action to perform
        state = true;
    }
});
David Yee
  • 3,515
  • 25
  • 45