Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
330 views
in Technique[技术] by (71.8m points)

java - How to name JFrame in a constructor

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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()).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...