• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Error in JButton Code

 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
...
}


 
eat bricks! HA! And here's another one! And a tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic