0

Here is my code snippet containing child JButton and JPanel objects but it's not working. And it's not showing any compilation errors in Eclipse.

import java.awt.FlowLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

class gui extends JFrame implements ActionListener {
    private JButton b;
    private TextField c;
    private JLabel l;
    private String sn;

    // Constructor for making framework
    public gui() {  setLayout(new FlowLayout());
    JFrame f=new JFrame("Hello!");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
    f.setSize(200,200);
    f.setTitle("GUI");

    b=new JButton("Click");
    l=new JLabel("Enter Name");
    c=new TextField("Enter..",10);
    c.setEditable(true);
    l.setBounds(20,20,20,20);
    f.setBounds(10, 10, 10, 10);
    b.addActionListener(this);
    add(b);
    add(f);
    add(l);
    add(c);
    } 

    public static void main(String[] args) {
        gui g=new gui();
        g.setVisible(true);
    } //main method

    @Override
    public void actionPerformed(ActionEvent e)
    {
        System.out.println("Working");
    }
}
roeygol
  • 4,908
  • 9
  • 51
  • 88
  • 1
    What do you mean by "not working"? _How_ is it not working? – Mike G Jan 30 '15 at 15:33
  • 2
    Java GUIs have to work on different OS', screen size, screen resolution etc. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Jan 30 '15 at 15:33

3 Answers3

4

Your class "is a" GUI and then you also create a new JFrame, so you really have two frames in your code.

However the frame you make visible does not have any components added to it so all you see is the frame.

You then attempt to add components to your class the is a frame. However, you then have two problems:

  1. you never make this frame visible and

  2. Swing uses layout managers (you don't need to use setBounds(...)). By default it is using the BorderLayout. When you add components to the frame without specifying a constraint the components get added to the "CENTER". However, only one component can be displayed in the "CENTER" so only the last one added will ever be visible.

You also have other problems because you don't create the GUI on the Event Dispatch Thread. So there are really too many problems to correct.

I suggest you read the section from the Swing tutorial on How to Use BorderLayout for a working example of how to create a simple GUI. Then modify that code.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • @camcikr why aint my Jlabel and Jtextfield working only jbutton is working ? – M.Usman Siddiqui Jan 30 '15 at 15:40
  • @M.UsmanSiddiqui, I already explained that. Read my comment about the BorderLayout!!! Read the tutorial for a better way to structure your code. Start with a working example! – camickr Jan 30 '15 at 15:46
1

Your JFrame f=new JFrame("Hello!"); is not needed.
You need to use this which is already your JFrame like:

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setSize(200,200);
this.setTitle("GUI");

Also remove: add(f); and f.setBounds(10, 10, 10, 10);

roeygol
  • 4,908
  • 9
  • 51
  • 88
0

Because you already extend JFrame, you don't have to create a new JFrame.

Because now your class is a JFrame itself. That means that you can replace every usage of your f-JFrame by using this instead:

That way, also your other Components will be added correctly. Because at the moment you add b, f, i and c to the right JFrame.

So use this:

this.setVisible(true);
this.setSize(200,200);
this.setTitle("GUI");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

or even more simple:

setVisible(true);
setSize(200,200);
setTitle("GUI");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Christian
  • 22,585
  • 9
  • 80
  • 106