I'm fairly new to working with graphics in Java, and have been trying to make a simple console to display text based games in a window. I have a test class, where I'm working on the console, but when I add a JTextArea to my console window, it either takes up the entire window or doesn't display at all.
Here is my code:
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.Event;
public class GUI {
public static void main(String[] args) {
JFrame frame = new JFrame("AoA");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setSize(1020,760);
frame.setBackground(Color.LIGHT_GRAY);
frame.setResizable(false);
JTextArea jta = new JTextArea(100,100);
jta.setEditable(false);
jta.setBackground(Color.WHITE);
frame.add(jta);
}
}
I know that some of my imports aren't used in this file, but they will used in the final game. I am also aware that the JTextArea is set to size 100,100, which I am unsure whether it is too large or small. I could really use some help on this, though.
Your problem is the installed by default BorderLayout in your frame. The easiest way to solve your problem is to set another layout manager.
public static void main(String[] args) {
SwingUtilities.invokeLater(GUI::startUp);
}
private static void startUp() {
JFrame frame = new JFrame("AoA");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1020,760);
frame.setBackground(Color.LIGHT_GRAY);
frame.setResizable(false);
frame.setLayout(new FlowLayout()); // FlowLayout is required
JTextArea jta = new JTextArea(40,40);
jta.setEditable(false);
jta.setBackground(Color.WHITE);
// JScrollPane to get the scroll bars when required
frame.add(new JScrollPane(jta));
// setVisible should be last operation to get a correct painting
frame.setVisible(true);
}
Please take a look for layout managers in Swing
Related
I am making a small game for my enjoyment and when I add my JPanel to the JFrame nothing is showing up that i have drawn in my other class.
Here is my main class
package Game;
import java.awt.*;
import javax.swing.*;
import Game.Player;
public class Frame {
static JFrame f = new JFrame("xx");
public static JLabel points = new JLabel();
public static void main(String[] args) {
Player pl = new Player();
JPanel p = new JPanel();
points.setFont(new Font("Verdana",1,20));
p.add(pl);
p.add(points);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setResizable(false);
f.setSize(800, 800);
f.setTitle("Box Game");
f.add(p);
f.setVisible(true);
}
}
I am having a hard time figuring it out so can someone please help me... THANKS!
nothing is showing up that i have drawn in my other class.
You are add components to a panel. A JPanel uses a FlowLayout by default. The FlowLayout respects the preferred size of component you add to the panel. If the components don't paint then the preferred size of the component is probably (0, 0).
nothing is showing up that i have drawn in my other class
If you are doing custom painting then you need to make sure you override the getPreferredSize() method of the class to return the size so the layout manager can do its job.
Read the section from the Swing tutorial on Custom Painting for more information and working examples to get you started.
I'm trying to add the ScrollPane to my TextArea, but it doesn't appear.
Here's the code:
import javax.swing.*;
public class PracownikGui extends JFrame {
private JPanel Panelek;
private JTextArea Tekscik;
private JScrollPane Skrol;
public PracownikGui() {
setMinimumSize(new Dimension(600, 600));
setLocationRelativeTo(null);
setContentPane(Panelek);
setResizable(false);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
Tekscik();
public void Tekscik() {
Tekscik = new JTextArea(2, 10);
Skrol = new JScrollPane( Tekscik );
Tekscik.setSize(300, 300);
Tekscik.setLocation(20, 70);
Tekscik.setEditable(true);
Tekscik.setLineWrap(true);
add(Tekscik);
}}
Any help, please.
You're shooting yourself in the foot by setting a JTextArea's size or preferredSize since this prevents it from expanding into the JScrollPane:
Tekscik.setSize(300, 300);
set its rows and columns only.
Also you need to add the JScrollPane to the GUI, not the JTextArea.
Also, while null layouts and setBounds() or setSize(...) and setLocation(...) might seem to Swing newbies like the easiest and best way to create complex GUI's, the more Swing GUI'S you create the more serious difficulties you will run into when using them. They won't resize your components when the GUI resizes, they are a royal witch to enhance or maintain, they fail completely when placed in scrollpanes, they look gawd-awful when viewed on all platforms or screen resolutions that are different from the original one.
e.g.,
import javax.swing.*;
public class PracownikPanel extends JPanel {
private JTextArea tekscik = new JTextArea(5, 25);
public PracownikPanel() {
tekscik.setLineWrap(true);
tekscik.setWrapStyleWord(true);
JScrollPane skrol = new JScrollPane(tekscik);
skrol.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
add(skrol);
}
private static void createAndShowGui() {
PracownikPanel mainPanel = new PracownikPanel();
JFrame frame = new JFrame("PracownikPanel");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
I made quite a few changes to your code. Your code wouldn't run.
Here's the GUI I created.
As you can see, there's a vertical scroll bar. The default action for the scroll bar is that it doesn't appear until you've filled the JTextArea with text.
Here are the important changes I made to your code.
Class names start with a capital letter. Method names and variable names start with a lower case letter.
A Swing application must start with a call to the SwingUtilities invokeLater method. This ensures that the Swing components are created and used on the Event Dispatch thread (EDT). Since the invokeLater method requires a Runnable, I had the PracownikGui class implement Runnable.
You use Swing components. You don't extend Swing components, or any other Java class, unless you want to override one of the methods in that class.
I removed all of the sizing and positioning statements, except for the statement that defines the rows and columns of the JTextArea. Hovercraft Full Of Eels explained this, but you use Swing layouts to get the arrangement of Swing components you want. The default layout for a JPanel is the FlowLayout. The default layout for a JFrame is the BorderLayout.
I added the JScrollPane to the JPanel. I added the JPanel to the JFrame.
Here's the code.
package com.ggl.testing;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class PracownikGui implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new PracownikGui());
}
private JFrame frame;
private JPanel panelek;
private JTextArea tekscik;
private JScrollPane skrol;
#Override
public void run() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
panelek = new JPanel();
tekscik(panelek);
frame.setContentPane(panelek);
frame.setResizable(false);
frame.pack();
frame.setVisible(true);
}
public void tekscik(JPanel panelek) {
tekscik = new JTextArea(2, 20);
tekscik.setEditable(true);
tekscik.setLineWrap(true);
skrol = new JScrollPane(tekscik);
panelek.add(skrol);
}
}
Recently every single one of my apps that extends JFrame fails to actually show the frame. The program will run and then will terminate after about 8 seconds without ever showing anything and without an error message. This happens for all the programs I've made in the past as well as any new programs.
For testing purposes I am using the basic example from the oracle documentations.
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class test extends JFrame{
public static void main(String[] args){
//1. Create the frame.
JFrame frame = new JFrame("FrameDemo");
//2. Optional: What happens when the frame closes?
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//3. Create components and put them in the frame.
JLabel emptyLabel = new JLabel();
frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
//4. Size the frame.
frame.pack();
//5. Show it.
frame.setVisible(true);
}
}
I am using eclipse and I've already tried switching workplaces.
I've looked at existing threads and found nothing that wasn't due to coding errors.
How can i solve this?
EDIT: The program doesn't output anything from System.out.println() after this line:
JFrame frame = new JFrame("FrameDemo");
It outputs anything before that.
Alright so I checked your code and it works.
First things first, your JLabel isn't visible if it doesn't have anything in it.
pack();
//basically CRUSHES the entire frame if it doesn't have objects to collapse on.
Also, if you don't want to put anything in your JLabel, don't pack it yet. Just set it to a certain size with
frame.setSize(width,height);
It only shows nothing because you collapsed it without putting anything in the JLabel. I hope I answered your question basically this is what you want
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class test extends JFrame{
public static void main(String[] args){
//1. Create the frame.
JFrame frame = new JFrame("FrameDemo");
//2. Optional: What happens when the frame closes?
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//3. Create components and put them in the frame.
JLabel emptyLabel = new JLabel("BLAHBLAHBHALBAHLKKDJF");
frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
//4. Size the frame.
frame.pack();
//5. Show it.
frame.setVisible(true);
}
}
Or you could just set size with
frame.setSize(width,height);
Have fun!
My JFrame window won't appear, though, by the tutorial I've been watching, I've been doing everything spot on, yet nothing happens. It doesn't even give me an error, which makes it so much worse. This is the code:
import javax.swing.*;
import java.awt.*;
public class Window {
public class Window {
public void newWindow() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
JLabel label = new JLabel("I am a star! A beautiful shining star!", SwingConstants.CENTER);
frame.getContentPane().add(label);
frame.setVisible(true);
}
}
(This is a second class, the main one properly links to this one)
JFrame uses a layout manager named BorderLayout by default. To see components added to the frame, you should refer to its javadocs. However, the easiest choice here is to use FlowLayout. You also should use JFrame's pack() method, which, according to Oracle:
Causes this Window to be sized to fit the preferred size and layouts of its subcomponents. The resulting width and height of the window are automatically enlarged if either of dimensions is less than the minimum size as specified by the previous call to the setMinimumSize method.
Thus, this should work for you:
import javax.swing.*;
import java.awt.*;
public class Window {
public void newWindow() {
JFrame frame = new JFrame();
frame.getContentPane().setLayout(new FlowLayout()); // specify the layout manager
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
JLabel label = new JLabel("I am a star! A beautiful shining star!", SwingConstants.CENTER);
frame.getContentPane().add(label);
frame.pack(); // handles sizing of the window
frame.setVisible(true);
}
}
Notice that I removed the duplicate public class Window declaration, which might have been the reason you didn't see a frame at all. If it's still not working for you, I think you aren't calling the newWindow() method. If you want the window to show by simply calling new Window();, then you should change public void newWindow() to public Window().
import javax.swing.*;
import java.awt.*;
public class Window {
public void newWindow() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel("I am a star! A beautiful shining star!", SwingConstants.CENTER);
frame.getContentPane().add(label);
frame.setVisible(true);
frame.pack();
}
}
From what I see, your not setting the size of the JFrame. Try,
frame.setSize(500, 500);
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?
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!
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);
}
}
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!
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?