Undefined Type for JFrame - java

so i was getting a error in Eclipse for my java program to make a new JFrame and i couldnt quiet figure out my it wouldnt load the correct data to set it to be visable and to set the title, location, size, and what it should do when someone closes the JFrame so id like some help if i could please
import javax.swing.*;
import java.awt.*;
public class JFrame
{
public static void main(String[] args)
{
JFrame JF = new JFrame();
JF.setTitle("Test");
JF.setSize(400, 200);
JF.setLocation(200, 300);
JF.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
JF.setVisible(true);
}
}
Thanks for the help in advance

You named your own class JFrame, thus hiding the standard javax.swing.JFrame class.
Choose another name for your class.

Related

Why does my frame shrink once I `setDefaultCloseOperation(false)`?

I have tried to setDefautCloseOperation(false) for my frame login. But the frame shrinks down to be practically invisible. Why does this happen? I have encountered this problem many times. Sometimes, when I re-arrange my code it works, but I want to know how to get over this issue? Is it because of the nature of the swing package?
Here is my code:
package tester_project;
import javax.swing.JFrame;
public class Main {
public static void main(String[] args) {
JFrame login = new JFrame();
login.setTitle("Login");
login.setVisible(true);
login.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
login.setSize(500, 500);
login.setResizable(false);
}
}
But when I move up the setDefaultCloseOperation(false); it works:
package tester_project;
import javax.swing.JFrame;
public class Main {
public static void main(String[] args) {
JFrame login = new JFrame();
login.setTitle("Login");
login.setVisible(true);
login.setResizable(false);
login.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
login.setSize(500, 500);
}
}
Does this happen because java supports procedural code? I am new to java so my question might seem trivial, but can someone explain to me why this is happening?

How to fix 'Cannot resolve method setText(java.lang.String)' error?

I am a beginner at Java and coding. I don't know if this problem is occurring because I forgot something in the code or something is not correct in the code.
I have already tried looking through all the questions on Stack Overflow that are similar to my question and none of them helped me. I've been doing trial and error but still cannot fix it.
import javax.swing.JFrame;
import javax.swing.JLabel;
public class JLabel {
public static void main(String args[]) {
JFrame myFrame = new JFrame();
String myTitle = "Blank Frame";
JLabel label1 = new JLabel("Test");
`````
label1.setText("Test Text");
`````
myFrame.setTitle(myTitle);
myFrame.setTitle(900,600);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setVisible(true);
}
}
At 'label1.setText("Test Text");' is where the problem is. My goal for this code is to create a window that has some text in it. Hopefully, the fix is simple and not as complicated as many codes.
You need to rename your class, as that's an already existing class that you're using. Creating a LJabel object will now create an instance of your object rather than the java.swing equivalent.
You didn't add the label to the frame.
myFrame.add(label1);
You're also calling setTitle() twice when I believe you meant to call setSize().
This code works for me :
public static void main(String[] args) {
JFrame myFrame = new JFrame();
String myTitle = "Blank Frame";
JLabel label1 = new JLabel("Test");
label1.setText("Test Text");
myFrame.add(label1);
myFrame.setTitle(myTitle);
myFrame.setSize(900,600);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setVisible(true);
}
remove the ````` things and you are fine...
or are these just for the example?
This message tells you that the method setText is not define in your defined class JLabel which is not equivalent of JLabel class available in Swing packages.
Rename your own defined class JLabel to JLabelTest for example.
Also, remove this line myFrame.setTitle(900,600);, the definition of setTitle doesn't allow those parameters, and replace it with myFrame.setSize(900,600);
Your complete code should be:
import javax.swing.JFrame;
import javax.swing.JLabel;
public class JLabelTest {
public static void main(String args[]) {
JFrame myFrame = new JFrame();
String myTitle = "Blank Frame";
JLabel label1 = new JLabel("Test");
label1.setText("Test Text");
myFrame.setTitle(myTitle);
myFrame.setSize(900,600);
myFrame.getContentPane().add(label1); // to display the label
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setVisible(true);
}
}
Just an personal advise; try to read very carefully your tutorial and understand how Swing works step by step.
Hope this will help you.

Adding a public class that extends JPanel to JFrame

Basically as the title says, I'm having a bit of an issue working out how to add a class that extends JPanel to a JFrame.
I've looked on here and on other forums, but no answer that I've tried out has work.
I've also got 3 separate files, my main.java my frame.java and panel.java (abbreviated for convenience)
Apparently it's good practice to have public classes in different files(?) or so I've been told!
I'd appreciate any help on how to actually add the JPanel to the JFrame, even just a link to some documentation I may not have seen. Also I'm open to any constructive criticisms that anyone has about the way I've entered/laid out my code.
Thanks!
main.java
public class MyGuiAttempt {
public static void main(String[] args) {
new mainFrame();
}
}
frame.java
public class mainFrame extends JFrame {
public mainFrame() {
setVisible(true);
setTitle("My Game");
setSize(800, 600);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(mainScreenMenu); //This is where I'm clearly going wrong.
}
}
panel.java
public class mainScreenMenu extends JPanel {
public mainScreenMenu() {
JLabel homeMenuBackground = new JLabel(new ImageIcon("images/my_image.jpg"));
setPreferredSize(new Dimension(800,600));
add(homeMenuBackground);
setVisible(true);
}
}
You need to create a new instance of your class
add(new mainScreenMenu());
You're confusing classes and objects. JFrame, JPanel and mainScreenMenu (which should be named MainScreenMenu) are classes. To have a concrete frame, you need to create an object of type JFrame. To have a concrete panel of type MainScreenMenu, you need to create an object of type MainScreenMenu:
MainScreenMenu theMenu = new MainScreenMenu();
add(theMenu);

JFrame does not appear

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.

Getting an error with frame.add() not sure why

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

Categories

Resources