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.
Related
I'm new to Java Swing (and fairly new to Java in general) and I've been messing around with Swing for a while, but after making some changes my panels stopped displaying and I don't know why.
When using isDisplayable() on my JPanel object it returns false. After more investigation my program does not seem to display graphics at all.
Even a simple piece of code doesn't work for me anymore:
public class window extends JFrame {
window(){
setBounds(100,100,1200,700);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBackground(Color.black);
setVisible(true);
}
public static void main(String[] args) throws IOException{
new window();
}
I have no idea what I changed for this to happen, but all I get is a blank a completely blank window.
Any help is appreciated a lot!
See the code comments.
import java.awt.Color;
import javax.swing.*;
public class BlackWindow extends JFrame {
BlackWindow(){
setBounds(100,100,400,100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// sets the frame black, not the content pane
//setBackground(Color.BLACK);
getContentPane().setBackground(Color.BLACK);
setVisible(true);
}
public static void main(String[] args) {
new BlackWindow();
}
}
This example also speaks to a few general principles we follow when using Swing's JFrame:
Don't extend the JFrame class, just use an instance of one.
Don't do anything significant to the coloring or styling of a frame, instead color (for example) a JPanel and add it to the frame.
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.
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.
so im making a simple box from java and this is the code so far:
import java.awt.Canvas;
import javax.swing.JFrame;
public class Display {
public static final int WIDTH = 800;
public static final int LENGTH = 600;
public static void main(String[] args) {
Display game = new Display();
JFrame frame = new JFrame();
frame.setResizable(false);
frame.setVisible(true);
frame.setSize(WIDTH, LENGTH);
frame.add(game);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
im getting an error here "frame.add(game);" its says "The method add(Component) in the type Container is not applicable for the arguments (Display)"
How im not sure what im doing wrong, im using javaSE-1.6
You're trying to add an instance of Display, which isn't a Swing component, to your frame, hence the error. Looking at your imports, you probably meant to add a JPanel within your Display class (if there is one) to the frame.
Alternatively, your Display class needs to inherit from something like JComponent, if you want to add it directly. You shouldn't mix AWT and Swing components needlessly.
Your display needs to extend Component as that is what the add method expects as a parameter. It's probably a good idea to make it a JPanel.
You mention that you just want a basic window. This will do the trick:
import javax.swing.*;
public class Display extends JFrame {
public static void main(String[] args){
new Display();
}
public Display() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 200);
setVisible(true);
}
}
I've looked at several other posts, and I have yet to find a clear answer. I don't entirely understand the paint method, which is probably my problem, but nowhere can I find a clear explanation. Can someone help me get this one working? The issue is that the paint method is not running. Everything else seems to work fine, but I do not see the oval I tell the program to render in the frame.
import java.awt.Color;
import java.awt.Component;
import java.awt.Frame;
import java.awt.Graphics;
#SuppressWarnings("serial")
public class TestObject extends Component {
MouseResponder mouseListener = new MouseResponder(); // Creates a new mouse listener.
WindowResponder windowListener = new WindowResponder(); // Creates a new window listener.
Frame mainFrame = new Frame(); // Makes a new frame.
public TestObject() {
mainFrame.setSize(400,500); // Makes the new frame 400 by 500 in size.
mainFrame.setLocationRelativeTo(null); // Sets the location of the window to center it.
mainFrame.setTitle("A Test program!"); // Sets frame label.
mainFrame.setBackground(new Color(199,199,199)); // Sets the background color of the window.
mainFrame.addWindowListener(windowListener); // Adds the window listener so close works.
mainFrame.addMouseListener(mouseListener); // Adds the mouse listener to the frame.
mainFrame.setVisible(true); // Makes the new frame visible.
System.out.println("[TestObject] Window" + // Prints a console message when main window is launched.
" configured and launched.");
}
public void paint(Graphics pane) {
System.out.println("[TestObject] Painting.");
pane.setColor(Color.BLACK);
pane.drawOval(10,10,10,10);
}
}
Other Info:
MouseResponder and WindowResponder are separate functioning classes.
The TestObject class seen above is called by a main class which
creates a new TestObject. The frame displays successfully as I
specify.
Thank you for any help!
-Docithe
You are late for your homework buddy !
Take a new java file.
Create a class
Make it extend JFrame
override the paint method
put a println in it
Take a second file
put a main in it
instanciate your first class and call show
move the window around, println should print out, meaning your code in paint is executing.
That's the way to do it in OOP, and for sure in java. Read more.
paint() is responsible for rendering a component when it is visible.
At least in your code snippet, you did not add the test-component to the frame - thus, it is not displayed and not painted.
public TestObject() {
//...
mainFrame.add( this );
//...
}
This still might not work because your Test Component is 0x0 pixel.
So you also
#Override
getPreferredSize(){
return new Dimension( 20, 20 );
}
you are mixing two things here, creating a frame and creating a component. If I understand you correctly, you want to create a frame and within that frame have a custom component draw an oval.
The component is just this:
public class TestObject extends Component {
public void paint(Graphics pane) {
System.out.println("[TestObject] Painting.");
pane.setColor(Color.BLACK);
pane.drawOval(10,10,10,10);
}
}
and your main program looks more like this:
public static void main(String[] args)
{
MouseResponder mouseListener = new MouseResponder();
WindowResponder windowListener = new WindowResponder();
Frame mainFrame = new Frame();
mainFrame.setSize(400,500);
mainFrame.setLocationRelativeTo(null);
mainFrame.setTitle("A Test program!");
mainFrame.setBackground(new Color(199,199,199));
mainFrame.addWindowListener(windowListener);
mainFrame.addMouseListener(mouseListener);
mainFrame.add(new TestObject());
mainFrame.setVisible(true);
}
I am not saying this code will run, but it splits the two things in what they should be, a main program creating the frame, and a component painting an oval. I agree with Snicolas on the reading more...