i can some one help me with jframe and panel. i want to create button using jpanel. i look online but they seem to have different ways to do it and i have no idea which is the best way to do this. ex some people extends jframe at top. other create jframe j = new jframe in main etc...

This is what iam trying to do. dont worry about my logic here. this is just so i can understand how to extends etc....
**
main.java will call aaaa.java.
aaaa.java will call bbbb.java
bbbb.java will create one button and that button will have a actionlistener.
**

public class Main extends JFrame{

    public Main() {
        // TODO Auto-generated constructor stub
    }

    public static void main(String[] args){
        aaaa a = new aaaa(); //??
    }
}




public class aaaa extends JFrame{    //???

    public aaaa() {
        bbbb b = new bbbb(); //??
    }
}





public class bbbb implements ActionListener{  //????

    JButton b1 = new JButton("check here");

    public bbbb() {
        //??
    }


public void actionPerformed(ActionEvent e) {

}
}

I hate it to say, but your code makes not very much sense to me.
having all those classes extend JFrame, I mean. anyway, what exactly is it you try to understand yet don't get?

Here is the general order of how you want to create a frame containing a panel containing a button

first you want to create a JFrame, JPanel and a JButton

Then you want to make sure you set the action command of the button (if you have multiple buttons this will allow you to distinguish between them).

Then you want to add the button to the JPanel and then the Panel to the JFrame. You want to set the frame visible and the other important thing is to set the frame's setDefaultCloseOperation() to JFrame.EXIT_ON_CLOSE

If you would like a more detailed guide then take a look at this it should have all the information you need

Some people extend JFrame, others just create JFrame in the code. I see little reason to extend JFrame unless you want to call JFrame's methods (eg setVisible) from another class, but in real life it's never that simple anyway. You don't normally need to extend anything else for your panels, buttons etc unless you have to do something special like override their normal paint methods.

For the ActionListener you can (1) implement that in your main class - that's the smallest code but limits you to one listener for all the buttons, menu items etc in that window, which leads to a huge listener with loads of nested ifs - IMHO a disaster (2) use an anonymous inner class - IMHO the simplest way to add a listner to each button or (3) use a dedicated class - which allows you to create instances with different parameters, eg for responding to the 0-9 buttons in a calculator.

Having said that, the standard beginners addActionListener is usually not a good idea anyway. Swing has Actions that encapsulate a name, an action method, accelerator key,mnemonic, enabled/disabled state etc. You create the Action then just pass that to the button's constructor to set everything up. This is especially valuable when you have a menu item, a toolbar button, and a right-click context menu item that all do the same thing, because they all share the same Action. See
http://docs.oracle.com/javase/tutorial/uiswing/misc/action.html

Here is a link to the beginning of a near 10 part video series on Swing, and developing GUIs in general.The author of this video series, is very well versed in what he is doing, and generally follows the best coding conventions. You may find his style to be up your alley. Feel free to check em' out.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.