JButton takes entire frame - java

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.

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 can i make a layout like the attached gif?

My question is about layout in Java Swing.
I want to make a screen like shown below. I saw this video on youtube and made a gif of the part I want.
I want 2 panels and a button like this:
When i clicked the button the JPanel will be hidden and JTable's width will be 100% like html/css like this; (And when button clicked again JPanel will be shown etc..)
How can I do this? Which layout should I use?
There is more than one way to do it, but here's an example that uses BorderLayout as the main layout, and places the button in a left aligning FlowLayout:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
public class LayoutDemo {
private LayoutDemo() {
JFrame frame = new JFrame("Demo");
frame.setLocationByPlatform(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel buttonHolder = new JPanel(new FlowLayout(FlowLayout.LEADING));
frame.add(buttonHolder, BorderLayout.NORTH);
JButton button = new JButton("Toggle visibility");
buttonHolder.add(button);
final JPanel left = new JPanel();
left.setPreferredSize(new Dimension(100, 200));
left.setBackground(Color.BLUE);
frame.add(left, BorderLayout.LINE_START);
JLabel table = new JLabel("This pretends to be a table", SwingConstants.CENTER);
table.setPreferredSize(new Dimension(400, 200));
frame.add(table);
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
left.setVisible(!left.isVisible());
}
});
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new LayoutDemo();
}
});
}
}
I used setPreferredSize() to give the components some reasonable default size, but usually it should be automatically calculated by the layout manager from the sizes of the child components, or in case of a custom component, you should override getPreferredSize() return what is appropriate for the component.
The result looks like:

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.

I'm trying to add a JPanel to a JFrame with Flowlayout, but continue to get an exception. What did I do wrong?

I am trying to add a JPanel to a JFrame with FlowLayout, but continue to get this exception: "Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: illegal component position". I would like to be able to implement a JPanel with a few buttons in the near future, so please let me know what I can do to allow that.
package textadv;
import java.awt.FlowLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Label;
import java.awt.event.KeyEvent;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class TextAdv {
private JFrame frame = new JFrame("Text Adventure");
private JPanel mainPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
private JButton b1 = new JButton("Left");
public TextAdv() {
mainPanel.setBackground(Color.RED);
int FRAME_WIDTH = 400;
int FRAME_HEIGHT = 400;
b1.setEnabled(true);
b1.setVisible(true);
b1.setPreferredSize(new Dimension(40, 40));
mainPanel.setVisible(true);
mainPanel.setPreferredSize(new Dimension (50, 50));
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setVisible(true);
mainPanel.add(b1, FlowLayout.CENTER);
frame.add(mainPanel);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
TextAdv fS = new TextAdv();
}
});
}
You cant specify FlowLayout options to the add method of Container. This resolves to the add() overload which accepts Component, and an int, index. Other layouts let you use the add(Component, Object) overload, like the GridBagLayout and its GridBagConstraints. For the FlowLayout you specify the options to the constructor of the layout only.
Replace this
mainPanel.add(b1, FlowLayout.CENTER);
with this
mainPanel.add(b1);
Consider revisiting the tutorial here: http://docs.oracle.com/javase/tutorial/uiswing/layout/flow.html
See Container here : http://docs.oracle.com/javase/7/docs/api/java/awt/Container.html#add%28java.awt.Component,%20int%29

Categories

Resources