63 Tutorial JButton Final Program I need some help

+2 Valdemar Voinič · May 19, 2015

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;


public class Gui extends JFrame {


private JButton reg;
private JButton custom;

public Gui (){
super("The title");
setLayout(new FlowLayout());

reg = new JButton("reg Button");
add(reg);

Icon b = new ImageIcon(getClass().getResource("b.png"));
Icon x = new ImageIcon(getClass().getResource("x.png"));

custom = new JButton("Custom", b);
custom.setRolloverIcon(x);
add(custom);

HandlerClass handler = new HandlerClass();
reg.addActionListener(handler);
custom.addActionListener(handler);
}

private class HandlerClass implements ActionListener{
public void actionPerfomed(ActionEvent event){
JOptionPane.showMessageDialog(null, String.format("%s", event.getActionCommand()));
}
}
}




import javax.swing.JFrame;

public class apples {
public static void main (String[] args){

Gui go = new Gui();
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go.setSize(300,200);
go.setVisible(true);

}
}

http://snag.gy/6HwXL.jpg

Something bad with my Handler Class.. I getting an error and cant run this project :/ someone have ideas what i need to do?

Post a Reply

Replies

Oldest  Newest  Rating
0 Dana Kennedy · May 20, 2015
I'm a novice at this Java thing myself, but I think this line:
public void actionPerfomed(ActionEvent event){

should be actionPerformed 

You have a typo :)  
(hope that helps) 
0 Valdemar Voinič · May 21, 2015
thanks a lot :)
0 Akshay Pimpalkar · November 2, 2015
Anybody please help me! I can't run this program while whole program seems right. Where i made a mistake please tell me.

import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

public class Gui extends JFrame{

private JButton reg;
private JButton custom;

public Gui(){
super("The title");
setLayout(new FlowLayout());

reg = new JButton("reg button");
add(reg);

Icon l = new ImageIcon(getClass().getResource("lp.png"));
Icon m = new ImageIcon(getClass().getResource("mr.png"));
custom = new JButton("Custom", l);
custom.setRolloverIcon(m);
add(custom);

HandlerClass handler = new HandlerClass();
reg.addActionListener(handler);
custom.addActionListener(handler);
}

private class HandlerClass implements ActionListener{
public void actionPerformed(ActionEvent event){
JOptionPane.showMessageDialog(null, String.format("%s", event.getActionCommand()));
}
}


}


import javax.swing.JFrame;

public class apples {

public static void main(String[] args) {

Gui go = new Gui();
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go.setSize(300,200);
go.setVisible(true);
}

}

  • 1

Java / Android Development

107,246 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator