简体   繁体   中英

I am having trouble implementing ActionListener, into a swing based GUI

I want to be able to detect when my button, ever so conveniently named button is clicked, I am having trouble making it be that way. here is some code

import java.awt.GridLayout;
import java.awt.event;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;

public class Main implements ActionListener
//im having immense problems implementing the action listener. 
{
  public static void main(String[] args) 
  {
    new GUI(); // calling in main.
    System.out.print("test for bad wifi because my wifi hates me"); // I'm using a cloud based ide
  }

  JFrame frame1 = new JFrame();
  JPanel panel1 = new JPanel();
  JFrame frame2 = new JFrame(); //not in use yet
  JPanel panel2 = new JPanel(); //""

  public void GUI()
  {
    JButton button = new JButton("moment"); 
    button.addActionListener(this);

    panel1.setBorder(BorderFactory.createEmptyBorder( 30, 30, 30, 30 )); 
    panel1.setLayout(new GridLayout(0, 1));
    panel1.add(button); 

    frame1.add(panel1, BorderLayout.CENTER); // frame is on the pannel or vice versa
    frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // funny close
    frame1.setTitle("Final.");
    frame1.pack();
    frame1.setVisible(true);
  }

  @Override
  public void actionPerformed(ActionEvent e)
  {
    
  }
}

Any help is appreciated

Download and use an IDE.

Oracle has a helpful tutorial, Creating a GUI With JFC/Swing . Skip the Netbeans section.

The JFrame methods must be called in a specific order. This is the order I use for all my Swing applications.

Here's the complete runnable code I wound up with.

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class SimpleJButtonExample implements ActionListener {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new SimpleJButtonExample().createAndShowGUI(); // calling in main.
            }
        });
        
        // I'm using a cloud based ide
        System.out.println("test for bad wifi because my wifi hates me"); 
    }

    JFrame frame1 = new JFrame();
    JPanel panel1 = new JPanel();
    JFrame frame2 = new JFrame(); // not in use yet
    JPanel panel2 = new JPanel(); // ""

    public void createAndShowGUI() {
        JButton button = new JButton("moment");
        button.addActionListener(this);

        panel1.setBorder(BorderFactory.createEmptyBorder(30, 30, 30, 30));
        panel1.setLayout(new GridLayout(0, 1));
        panel1.add(button);

        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // funny close
        frame1.setTitle("Final");
        frame1.add(panel1, BorderLayout.CENTER); // panel is within the frame
        frame1.pack();
        frame1.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("Button clicked");
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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