使用void方法添加按钮 [英] Adding buttons with a void method

查看:110
本文介绍了使用void方法添加按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对编程电路还很陌生,现在我正试图系统地学习Java,并最终在Swing中学习。我想我知道有什么空方法可以使用并且用于什么方法,但是我无法理解这段示例代码(摘录自 http://docs.oracle.com --BorderLayoutDemo):

I'm quite new to the programming circuit and I'm trying to learn Java systematically ending up at Swing for the moment. I think I get what void methods do and are used for but I can't wrap my head around this piece of example code (extracted from http://docs.oracle.com --BorderLayoutDemo):

public class BorderLayoutDemo {

public static void addComponentsToPane(Container pane) {

    JButton button = new JButton("Button 1 (PAGE_START)");
    pane.add(button, BorderLayout.PAGE_START);

}

private static void createAndShowGUI() {

    JFrame frame = new JFrame("BorderLayoutDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    **addComponentsToPane(frame.getContentPane());**
    frame.pack();
    frame.setVisible(true);
}

public static void main(String[] args) {
   createAndShowGUI();

}
}

为什么这段代码有效,导致一个带有1个按钮的窗口(我希望这只会导致一个空窗口)。是不是将frame.getContentPane()作为参数提供给复制到addComponentsToPane()并在此void方法结束时销毁(未添加到frame)?它为什么不返回然后可以添加到框架(即frame.add())的Container对象或JPanel对象。

Why does this piece of code work, resulting in a window with 1 button (I expected this to result in just an empty window). Isn't frame.getContentPane() given as argument copied to addComponentsToPane() and destroyed as this void method ends (not added to frame)? Why shouldn't it return a Container object or JPanel object that then can be added to the frame (i.e. frame.add()).

(问题已编辑。pivot关键字是空的,不是静态的。我看到我已经将此概念(无效方法)与原始类型一起使用,这使问题感到困惑)

(Question edited. The pivot keyword was void, not static. I see I've been taught this concept (void methods) with primitive types which confuses the matter)

推荐答案

只是要澄清@tzhechev,不是Java中通过引用传递对象,而是Java中通过值传递对象引用,阿布在回答中非常清楚地说明了这一点。

Just to clarify @tzhechev, that Objects are not passed by reference in Java instead, Object references are passed by value in Java, which is very much stated by Abu in the answer. To clarify the topic a bit more, watch this example.

class Employee
{
    String name;
    int age;
}

public class ValueOrReference
{
    public static void main(String... args)
    {
        Employee emp1 = new Employee();
        emp1.name = "Myself";
        emp1.age = 1;
        Employee emp2 = new Employee();
        emp2.name = "Yourself";
        emp2.age = 2;
        System.out.println("Before swapping values are : ");
        System.out.println("Employee 1 : ");
        System.out.println("Name : " + emp1.name);
        System.out.println("Age : " + emp1.age);
        System.out.println("Employee 2 : ");
        System.out.println("Name : " + emp2.name);
        System.out.println("Age : " + emp2.age);
        /*
         * Now if Objects are passed by Reference
         * then if I swap the two objects in a method, 
         * then the actual objects will reciprocate
         * to the said change. Lets find out.
         */
        swapObjects(emp1, emp2); 
        System.out.println("****************************************");
        System.out.println("Inside main after swap method.");
        System.out.println("After swapping values are : ");
        System.out.println("Employee 1 : ");
        System.out.println("Name : " + emp1.name);
        System.out.println("Age : " + emp1.age);
        System.out.println("Employee 2 : ");
        System.out.println("Name : " + emp2.name);
        System.out.println("Age : " + emp2.age);
        System.out.println("****************************************");
    }

    private static void swapObjects(Employee emp1, Employee emp2)
    {
        System.out.println("****************************************");
        System.out.println("Inside the swap method.");
        Employee temp = emp1;
        emp1 = emp2;
        emp2 = temp;
        System.out.println("After swapping values are : ");
        System.out.println("Employee 1 : ");
        System.out.println("Name : " + emp1.name);
        System.out.println("Age : " + emp1.age);
        System.out.println("Employee 2 : ");
        System.out.println("Name : " + emp2.name);
        System.out.println("Age : " + emp2.age);
        System.out.println("****************************************");
    }
}

输出情况1:

C:\Mine\JAVA\J2SE\classes>java ValueOrReference
Before swapping values are :
Employee 1 :
Name : Myself
Age : 1
Employee 2 :
Name : Yourself
Age : 2
****************************************
Inside the swap method.
After swapping values are :
Employee 1 :
Name : Yourself
Age : 2
Employee 2 :
Name : Myself
Age : 1
****************************************
****************************************
Inside main after swap method.
After swapping values are :
Employee 1 :
Name : Myself
Age : 1
Employee 2 :
Name : Yourself
Age : 2
****************************************

尽管如果我仅使用一个Object并以某种方法修改其内容,那么结果将是令人惊讶的。尝试下面的代码使您感到意外:

Though if I use only one Object and modify it's contents in some method, then the outcome will be surprising. Try the code that follows for that surprise :

class Employee
{
    String name;
    int age;
}

public class ValueOrReference
{
    public static void main(String... args)
    {
        Employee emp1 = new Employee();
        emp1.name = "Myself";
        emp1.age = 1;
        System.out.println("Before swapping values are : ");
        System.out.println("Employee 1 : ");
        System.out.println("Name : " + emp1.name);
        System.out.println("Age : " + emp1.age);
        /*
         * Now if Objects are passed by Reference
         * then if I swap the two objects in a method, 
         * then the actual objects will reciprocate
         * to the said change. Lets find out.
         */
        modifyObject(emp1); 
        System.out.println("****************************************");
        System.out.println("Inside main after modify method.");
        System.out.println("After modifying values are : ");
        System.out.println("Employee 1 : ");
        System.out.println("Name : " + emp1.name);
        System.out.println("Age : " + emp1.age);
        System.out.println("****************************************");
    }

    private static void modifyObject(Employee emp1)
    {
        System.out.println("****************************************");
        System.out.println("Inside the modify method.");
        emp1.name = "Yourself";
        emp1.age = 2;
        System.out.println("After modifying values are : ");
        System.out.println("Employee 1 : ");
        System.out.println("Name : " + emp1.name);
        System.out.println("Age : " + emp1.age);
        System.out.println("****************************************");
    }
}

输出案例2:

C:\Mine\JAVA\J2SE\classes>java ValueOrReference
Before swapping values are :
Employee 1 :
Name : Myself
Age : 1
****************************************
Inside the modify method.
After modifying values are :
Employee 1 :
Name : Yourself
Age : 2
****************************************
****************************************
Inside main after modify method.
After modifying values are :
Employee 1 :
Name : Yourself
Age : 2
****************************************

这篇关于使用void方法添加按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆