I'm very new to Java and OOP in general and I want to call my GUI class from my Main class which will be the start point for my program.
This should be pretty simple but any help will be appreciated
GUIForm1 Code:
import javax.swing.*;
import java.awt.*;
public class GUIForm1 {
private JPanel MainPanel;
private JPanel MenuPanel;
Private JPanel AnimationPanel;
private JButton greenTrail;
private JButton purpleTrail;
private JSeparator animationMenuDivider;
private JSlider rangeSlider;
//more components
public GUIForm1() {
JFrame frame = new JFrame("GUIForm1");
frame.setContentPane(new GUIForm1().MainPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setExtendedState(Frame.MAXIMIZED_BOTH);
frame.pack();
frame.setVisible(true);
}
}
Main Class Code:
public class ProjectileSim {
public static void main(String[] args){
GUIForm1 gui = new GUIForm1();
}
}
Your code is good. Your main class instantiation of the GUIForm class is also correct. The reason why you are having an issue is here:
frame.setContentPane(new GUIForm1().MainPanel);
That is your issue in the code. You are trying to call the constructor of your class within your class to set a MainPanel that is non-existent.
If you are using NetBeans (Or any IDE) it is simple enough for you to just drag a JPanel onto your GUI and then you can set your frames content frame to that (for example):
frame.setContentPane(myNewJPanel);
Give it a go. Comment out the line where you are setting your contentframe and see what I mean.
(this is how you comment out by the way :
//frame.setContentPane(new GUIForm1().MainPanel);
You just insert 2 forward slashes in front of the line that you do not want to be executed.
All the best. Let me know of the outcome.
add text Jfield or JButton or change the colour of the panel you feel somthing new.
And the main method is must in java which should exist in public classs.
Your problem is very general. I would advise you to go through https://docs.oracle.com/javase/tutorial/uiswing/start/index.html at the beginning.
But, what you should do in order to get any visible result at all would be:
public GUIForm1() {
JFrame frame = new JFrame("GUIForm1");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setExtendedState(Frame.MAXIMIZED_BOTH);
frame.setVisible(true);
}
Normally you should initialize the GUI objects (like JButtons, JFrame etc.) and then add them to the Content Pane, and after that set everything to visible. But go through the tutorials, since they cover all the basics.
Related
I know this question has been asked a lot and I have done my research but still can not find anything. Below is proof of this before anyone gets upset:
I found this link:
https://coderanch.com/t/563764/java/Blank-Frame-Panel
and this:
Adding panels to frame, but not showing when app is run
and this:
Why shouldn't I call setVisible(true) before adding components?
and this:
JPanel not showing in JFrame?
But the first question says use repaint which I tried with no fix and the second and third to last questions talk about putting setVisible after components added which I have.
The last one talks about making the users JPanelArt a extension (done so) of JPanel instead of JFrame and not to make a frame in a frame constructor (also have not done)
When ever I run this I just get a blank frame, as if the panel was never added in.
I apologise if I have missed something in those links. Anyway below is my classes:
GUI.java (extends JFrame)
package javaapplication2;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GUI extends JFrame{
public GUI(String name) {
super(name);
getContentPane().setLayout(null);
JPanel myPanel1 = new GUIPanel();
myPanel1.setLocation(20, 20);
getContentPane().add(myPanel1);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setResizable(true);
}
public static void main(String[] args) {
JFrame frame = new GUI("Game");
frame.setVisible(true);
}
}
GUIPanel.java (extends JPanel)
package javaapplication2;
import java.awt.*;
import javax.swing.*;
public class GUIPanel extends JPanel {
JButton start;
JButton inspect1;
JButton inspect2;
JButton inspect3;
JButton suspect;
public GUIPanel() {
setLayout(new BorderLayout());
start = new JButton("Start Game");
inspect1 = new JButton("Inspect 1");
inspect2 = new JButton("Inspect 2");
inspect3 = new JButton("Inspect 3");
suspect = new JButton("Choose Suspect");
add(start, BorderLayout.WEST);
add(inspect1, BorderLayout.WEST);
add(inspect2, BorderLayout.WEST);
add(inspect3, BorderLayout.WEST);
add(suspect, BorderLayout.WEST);
}
}
I know it is very simple, but that is because I am following a tutorial from my lecturer to get the hang of things as I previously used a GUI builder which someone in this community pointed out to me is not good to start with (very correct!)
The issue lies in your GUI class when you call getContentPane().setLayout(null). Because of this method call, your JFrame is not displaying anything. If you remove it, your elements should show up.
I also noticed that you were setting each JButton to have a constraint of BorderLayout.WEST. This will most likely put your JButtons on top of each other and render only one of them.
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?
I have this piece of code:
public class GUI extends JFrame {
private PlaneUI planeui;
public GUI(PlaneUI planeui) {
this.planeui = planeui;
}
//We have put the code that creates the GUI inside a method
public GUI() {
start();
planeui.display();
} ...
This is just a test and I need the method "planeui.display" to work when the program starts, together with the method "start();" which already works.
public final class PlaneUI extends JFrame {
public void display() {
//Creates a new JPanel object
JPanel panelStart = new JPanel();
getContentPane().add(panelStart);
//Changing the default layout from Flowlayout to absolute
panelStart.setLayout(null);
setTitle("Reservationer"); //Sets the window title
setSize(236, 256); //Sets the default size of the window
setLocationRelativeTo(null); //Start location of the window (centered)
setDefaultCloseOperation(EXIT_ON_CLOSE); //Exits the window
}
}
I have imported the needed libraries and I feel like the problem lies in an object that isn't created correctly since I get a nullpointerexception. I tried running this planeUI class in the main method and it worked correctly. I just can't get it to work this way..
In function PlaneUI.display() add one last line setVisible(true) because your adding everything but not displaying anything
you have to add this into your display() method:
setVisible(true);
Otherwise, all you are doing is setting all the aspects of the JFrame and adding the JPanel to it. You have to make it visible afterwards.
I have asked a similar question a while ago, so I apologize for asking this if I am abusing the website by doing this, but this question is a bit different. I have a Main class, in which I have created a JFrame, and a JPanel. To avoid clutter, I created another class called "Buttons" to hold all my JButtons. I want to add my JButton to the mainPanel (my JPanel), but I am having trouble inheriting access to the mainPanel from the Button's class.
Here is my Main Class:
package main;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main extends JPanel {
public Main() {
Main m = new Main();
//The main window
JFrame Main = new JFrame("Don't Blink");
Main.setSize(500,500);
Main.setResizable(false);
Main.setVisible(true);
Main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//The main window's panel
JPanel mainPanel = new JPanel(); //I want to add buttons to this panel from the Buttons class
mainPanel.setSize(500, 500);
mainPanel.setVisible(true);
Main.add(mainPanel);
}
public static void main(String[] args) {
Main m = new Main();
}
}
The Buttons class:
package main;
import javax.swing.JButton;
public class Buttons extends Main {
public Buttons(Main m) {
//The button to get a job
JButton workButton = new JButton("Get a job");
mainPanel.add(workButton);//The mainPanel here gives me
//the error "mainPanel can
//not be resolved". It
//doesn't seem to recoginze
//mainPanel
}
}
Few issues here... first of all this is an awful OOP design, but let's see...
You should add as a class variable your mainPanel
public class Main extends JPanel {
JPanel mainPanel;
Now to start you don't declare the type, since you want to have it in a class scope:
this.mainPanel = new JPanel(); // I want to add buttons to this panel from the Buttons class
You are trying to call it in Buttons but you only declared it locally in your Main constructor.
You are calling new Main inside new Main, I believe this will end in an infinite recursion.
remove Main m = new Main(); from your Main constructor.
I don't think that inherit Main on Buttons is good, but if you really need that, Main does not know about Buttons, anyway you could use the main method in Buttons instead of Main. I'd remove the main method from Main class and add it to Buttons class instead since it is extending Main anyway.
public static void main(String[] args) {
Buttons m = new Buttons();
}
Now, buttons claims to need a Main as argument, that is weird because you want to call the Main, not another Main, I'd remove the argument Main from Buttons constructor. You may think that then you will not have mainPanel but Buttons is already a Main class so it has mainPanel too.
From an OOP point of view, this is quite bit strange, and does not seem right.
In my opinion, you should not create a separate class for this, just a method, inside the Main class, and pass the reference of the panel to it:
public class Main extends JPanel{
public Main(){
Main m = new Main();
//The main window
JFrame Main = new JFrame("Don't Blink");
Main.setSize(500,500);
Main.setResizable(false);
Main.setVisible(true);
Main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//The main window's panel
JPanel mainPanel = new JPanel(); //I want to add buttons to this panel from the Buttons class
mainPanel.setSize(500, 500);
mainPanel.setVisible(true);
Main.add(mainPanel);
addTheButtons(mainPanel); //this does the heavy lifting
}
private addTheButtons(JPanel mainPanel){
//The button to get a job
JButton workButton = new JButton("Get a job");
mainPanel.add(workButton);
}
}
This would work as long as you don't want to reuse the logic creating the buttons in other classes - but as far as my experience goes with this, this granularity of UI code doen't get reused much...
First of all, it is bad practice to name a variable name with a capital letter, like Main. Use camelCase.
Please don't use inheritance for this. I recommend you define a method as follows:
private Container getButtonContainer() {
final JPanel buttonPanel = new JPanel(...);
...
buttonPanel.add(button1);
buttonPanel.add(button2);
return buttonPanel;
}
Then add this panel to the main panel.
I'm using the Intelij idea platform.
I have the following code:
package GUI.test;
import javax.swing.*;
public class Frame extends JFrame{
Frame(){}
public void main (String[] args){
new Frame();
}
}
I expected to see a JFrame after compiling this code, but was nothing appeared. What kind of problem can it be?
Frames are not visible by default--use the
setVisible(true);
method in order to display frames.
You also might want to take a look at other options such as
setSize(int width, int height);
method to resize a frame,
setLocation(int xLoc, int yLoc);
to move the frame, and
setTitle(String title);
to set the title of the component.
Aside, it is good practice to use a variable to hold components so they can be manipulated when needed.
You might want to add this
Frame()
{
setVisible(true);
setSize(100,100);
}
package GUI.test;
import javax.swing.*;
public class Frame extends JFrame{
private myFrame;
public Frame()
{
myFrame = new JFrame("put a title here"); //title not necessary but it's there if you want it
myFrame.setSize(400,400); // sets the window size
myFrame.setVisible(true); // toggles the frame to be visible inside the window
myFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // this will terminate the VM once the *last* JFrame is closed, so you can have multiple frames open and just close one
}
public void main (String[] args){
new Frame();
}
}
Thanks a lot for answering. I wrote an example without details. I have added: setVisible(true);
setSize(100,100);
to a class constructor. But i havent seen form jet.
Have to say about one feature. I'm not running method main in class Frame, i'm compiling this class Frame. In my previous exersises with java i allways run main method, but when i tried to work with GUI form and used swing library, ability to run method main have dissapiared.
If you haven't cought what i mean, i'd send a screenshot.