I have a class which has a main method. Now, I want to create an object of the JFrame class in that class, but as a global entity and not inside the main function, how can I do it? It gives me some compilation error when I just cut the instantiation code from inside the main and paste it outside.
public class Driver {
JFrame frame = new JFrame("Frame Heading");
public static void main(String[] args) {
frame.setSize(1000,500);;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
You can use a combination of a static variable and static method:
public class YourClass {
private static JFrame frame = createFrame();
private static JFrame createFrame() {
frame = new JFrame();
// ... more code ...
}
public static void main(String[] args) {
// make use of frame
frame.show();
}
}
You can also use a combination of a static variable and static initializer:
public class YourClass {
private static JFrame frame;
static {
frame = new JFrame();
// ... more code ...
}
public static void main(String[] args) {
// make use of frame
frame.show();
}
}
Related
test.java
import javax.swing.JFrame;
public class test {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setSize(600, 600);
}
}
My Other java File test2.java
import javax.swing.JButton;
public class test2 {
public static void main(String[] args) {
JButton Button = new JButton();
frame.add(Button);
}
}
am trying to call frame to test2.java
The reason you are getting this problem:
When you run a java application, the application's main function will be called. Therefore you should really only have one main function per application.
In your scenario you had 2 main functions. Think of this as 2 different applications. The following scenarios were happening:
When you run the Test class, your application was creating a new JFrame object. That's pretty much it, it ended there. It had no idea that the Test2 class existed.
When you run the Test2 class, your application was creating a new JButton object. Although, your Test2 class had no reference to the frame variable (that is why you were getting an error). It didn't even know there was a Test class.
In order to fix this in your situation, try this:
Test.java
public class Test
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setSize(600, 600);
// By passing the frame as a reference, the function
// will be able to add the button to this frame.
Test2.addButton(frame);
}
}
Test2.java
public class Test2
{
public static void addButton(JFrame frame)
{
JButton button = new JButton();
frame.add(button);
}
}
A more OOP approach:
Here, I made a Driver class that would connect the Test2 and MyFrame classes together.
Driver.java
public class Driver
{
public static void main(String[] args)
{
MyFrame frame = new MyFrame();
Test2.addButton(frame);
}
}
MyFrame.java
public class MyFrame extends JFrame
{
public MyFrame()
{
this.setSize(600, 600);
this.setVisible(true);
}
}
Test2.java
public class Test2
{
public static void addButton(JFrame frame)
{
JButton button = new JButton();
frame.add(button);
}
}
I assume you're trying to add Button to the JFrame frame you created in test To do this, you'll need to make frame visible to what is essentially the global scope, as such:
import javax.swing.JFrame;
public class test {
public static JFrame frame;
public static void main(String[] args) {
frame = new JFrame();
frame.setVisible(true);
frame.setSize(600, 600);
test2.main(args)
}
}
and then, to add the button in test2, you need to access test by name
import javax.swing.JButton;
public class test2 {
public static void main(String[] args) {
JButton Button = new JButton();
test.frame.add(Button);
}
}
I have copied most of my Code from an oracle-example, so I think at least the code I did not add should be correct and I donĀ“t like to change that. But in the oracle-code I can not implement this line to properly close my JFrame: frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
private static void createGUI() {
JFrame frame = new JFrame("NameChooser");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
...
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createGUI();
}
});
}
public void actionPerformed(ActionEvent e) {
if (e.getSource()==skipButton){
frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING)); WindowEvent.WINDOW_CLOSING)); // does not work ofc
}
}
How can I close my JFrame in the actionPerformed-method without destroying this correct way of opening a JFrame ?
Or is this oracle-code just suitable for examples and not for real applications ?
You should make frame an instance field like this:
private static JFrame frame;
private static void createGUI()
{
frame = new JFrame( "NameChooser" );
...
}
the code down here only calls the class(class "oefen") whith the code that makes a jframe and a ball(using the painComponent method)and if you press the arrow keys it moves in that direction...it works fine but when i add the target(jlabel) to the jframe only the target shows and nothing else..please help me to put the target and ball in the same frame
public class theFrame implements KeyListener {
public static JFrame j = new JFrame();
public static ImageIcon tar = new ImageIcon("c://fruit//target.png");
public static JLabel target = new JLabel(tar);
public static JPanel p = new JPanel();
public static void main(String args[]){
j.setSize(500,600);
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
j.setVisible(true);
oefen o = new oefen();
j.add(o);
target.setLayout(null);
target.setSize(100,100);
target.setLocation(250,0);
j.add(target);
}
}
thanks
public class theFrame extends JComponent {
public static JFrame j = new JFrame();
public static ImageIcon tar = new ImageIcon("c://fruit//target.png");
public static JLabel target = new JLabel(tar);
public static JPanel p = new JPanel();
public static oefen o = new oefen();
public static void main(String args[]){
j.setSize(500,600);
j.setLayout(null);
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
j.setVisible(true);
target.setLayout(null);
target.setSize(0,0);
target.setLocation(0,0);
j.add(o);
j.add(target);
}
}
if i set the JFrame Layout to null notihng shows...the code above shows how i set the JFrameLayout cuz i don't know if its wrong.
please help
try setting the JFrames layout to null too, because you use absolute positioning.
How would i be able to run this function from my main() to build the gui, and then use code from elsewhere to handle the button click and retrieve input from the text field?
package main;
import javax.swing.*;
import java.awt.*;
public class Gui {
public static void mainGUI() {
UIManager.put("swing.boldMetal", Boolean.FALSE);
java.net.URL imgApp = ClassLoader.getSystemResource("res/app.png");
JFrame mainWin = new JFrame("jIRC");
mainWin.setSize(1024, 720);
mainWin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainWin.setIconImage(new ImageIcon(imgApp).getImage());
Container panel = mainWin.getContentPane();
panel.setLayout(null);
JTextArea inputBox = new JTextArea();
inputBox.setSize(300, 100);
inputBox.setLocation(500, 250);
JButton sendButton = new JButton();
sendButton.setText("Send");
sendButton.setFont(new Font("Helvetica", Font.ITALIC, 16));
sendButton.setSize(72, 32);
sendButton.setLocation(500, 500);
panel.add(inputBox);
panel.add(sendButton);
mainWin.setVisible(true);
}
}
Here's my class with the main function:
public class Run{
public static void main(String[] args) {
main.Debug.startupDebug();
main.Gui.mainGUI();
}
}
How would I go about placing some of my code in a non-static field?
You've got everything in a static method, and that won't allow you to use any of the power of object-oriented programming. Consider creating OOP-compliant classes non-static fields and methods and with public getter and setter methods, and this way other classes can affect the behavior of this class.
Edit
You posted:
public class Run{
public static void main(String[] args) {
main.Debug.startupDebug();
main.Gui.mainGUI();
}
}
But what you need to do instead is something like:
public class Run{
public static void main(String[] args) {
GUI gui = new GUI();
Debug debug = new Debug();
debug.setGui(gui);
gui.setDebug(debug);
gui.startGui();
}
}
Or something similar. Again avoid using static anything.
i have created a JFrame in netbeans. But when i run the program, the Jframe size is too small.
here is my code.
import javax.swing.JFrame;
public class Window {
private static void demo()
{
JFrame frame =new JFrame();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable(){
public void run()
{
demo();
}
});
}
}
You can use frame.setSize(width, height) in order to set its size or frame.setBounds(x, y, width, height) for setting both the location and size.
A better choice would be to call frame.pack() after you add some components to its content pane.
Try this way...
Use the setSize(width, height) method of JFrame.
public class Myframe extends JFrame{
public Myframe(){
this.setSize(300,300);
}
public static void main(String[] args){
EventQueue.invokeLater(new Runnable(){
public void run(){
Myframe f = new Myframe("Frame");
f.setVisible(true);
}
});
}
}
If you want to maximize it you could try
this.setVisible(false);
this.setExtendedState(MAXIMIZED_BOTH);
this.setVisible(true);
this.setResizable(false);
Else for some specific size use
this.setSize(width,height);
You just need to add one line line so that you can give your frame a size.The line is
frame.setSize(300,300);
Here is the full code:
import javax.swing.JFrame;
public class Window {
private static void demo()
{
JFrame frame =new JFrame();
frame.setSize(300,300);
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable(){
public void run()
{
demo();
}
});
}
}
You must have a public component method like
public gestion_name_of_class() {
initComponents();
}