将JFrame setTitle(String)重载到setTitle(int)

时间:2013-03-27 11:09:45

标签: java swing jframe overloading

我的教训是更改JFrame的setTitle方法,因此它允许整数作为参数。怎么做?我必须重载那个方法,对吧?我在setTitle方法中尝试的任何内容都以堆栈溢出结束。

import javax.swing.*;

public class MyFrame extends JFrame
{       
    MyFrame()
    {
        super();
        setSize(400, 400); // Standard initial size
        setVisible(true);
        setDefaultCloseOperation(MyFrame.EXIT_ON_CLOSE);
    }

    MyFrame(int size)
    {
        this();
        setSize(size, size); 
    }

    public void setTitle(int title)
    {           


    }

}


public class MainClass 
{
    public static void main(String[] args) 
    {   
        MyFrame frame = new MyFrame();
        frame.setTitle(1000);
    }
}

2 个答案:

答案 0 :(得分:1)

来自JFrames API

方法setTitle

public void setTitle(String title)
Sets the title for this frame to the specified string.

然后frame.setTitle("1000");将起作用

答案 1 :(得分:1)

对于重载方法,您似乎处于正确的轨道上。尝试:

public void setTitle(int title)
{           
    super.setTitle(""+title);
}

我没有看到限制原始String参数的要求;这只是向您的子类添加另一个重载方法。

注意:同意Robin这是一个有点奇怪且做作的例子......因为通常标题是一个字符串,所以为什么要改变它......