How to name JFrame in a constructor

做~自己de王妃 提交于 2019-12-13 22:47:10

问题


Question may be different than what you expected, but I'm creating a utility function for JFrames to make it easier for future me.

public void setJframe(String title, int w,int h, JFrame name, Boolean maximize){
    name.setSize(w, h);
    name.setTitle(title);

    if (maximize == true) {
        name.setExtendedState(name.getExtendedState()|JFrame.MAXIMIZED_BOTH);
    } else {
        name.setLocationRelativeTo(null);
    }
}

I want the ability to name the JFrame as I type in the parameters. At the moment, when I type in a string it simply spits out an error saying I can't use a string? I want "name" to be like a string variable where I can type in a string value and have the object be named that.

Edit: Need to make the question more clear...

PackageName.setJframe("Title of the Frame", 500, 800, f, false);

Returns this error:

Exception in thread "main" java.lang.Error: Unresolved compilation problems:

f cannot be resolved

f cannot be resolved

at gui.GuiMain.guiSet(GuiMain.java:17)

at urAPackage.Main.main(Main.java:8)

Eclipse says method f is not applicable to constructor


回答1:


Using your method, setJframe, you simply need to pass an instantiated new JFrame into the forth parameter as such:

import java.awt.EventQueue;
import javax.swing.JFrame;

public class CreateJFrame {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    JFrame frame1 = new JFrame();
                    setJframe("Title of the first Frame", 500, 800, frame1, false);
                    frame1.setVisible(true);

                    JFrame frame2 = new JFrame();
                    setJframe("Title of the second Frame", 100, 200, frame2, false);
                    frame2.setVisible(true);

                    JFrame frame3 = new JFrame();
                    setJframe("Title of the third Frame", 100, 200, frame3, true);
                    frame3.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public static void setJframe(String title, int w, int h, JFrame name, Boolean maximize) {
        name.setSize(w, h);
        name.setTitle(title);

        if (maximize == true) {
            name.setExtendedState(name.getExtendedState() | JFrame.MAXIMIZED_BOTH);
        } else {
            name.setLocationRelativeTo(null);
        }
    }
}

If you want the forth parameter to be a string, you could either extend a JFrame and specify your additional constructor(s) to accept the string, or you can create a method whereby a JFrame object is returned.

EDIT: Unless of course, you meant naming the internal variable name. This functionality is not possible during runtime. I can't imagine any use for such a function anyways. The above paragraph assumes you mean setting the name of a JFrame (via setName()).




回答2:


Variable names are only known at compile time. You cannot create a variable name from the value of a String at run time.



来源:https://stackoverflow.com/questions/23975528/how-to-name-jframe-in-a-constructor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!