R
R
roman38472014-03-12 20:10:52
Java
roman3847, 2014-03-12 20:10:52

Action on button click, what's wrong?

Created Frame. Placed a Button on it. Now I want to output the text to the console by clicking on the button, but there is some kind of error. What is wrong? Eclipse environment.

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class Main {

  public static void main(String[] args) {
    // TODO Auto-generated method stub
    
   JFrame frame = new JFrame("name of frame");
   frame.setSize(500, 300);
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.setLocationRelativeTo(null);
   frame.setLayout(new BorderLayout());
   
   JButton Button = new JButton("Button");
   frame.add(Button, BorderLayout.EAST);
   
   Button.addActionListener(new myButton());
   
   frame.setVisible(true);
  }
  
}

public class myButton implements ActionListener{
  
  @Override
  public void actionPerformed(ActionEvent event) {
    // TODO Auto-generated method stub
    System.out.println("Hello, World");
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Suleyman9999, 2019-01-03
@Suleyman9999

It doesn't work because you wrote the word public for each class and it should only be once in one class file, subclasses can't use it, remove public from class myButton

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question