Program with JFrame won't start - java

I was trying to write a program, where you put some numbers in a JTextField an then it does something with it. I tried figuring out how to even make an input possible. But the way I'm trying it doesn't work despite Eclipse showing no errors. And yes, I know there is no way to stop this program but this is just a test.
import javax.swing.*;
public class NotenEingabe extends JFrame {
public static void main(String[]args) {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JLabel label = new JLabel("text");
JTextField field = new JTextField("text");
panel.add(label);
panel.add(field);
frame.setTitle("Grade input");
frame.pack();
frame.add(panel);
frame.setVisible(true);
}
}
I hope it is a real problem and not simply my tiredness.

frame.setVisible(true);
frame.add(panel);
Components should be added to the frame BEFORE the frame is made visible.
The layout manager is not invoked so the components have a size of (0, 0) which means there is nothing to paint.
frame.setTitle("Grade input");
frame.add(panel);
//frame.setSize(700, 700);
frame.pack();
frame.setVisible(true);

First of all when you extend your class from jframe you dont need to instantiate new jFrame just make a new instance of your class then access jframe with that or just delete extend Jframe and use functional approach.
The you didn't set de layout of pane
Test this one then you can choose from veriety of layouts
panel.setLayout(newFlowLayout());

Related

Change Jframe content without opening a new frame

I have a program that opens up a Jframe. inside this Jframe, I have a button that opens another class that has a Jframe too. Is there any way to to get the new class, and new gui opened in the same frame as the first one, and remove the existing content in the first frame?
This is the first class:
public class FirstFrame {
public FirstFrame() {
JFrame frame = new JFrame();
frame.setTitle("Title");
frame.setLayout(new BorderLayout());
frame.setSize(500,350);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Code for a button, that has an actionlistener wich opens the next
//class
frame.setVisible(true);
frame.setResizable(false);
}
public static void main(String[] args){
new FirstFrame();
}
}
The second class is almost like this one. The difference between the two frames is that they have different content.
When I run my program, everything works just fine, but I get two windows. Also, it says in the task manager that the two frames are running as two different programs.
Is there a way to make the content and even the name of the frame change, instead of having to deal with two different frames?
You can change the content pane of your JFrame. Let's say you have two JComponents called a and b. You initialize your frame:
JFrame frame = new JFrame();
frame.setContentPane(a);
Then you can just call
frame.setContentPane(b);
to change the content. You may also need to call revalidate and repaint to refresh the frame.
In the below example, pressing the button changes the content of JFrame from single JButton a to JLabel b. You can use any JComponent instead, such as JPanel containing multiple JComponents.
public static void main(String[] args) {
final JFrame frame = new JFrame();
frame.setTitle("Title");
frame.setSize(500, 350);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton a = new JButton("button A");
frame.setContentPane(a);
frame.setVisible(true); // calling setVisible after content pane has been set to refresh a frame
a.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JComponent b = new JLabel("label B");
frame.setContentPane(b);
frame.revalidate();
}
});
}

My JFrame window won't appear

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);

Putting everything into a JFrame

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?

A Small Guessing Game GUI opens in two windows

Alright so here's the problem:
I created this small guessing game and wanted to make it gui based...
but it appears in two different windows -
first window is the menu(button and label)
second window is activated by the button and has the game in it.
Is there a way for me to have both windows in one- AS IN the first window gets overwritten by the second window?
public class Skeleton extends JFrame implements ActionListener
{
JPanel glass = new JPanel();
JButton btn = new JButton("Start");
TextField tf = new TextField();
JLabel label = new JLabel("Enter Guess Here: ");
JLabel answerLabel = new JLabel("Answer:...");
Board bob =new Board();
public Skeleton()
{
setIconImage(new ImageIcon("icon.png").getImage());
getContentPane().setBackground(Color.darkGray);
getContentPane().setForeground(Color.black);
setLayout(new FlowLayout(FlowLayout.CENTER,10,10));
add(label);
label.setForeground(Color.black);
add(tf);
tf.setText("");
add(btn);
btn.setBackground(Color.green);
btn.addActionListener(this);
add(answerLabel);
answerLabel.setForeground(Color.black);
setTitle("Guessing Game");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(300,200);
setLocationRelativeTo(null);
setVisible(true);
setResizable(true);
}
public void actionPerformed(ActionEvent e)
{//GAME FRAME
JFrame frame = new JFrame();
int userGuess= Integer.parseInt(tf.getText());
frame.add(bob);
frame.setVisible(true);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setSize(300,285);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setIconImage(new ImageIcon("icon.png").getImage());
...........
This is obviously not the full code, just the part that uses the GUI.
NOTE: I do have some other within this actionevent which occur after the button is clicked.
Also this is my first game ever :D (if i can call it one)
You create new JFrame into "actionPerformed" method, so it's normal to have two frames (the Skeleton instance and the new one).
Just use "this" instead of a new JFrame instance to keep only one JFrame instance.
Each JFrame will create a new window. If you want to display everything into the same window, create only one JFrame and change its content.
You can change the whole content of a JFrame by setting its contentPane (frame.setContentPane(...)) or by adding/removing Panel to it.
I suggest to take a look at JPanel since you will use it a lot.

Why is the window too small if I don't resize it?

I am wondering why when I enter this code without resizing the window, I cannot see anything:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GolfScoresGUI
{
public static void main(String[] args)
{
JFrame frame = new JFrame("GolfScoresGUI");
JLabel label = new JLabel("Did you score it? ");
JTextField textField = new JTextField(10);
frame.setVisible(true);
frame.getContentPane().add(textField);
}
}
add your components to a panel on which you call setPreferredSize, add the panel to the frame and call JFrame.pack().
JFrame.pack() updates the size of the frame to take the minimum possible size, given the size on its contained elements.
If you don't call it, the size will be something like 0x0, explaining why you don't see anything.
JFrame frame = new JFrame("GolfScoresGUI");
JPanel panel=new JPanel();
panel.setPreferredSize(new Dimension(600,400)); // Not mandatory. Without this, the frame will take the size of the JLabel + JTextField
frame.add(panel);
JLabel label = new JLabel("Did you score it? ");
JTextField textField = new JTextField(10);
panel.add(label);
panel.add(textField);
frame.setVisible(true);
frame.pack();
EDIT
btw, you should also add this line so that your application stops when you close the frame :
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Everything is fine but you are not specifying any size for that JFrame. That is the problem. Try giving frame.setSize(width,height), or frame.pack(). By using one of this your problem will be solved.
http://docs.oracle.com/javase/tutorial/uiswing/components/frame.html
have a look at this to know about using JFrame in detailed.
Be carefull while using setVisible(true). Try to place setVisible(true) at the end of your GUI code, i.e: use it after adding all the GUI components to the container, because some times when you are adding more components it will not display some components until the frame is resized.

Categories

Resources