Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Error in JButton Code

 
Avin Sinanan
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can some please tell me hoe to fix the error in this code... i have tried everything.... the code is suppose to do the following
1)It creates a Button with a label on the button.
2)When pressed the label on the button changes to something diffrent.
3)A label at the buttom of the button also appears.
here is the code.. its a very small program...

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ButtonThing implements ActionListener
{
private JPanel jp;
private JButton jb;
private JLabel jl;
private JFrame jf ;
private int presses = 0;

public ButtonThing(String title) {
jf = new JFrame(title);
jp = new JPanel(new BorderLayout());
jb = new JButton("Click Me");
jl = new JLabel();
jp.add(jb, BorderLayout.NORTH);
jp.add(jl, BorderLayout.SOUTH);

jf.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
jf.setContentPane(jp);
jf.setSize(250,250);
jf.setVisible(true);

}

public void actionPerformed(ActionEvent ae) {
++presses;
jb.setLabel("Thank you for clicking");
jl.setText("Press count: " + presses);
}
public static void main(String args[])
{
// Run program with:
// java ButtonThing
String s = "Hello";
ButtonThing bt = new ButtonThing(s);
}
}

Any help would be grealty appreciated... thanks ..
the main error iam getting is this :
Note: C:\jdk1.3\bin\ButtonThing.java uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.

------------------
Thanks a lot
 
Wilfried LAURENT
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apparently you do not register any ActionListenr on your Button. Thus there is no callback performed when pushing the Button. Add the bold line.
public class ButtonThing implements ActionListener
{
...
private JButton jb;

public ButtonThing(String title) {
jb = new JButton("Click Me");
jb.addActionListener(this);
...
}


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic