当getter方法为我做同样的工作时,为什么我需要有setter方法?

问题描述 投票:0回答:2

以下两种方法都返回gui引用类型。

如果我用void替换JFrame和JButton返回类型并删除return语句,它仍然有效。我无法理解两种方法之间的区别。

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();   
    }
}
java swing
2个回答
1
投票

这些方法不需要返回任何内容,因为frame对象存储在它们的类中。如果它在另一个类或main方法中,则需要return语句。

这两种方法都可以访问您的JFrame,因此您可以在其中执行所有操作,但下面是一种更好的方法:

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();     
    }
}

2
投票

createButtoncreateGui应该创建一个按钮和gui,没有别的。当您的代码创建它们并将按钮添加到框架并将框架分配给全局变量时。

请看两个不同的重新实现:

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());
    }
}

要么

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();
    }
}

您将使用第一种情况(返回对象JFrame和JButton),因为您想在其他地方使用它们。

当您希望方法构建UI(更像是状态机)时,您将使用第二种情况。

© www.soinside.com 2019 - 2024. All rights reserved.