-1

I am new to Java and trying to create a button through my class and it has a method with arguments. But when I create two instances of my class, it shows only one button i.e., the latest one. Could you tell me what mistake I have done here?

my class file

public class CreateButton {
    int posx;
    int posy;
    int buttonWidth;
    int buttonHeight;
    public void newButton(int x, int y, int w, int h) {
        posx = x;
        posy = y;
        buttonWidth = w;
        buttonHeight = h;
        JFrame f = new JFrame();
        JButton b = new JButton("Test");
        b.setBounds(posx, posy, buttonWidth, buttonHeight);

        f.add(b);
        f.setSize(400, 400);
        f.setLayout(null);
        f.setVisible(true);

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

my file

class Project {
    public static void main(String[] args) {
        CreateButton myButton1 = new CreateButton();
        CreateButton myButton2 = new CreateButton();
        myButton1.newButton(50, 200, 100, 50);
        myButton2.newButton(100, 250, 100, 50);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Rajesh
  • 195
  • 1
  • 2
  • 15
  • Seems like an odd name for a class that not only creates a button but a frame as well. Why not just create a button? – ChiefTwoPencils Aug 24 '16 at 06:41
  • Generally speaking, if the name you chose for your class is a verb phrase, your design must be faulty (or your naming, or your understanding of what you are doing). Methods are supposed to be "verbs". Classes are supposed to be "objects". In any case, your button is created in a frame of its own. When you close that frame, don't you see the frame of the other button behind it? – RealSkeptic Aug 24 '16 at 06:45
  • Yeah, I got it. Thanks – Rajesh Aug 24 '16 at 06:48
  • 1) There should be two frames on-screen, in exactly the same location. Did you try *moving* the visible frame to reveal the one beneath? 2) `f.setLayout(null);` Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. 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 Aug 24 '16 at 07:01
  • 1
    .. 3) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! 4) Tip: Add @RealSkeptic (or whoever, the `@` is important) to *notify* the person of a new comment. – Andrew Thompson Aug 24 '16 at 07:04
  • Now my class file has code only to create buttons. I moved frame codes to my Project program. How can i add buttons to my frame? I cannot do f.add(b) as I create button from the class. – Rajesh Aug 24 '16 at 07:05

1 Answers1

0

Actually, your code creates two buttons, but the JFrame windows are above each other, so you can see only one. Move the window, and you have two buttons visible.

To add the buttons to the same frame, you need to remove the creation of the JFrame from the createButton() method and add it as a parameter to the function. Create the frame in your main()-method.

As others already commented, the name of the class is somewhat "nonstandard". A better name would be ButtonFactory or UIFactory if you plan to create other widgets there as well. Anyway, consider that the createButton() method returns the initialized button instead of adding it to the JFrame for you. This way you will gain flexibility and you can test the created button's parameters in a unit test more easily. If the button is automatically added to the frame, this is much more complicated to achieve.

import javax.swing.*;

public class CreateButton {


    public static class UIFactory {

        public JButton newButton(int posx, int posy, int buttonWidth, int buttonHeight) {
            JButton b = new JButton("Test");
            b.setBounds(posx, posy, buttonWidth, buttonHeight);

            return b;
        }

        public JFrame newFrame(int width, int height) {
            JFrame f = new JFrame();
            f.setSize(width, height);
            f.setLayout(null);

            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            return f;
        }

        public JFrame mainWindow() {
            JFrame f = newFrame(400, 400);
            f.add(newButton(50, 200, 100, 50));
            f.add(newButton(100, 250, 100, 50));
            return f;
        }
    }

    public static void main(String[] args) {
        UIFactory ui = new UIFactory();
        JFrame main  = ui.mainWindow();
        main.setVisible(true);

        JFrame win2 = ui.newFrame(150, 150);
        win2.setLocation(400, 400);
        JButton b2;
        win2.add(b2 = ui.newButton(50, 50, 100, 50));
        b2.addActionListener( e -> win2.setVisible(false) );
        win2.setVisible(true);
    }
}
thst
  • 4,592
  • 1
  • 26
  • 40