info catalyst
info catalyst

Reputation: 39

Why do I need to have setter methods when getter methods do the same work for me?

Both of the below methods return gui reference types.

If I replace JFrame and JButton return types with void and remove return statements, it still works. I couldn't understand the difference between both approaches.

public class JavaGui {

    JFrame frame;

    JFrame createGui(){
        GraphicsConfiguration g = null ;
        frame = new JFrame(g);  
        frame.setTitle("gui");
        frame.setSize(600, 400);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setLayout(null);  

        return frame;
    }

    JButton createButton(){
        JButton button=new JButton();
        button.setBounds(130,100,100, 40); 
        button.setText("aaa");
        button.setSize(100, 40);
        button.setLayout(null);
        frame.add(button);

        return button;       
    }

    public static void main(String[] args){
        JavaGui javaGui=new JavaGui();
        javaGui.createGui();
        javaGui.createButton();   
    }
}

Upvotes: 0

Views: 235

Answers (2)

Keval
Keval

Reputation: 1632

The methods do not need to return anything because the frame object is stored in their class. If it was in another class or in the main method, you would need return statements.

Your JFrame is accessible to both the methods and so you can just do everything within them but below is a nicer way to do it:

public class JavaGui {

    JFrame frame;

    public JavaGui() {
        GraphicsConfiguration g = null;
        frame = new JFrame(g);  
        frame.setTitle("gui");
        frame.setSize(600, 400);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setLayout(null);  
    }

    public void createButton(){
        JButton button = new JButton();
        button.setBounds(130,100,100, 40); 
        button.setText("aaa");
        button.setSize(100, 40);
        button.setLayout(null);

        frame.add(button);      
     }

    public static void main(String[] args) {
        JavaGui gui = new JavaGui();
        gui.createButton();     
    }
}

Upvotes: 1

David
David

Reputation: 3055

createButton and createGui should create a button and the gui, and nothing else. While your code is creating them and adding the button to the frame and assigning the frame to the global variable.

Please see two different re-implementations:

public class JavaGui {
    public static JFrame createGui(){
        GraphicsConfiguration g = null ;
        JFrame frame = new JFrame(g);  
        frame.setTitle("gui");
        frame.setSize(600, 400);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setLayout(null);  
        return frame;
    }

    public static JButton getButton(){
        JButton button=new JButton();
        button.setBounds(130,100,100, 40); 
        button.setText("aaa");
        button.setSize(100, 40);
        button.setLayout(null);
        return button;       
     }

    public static void main(String[] args){
        JavaGui.createGui().add(getButton());
    }
}

or

public class JavaGui {

    static JFrame frame;

    static void createGui(){
        GraphicsConfiguration g = null ;
        frame = new JFrame(g);  
        frame.setTitle("gui");
        frame.setSize(600, 400);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setLayout(null);
    }

    static void addButton(){
        JButton button=new JButton();
        button.setBounds(130,100,100, 40); 
        button.setText("aaa");
        button.setSize(100, 40);
        button.setLayout(null);
        frame.add(button);       
     }

    public static void main(String[] args){
        JavaGui.createGui();
        JavaGui.addButton();
    }
}

You would use the first case (returning the objects JFrame and JButton) because you want to use them somewhere else.

You would use the second case when you want your methods to build the UI (working more like a state machine).

Upvotes: 2

Related Questions