如何在构造函数中命名JFrame [英] How to name JFrame in a constructor
问题描述
问题可能与您预期的有所不同,但是我正在为JFrames创建一个实用程序函数,以使将来对我来说更容易.
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);
}
}
我希望能够在输入参数时为JFrame命名.此刻,当我输入一个字符串时,它只会吐出一个错误,提示我不能使用字符串?我希望名称"像一个字符串变量,在其中可以输入字符串值并将对象命名为该变量.
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.
编辑:需要使问题更清楚...
Need to make the question more clear...
PackageName.setJframe("Title of the Frame", 500, 800, f, false);
返回此错误:
线程"main"中的异常java.lang.Error:未解决的编译问题:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
f无法解决
f cannot be resolved
f无法解决
在gui.GuiMain.guiSet(GuiMain.java:17)
at gui.GuiMain.guiSet(GuiMain.java:17)
在urAPackage.Main.main(Main.java:8)
at urAPackage.Main.main(Main.java:8)
Eclipse表示方法f不适用于构造函数
Eclipse says method f is not applicable to constructor
推荐答案
使用方法setJframe
,您只需要将实例化的新JFrame传递给第四参数,如下所示:
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);
}
}
}
如果希望第四个参数为字符串,则可以扩展JFrame并指定其他构造函数以接受该字符串,也可以创建一个方法以返回JFrame对象.
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.
当然,除非您是要命名内部变量名称.运行时无法使用此功能.无论如何我都无法想象这种功能有什么用.上一段假设您的意思是设置JFrame的名称(通过setName()
).
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()
).
这篇关于如何在构造函数中命名JFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!