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

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?

Related

GUI doesn't show up on a Mac?

I bought a Mac, I I download netbeans for my java.
package gui;
import javax.swing.*;
import java.awt.*;
public class Gui extends JFrame {
public void Gui(){
setTitle("Gui");
setSize(640,320);
setVisible(true);
}
public static void main(String[] args) {
new Gui();
}
}
It is very easy code and I didn't find any problem with it, but somehow the GUI is not showing up.
is GUI no suppose to show up on a Mac?
Somehow, the program didn't go through the Gui method, I tried
System.out.println("Hello");
didn't show up.
You think you're using a constructor but you are not! The constructor is what makes the app become a JFrame. This line:
public void Gui() {
should be:
public Gui() {
Also, nice to add a setMinimumSize(new Dimension(640,320));
I think the problem is you have a empty container, but I make an example for you:
import javax.swing.*;
public class Main {
private static JPanel panel1;
private static JButton button;
public static void main(String[] args) {
JFrame frame = new JFrame( "Main");
panel1 = new JPanel();
button = new JButton("Button");
panel1.add(button);
frame.setContentPane(panel1);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setTitle("Gui");
frame.setSize(640,320);
frame.setVisible(true);
}
}

Undefined Type for JFrame

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.

Adding button to Frame

I have never actually worked with GUI's before when it comes to Java. I am trying to add a simple button to the JFrame, but it doesn't add. This is the way that I have been seeing online can someone point out what I'm doing wrong?
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.event.*;
public class WavPlayer
{
public void go()
{
JFrame frame = new JFrame("Wav Player");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//JButton play = new JButton("test");
play = new JButton("Test");
frame.setSize(500, 500);
add(play);
}
public static void main(String [] args)
{
WavPlayer player = new WavPlayer();
player.go();
}
}
You have to specifiy where you want your button to be added.
use frame.add(play) instead of add(play)
You also have several other errors in this code, you have to state the type of "play".
To actually see something, you have to set the visibility of your Frame.
Here is my Code for your Problem (I renamed the class, you have to Change it):
public void go(){
JFrame frame = new JFrame("Wav Player");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton play = new JButton("Test");
frame.setSize(500, 500);
frame.add(play);
frame.setVisible(true);
}
public static void main(String [] args)
{
Main player = new Main();
player.go();
}

Java Swing Using Multiple packages/classes

Some time ago I wrote a code in a single main class which worked fine. Later I added JPanel and JFrame in again its single main it again worked fine.
Now I am trying to break the code down and move them into packages.
here is my main
-( src/start/Main.java)-
package start;
import javax.swing.JFrame;
import frames.*;
public class Main {
public static void main(String[] args) {
MainFrame.createFrame();
LogoPanel.createLogoPanel(); //this line is the problem
}
}
here is MainFrame
-(src/frames/MainFrame.java)-
package frames;
import javax.swing.JFrame;
public class MainFrame {
public static JFrame createFrame() {
JFrame frame = new JFrame ("App");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setResizable(false);
frame.setBounds(140,140, 1000, 580);
frame.setVisible(true);
frame.setLayout(null);
return frame;
}
}
here is LogoPanel
-(src/frames/LogoPanel.java)-
package frames;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class LogoPanel {
public static JPanel createLogoPanel(JFrame frame) {
JPanel logoPanel = new JPanel ();
logoPanel.setVisible(true);
logoPanel.setBounds(0, 0, 1000, 80);
logoPanel.setBackground(Color.gray);
logoPanel.setLayout(null);
frame.add(logoPanel);
return logoPanel;
}
}
as I said LogoPanel.createLogoPanel(); is the problem . It does not accept createLogoPanel(JFrame frame) from me ? setting it to null doesn't show the panel at all . anyway I can make this work ?
Your createLogoPanel takes a JFrame type argument.
But I don't see you passing an argument when you are actually invoking the method.
Do something along the lines of :
JFrame frame = MainFrame.createFrame();
LogoPanel.createLogoPanel(frame);
This error seem to suggest a lack of understanding about basic Java syntax and concepts. If you are able to accumulate knowledge in this area it will benefit you greatly in the future.

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