Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

wats the prob?  RSS feed

 
Tamana Joshi
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class test1 implements ActionListener
{
JButton b;

test1(){
super(new BorderLayout());
b=new JButton("Click Me");;
b.setLayout(new BorderLayout());
b.setPreferredSize(new Dimension(200,80));
add(b, BorderLayout.CENTER);
b.addActionListener(this);
}

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

private static void createAndShowGUI(){
JFrame f = new JFrame("Hello");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}

public static void main(String[] args) {
createAndShowGUI();
}


plz help..
thanks in advance
 
Joe Ess
Bartender
Posts: 9425
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a gander at How To Ask Questions The Smart Way and see if you can't ask a better question than "wats the prob".
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe's right: asking a good question usually helps a lot. But this program is so broken, several things jump out on even a casual glance. For example although JButtons do have a "setLayout" method, it's only as an unintended side effect of their inheritance; you shouldn't use it. Your class "test1" extends Object, and Object has no constructor that takes a LayoutManager, so the super(new BorderLayout()) won't even compile. I'm assuming you want to construct a JFrame with a BorderLayout, but not even JFrame has such a constructor -- you have to use setLayout() on the JFrame. Finally, in your main(), you don't create a test1 object anywhere, but you do create an empty JFrame and show it, so your button won't be in evidence.

So that's the prob.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!