0

I want to know how to put put console output into a JFrame. For example, putting this output into a JFrame:

import static java.lang.System.out;

public class frame{

public static void main(String [] args){  
        out.println("hello");
}  

}

How is it possible?

2

4 Answers 4

1

You need to set up the JFrame first.

JFrame frame = new JFrame("title");

Then, set the properties of the JFrame:

frame.setSize(1280,720); //Sets the program's size
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Tells the program to exit on close
frame.setResizable(true); //Tells the program if resizing is enabled

Then, create a panel to store the components:

JPanel p = new JPanel();

After that, you must add the panel to the JFrame like so:

frame.add(p);

Then, with that done, you can use the components supplied in the swing framework, and add them to the panel. A reference for these components can be found here: http://docs.oracle.com/javase/tutorial/uiswing/components/componentlist.html.


To create a component, use the following code:

JLabel label = new JLabel();

Then, use it's build in functions to change it:

label.setText("new text");

Then, once again, to add a component to a panel, use the panel's add() method:

panel.add(label);

Those are just the basics of making a GUI with java. A full tutorial can be viewed here:

http://docs.oracle.com/javase/tutorial/uiswing/


Good Luck!

1
  • Sorry for the long answer! Hope it helps!
    – DripDrop
    Oct 31, 2014 at 21:46
0

I can help you with this, but let me please fix some syntax errors you have. When you put the import, an import can't be static (that I know of) and when you want to print out something using "System.out.print" or "System.out.println" you MUST include the "System" part of the line. If you want to add text to a a JFrame use the JLabel to import both just do this bit of code:

import javax.swing.*;

That should import all of your swing elements such as JLabel and JFrame and JPanel, and try this code it will make a window that will have a button and a label. The button doesn't do anything in this code:

import javax.swing.*;

public class main{
    public static void main(String[] args)
    {   
 /*
 * Creates the frame, makes it visible, and makes 
 * appear in the center of the screen. While also making it have a close operation
 */
    JFrame frame = new JFrame("Button");
    frame.setVisible(true);
    frame.setSize(300, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);

//Creates the panel, and adds it to the frame
    JPanel panel = new JPanel();
    frame.add(panel);

//Creates the label and adds it to the panel, also sets the text
    JLabel label = new JLabel();
    label.setText("Welcome" + "\n" + "\n");
    panel.add(label);

//Creates the button and adds it to the panel
    JButton button1 = new JButton("Button 1");
    panel.add(button1);

    }   
}   
2
0

1.If you want to use JFrame you have to extend your class to a subclass of JFrame:

public class frame extends JFrame {}

2.a)If you want to put Text in your Frame use JLabel and add it to your frame:

JLabel hello = new JLabel("Hello");
add(hello); 

2.b)If you want a console output just call System.out.println() in the constructor

Here is a small example class:

import javax.swing.JFrame;
import javax.swing.JLabel;

public class Frame extends JFrame {

    public static void main(String args[]) {
        new Frame();
    }
    Frame() {
        System.out.println("Hello");
        JLabel hello = new JLabel("Hello");
        add(hello);
        this.setSize(100, 100);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
}

have a look to the oracle lessons... or any java book!

0

If that is the case, I don't want to get into GUI just yet. (Learning from a book) Can I convert my current project to a jar file and have it automatically open a command prompt window upon double click?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.