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()
).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…