Jframe window will not work anymore - java

package Jframe;
import java.awt.Color;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class JavaWindows {
public static void main(String args[]) {
JFrame Window = new JFrame();
JLabel Label = new JLabel("test");
JPanel Panel = new JPanel();
ImageIcon icon = new ImageIcon("Heart.png");
Window.setIconImage(icon.getImage());
Window.add(Panel);
Window.add(Label);
Window.setSize(500,750);
Panel.setSize(500, 900);
Window.getContentPane().setBackground(Color.PINK);
Window.pack();
}
}
About 10 min ago this code worked. It made a window that was about 100,100 with a 500,900 panel that was sized correctly. The window was not 500,750 like i specified I don't know what went wrong there but all sudden the window wont run in Eclipse. I deleted window.pack(); and retyped it now its broke. Any ideas?

you have to set visibility of the frame
window.setVisible(true);

Related

How to locate and set size of both scroll pane and the components inside the scroll pane?

I am quite new to Java GUI, and I have a question about JScrollPane:
Why does my scroll_pane not show up?
package good_Package;
public class Class_Operation
{
public static void main(String[] args)
{
Class_Frame frame = new Class_Frame();
frame.setVisible(true);
}//END MAIN
}//END CLASS
And the frame class is below.
package good_Package;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Class_Frame extends JFrame
{
JTextArea text_area;
JScrollPane scroll_pane;
public Class_Frame()
{
//Basics
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("This is a title.");
getContentPane().setPreferredSize(new Dimension(500, 500));
pack();
setResizable(false);
setLayout(null);//This is a null Layout
getContentPane().setBackground(Color.GREEN);//frame has a different color from the scroll_pane
//END Basics
text_area = new JTextArea(20, 20);
scroll_pane = new JScrollPane(text_area);
scroll_pane.setBackground(Color.CYAN);//scroll_pane has a different color from the frame
scroll_pane.setVisible(true);
getContentPane().add(scroll_pane);
}//END CONSTRUCTOR
}//END CLASS
Thank you for all suitable answers come from the future.

Why is the textfeld not enlarging on the JFrame?

Below is a Java program which does not work properly. I wanted the JTextfield to go to the bottom, and be 50x50, but it is not working. I was wondering if I could get some help.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class Main {
public static JTextField intext = new JTextField();
public static JPanel panel = new JPanel();
public static boolean running = true;
public static String runtext;
public static String word = "HELLO";
public static String guesses = "";
public static void main(String[] args) {
intext.setBounds(5,667,50,50);
panel.add(intext);
JFrame frame = new JFrame("Hangman");
frame.setBounds(10,10,600,750);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.setResizable(false);
frame.setVisible(true);
I inferred that when using the .setBounds() method the JTextfield would go where it was needed, though it remains at the top, and extremely thin.
Try editing your code this way:-
panel.setLayout(null); // setting layout for panel
panel.setBounds(0,0,600,650); // setting bounds for panel with respect to frame
intext.setBounds(5,557,50,50); //setting bounds for textfield with respect to panel
panel.add(intext);
JFrame frame= new JFrame("Hangman");
frame.setBounds(10,10,600,750);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.setResizable(false);
frame.setLayout(null); // setting layout for frame
frame.setVisible(true);
Make sure to include layouts for your components and set the bounds for your JPanel's and JTextField's properly.

How to setup radio button in jpanel within the jframe?

The radio buttons don't seem to be grouped together properly as one of them is slightly slanting towards the left. I am not sure what the error is. Everything in the code seems fine to me...I am not sure what is missing.
I have attached an image below showing the problem. The Ide I am using is NetBeans.
Thank you in advanced! :)
package pizzaorder2;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.FlowLayout;
import javax.swing.SwingUtilities;
import javax.swing.JRadioButton;
public class PizzaOrder2 extends JFrame {
public static void main(String[] args) {
JFrame frame = new PizzaOrder2();
JRadioButton tomato = new JRadioButton("Tomato");
JRadioButton barbeque = new JRadioButton("Barbeque");
ButtonGroup group = new ButtonGroup();
group.add(tomato);
group.add(barbeque);
JPanel radiopanel = new JPanel();
radiopanel.add(tomato);
radiopanel.add(barbeque);
frame.getContentPane().add(radiopanel);
radiopanel.setBounds(240,330,110,70);
radiopanel.setOpaque(false);
tomato.setForeground(Color.white);
barbeque.setForeground(Color.white);
frame.setLayout(null);
frame.setSize(600, 700);
frame.getContentPane().setBackground(new Color(40, 80, 120));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Try adjusting radiopanel to this:
JPanel radiopanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
to explain a little better, you're just setting it up so items align on the left, rather than the center (which I believe is default). You will need to import FlowLayout for this.

JButton takes entire frame

I'm creating a login system and when I made my login button, it took up the entire frame. I tried the .setBounds(); but it did not work. Not sure what I did wrong, please help.
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class Main extends JFrame{
public Main(){
JTextField name = new JTextField("Username");
JTextField pass = new JTextField("Password");
JButton login = new JButton("Login");
name.setBounds(230, 110, 100, 25);
pass.setBounds(230, 145, 100, 25);
login.setBounds(230, 165, 100, 25);
add(name);
add(pass);
add(login);
}
public static void main(String [] args) {
Main a = new Main();
a.setDefaultCloseOperation(EXIT_ON_CLOSE);
a.setSize(500, 300);
a.setLocationRelativeTo(null);
a.setVisible(true);
a.setTitle("Login System");
a.setResizable(false);
a.setLayout(new FlowLayout());
}
}
in your constructor, try setLayout(new FlowLayout());.
This will likely not lead to what you want, but from there on you can investigate further. i recommend you read about layouts here:
https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
By default, Frame has a BorderLayout installed. When the items are added they are all added to "centre" because constraints are not passed. And the centre component occupies all free space in the BorderLayout. You can either change the layout manager or provide constraints while adding components.
FlowLayout or BoxLayout are good candidates for this.
Problem is by default the layout manager for JFrame is BorderLayout. Once you add components with method add() it gets added to the center region. So the last component added is shown. In your case its the login button. Also setBounds() don't work with the said layout manager.
You have to work a lot on your coding style. What you did is first created the frame and added components to it and later in the main() you have set the size, made it visible and then you set the layout manager to FlowLayout.
Ideally, you must construct the frame, set layout, add components to frame, use pack() to set frame size, set frame's location and finally make frame visible.
Using BorderLayout:
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class Main implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Main());
}
#Override
public void run() {
JFrame frame = new JFrame("BorderLayout");
frame.getContentPane().add(new JTextField(15), BorderLayout.WEST);
frame.getContentPane().add(new JPasswordField(15), BorderLayout.CENTER);
frame.getContentPane().add(new JButton("Login"), BorderLayout.EAST);
frame.pack();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
}
}
Using FlowLayout:
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class Main implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Main());
}
#Override
public void run() {
JFrame frame = new JFrame("FlowLayout");
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(new JTextField(15));
frame.getContentPane().add(new JPasswordField(15));
frame.getContentPane().add(new JButton("Login"));
frame.pack();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
}
}
P.S. If you still want to use absolute positions, I would recommend you using SpringLayout or GroupLayout.

Can't Get ImageIcon to Display

I've looked at a ton of other questions where people have similar issues to what I'm having here (most solutions found by simple mistakes) but I can't for the life of me figure out why my graphics won't display in my jframe. I'm pretty new to Java Graphics so I'd appreciate all the help that I can get. (If someone thinks this is a repeat question, all I ask is that you wait until I get an answer before you close it)
Oh, also, when I run the program, it tells me that the repaint method is called, if that helps in some way
package game.try5;
import java.awt.Dimension;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Window {
public Window() {
JFrame frame = new JFrame("Epic Game");
frame.setSize(800,600);
frame.setLocationRelativeTo(null);
frame.setLayout(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GamePanel panel = new GamePanel();
frame.add(panel);
frame.setVisible(true);
}
public static void main(String[] args){
Window window = new Window();
}
}
.
package game.try5;
import java.awt.Graphics;
import javax.swing.JPanel;
public class GamePanel extends JPanel{
GameObject go = new GameObject(0,0,false,"Dog.jpg");
public GamePanel(){
repaint();
System.out.println("Repaint method called");
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(go.getImg().getImage(), go.getxLoc(), go.getyLoc(), 50, 50, null);
System.out.println("Graphics method called");
}
}
.
package game.try5;
import java.awt.Dimension;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Window {
public Window() {
JFrame frame = new JFrame("Epic Game");
frame.setSize(800,600);
frame.setLocationRelativeTo(null);
frame.setLayout(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GamePanel panel = new GamePanel();
frame.add(panel);
frame.setVisible(true);
}
public static void main(String[] args){
Window window = new Window();
}
}
Comment out frame.setLayout(null); and you'll most likely see the game.
The default layout of a JFrame is currently a BorderLayout
An object added to a BorderLayout without a constraint defaults to the CENTER.
An object in the CENTER of a BorderLayout is stretched to the available with and height.
Since the code calls frame.setSize(800,600); that available width and height is 800x600 pixels less the frame decorations.
A better all round approach is to:
Have the custom painted component override getPreferredSize() to return a sensible value.
Add the component to a container.
Lastly, pack the top level window. This will make the frame the smallest size it needs to be in order to display the content.

Categories

Resources