No error shown but JButton and Label can't be seen - java

I'm new to coding and I am facing this problem where it doesn't show the JButton and JLabel that i added in the GUI. What have i done wrong and how do i fix it?
import java.awt.ComponentOrientation;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
public class MainMenu {
public static void main (String []args) {
JFrame frame = new JFrame ("Main Menu");
frame.setSize(480,720);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3,2,5,5));
JButton meals = new JButton ("Meals");
JLabel label = new JLabel ("Welcome back!");
panel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
panel.add(meals);
panel.add(label);
frame.add(panel);
}
}

That happens because you frame.setVisible(true); before adding any components to it. You should add the components to the frame first, and then, use the setVisible method.
panel.add(meals);
panel.add(label);
frame.add(panel);
frame.setVisible(true); //visible after components added

Fixed version of your code below. Hopefully it works. I tested it in my IDE.
import java.awt.ComponentOrientation;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
public class MainMenu {
public static void main(String[] args) {
JFrame frame = new JFrame("Main Menu");
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3, 2, 5, 5));
JButton meals = new JButton("Meals");
JLabel label = new JLabel("Welcome back!");
panel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
panel.add(meals);
panel.add(label);
frame.add(panel);
frame.setSize(480, 720);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

Related

Java Swing Alignment problem with BoxLayout

I have three components inside a top to down boxLayout (JLabel, JTextField, JButton). The problem is that when i set the X alignment for the label it looks as if i would've changed the X alignment of the button and vice versa, only when both have the same alignment it works fine.
When the screen gets wider both components take a weird alignment.
when both components have the same alignment everything works fine.
here is my code:
public void create(){
JPanel panel = new JPanel();
BoxLayout boxLayout = new BoxLayout(panel, BoxLayout.Y_AXIS);
panel.setLayout(boxLayout);
panel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
JLabel etiqueta = new JLabel("Numero de consultorio: ");
etiqueta.setBackground(Color.BLUE);
etiqueta.setOpaque(true);
etiqueta.setAlignmentX(Component.LEFT_ALIGNMENT);
panel.add(etiqueta);
JTextField consultorio = new JTextField();
panel.add(consultorio);
JButton registrar = new JButton("Registrar");
registrar.setAlignmentX(Component.LEFT_ALIGNMENT);
panel.add(registrar);
this.getContentPane().add(panel, BorderLayout.CENTER);
}
Here is the proposed by Andrew Thompson solution:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
public class TestFrame {
public static void main(String[] args) {
SwingUtilities.invokeLater(new TestFrame()::create);
}
private void create() {
JPanel panel = new JPanel();
BoxLayout boxLayout = new BoxLayout(panel, BoxLayout.Y_AXIS);
panel.setLayout(boxLayout);
panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
JLabel etiqueta = new JLabel("Numero de consultorio: ");
etiqueta.setBackground(Color.BLUE);
etiqueta.setOpaque(true);
JPanel layout = new JPanel(new FlowLayout(FlowLayout.LEADING, 0, 0));
layout.add(etiqueta);
panel.add(layout);
JTextField consultorio = new JTextField();
panel.add(consultorio);
JButton registrar = new JButton("Registrar");
layout = new JPanel(new FlowLayout(FlowLayout.TRAILING, 0, 0));
layout.add(registrar);
panel.add(layout);
JFrame frm = new JFrame("Test");
frm.getContentPane().add(panel, BorderLayout.CENTER);
frm.pack();
frm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frm.setLocationRelativeTo(null);
frm.setVisible(true);
}
}

cannot setVerticalTextPosition for JLabel?

I have an icon with text for JLabel, I am trying to get the text to vertically positioned at the bottom, but this can't work, this is my whole class:
import java.awt.FlowLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
public class Test {
public static void main(String[] args) {
JFrame frame = new JFrame("my frame");
frame.setLayout(new FlowLayout());
JLabel label = new JLabel("my label");
ImageIcon mouse = new ImageIcon("mouse.jpg");
label.setIcon(mouse);
label.setVerticalTextPosition(SwingConstants.BOTTOM);
frame.add(label);
frame.setVisible(true);
frame.setSize(500, 500);
}
}
import java.awt.FlowLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
public class Test {
public static void main(String[] args) {
JFrame frame = new JFrame("my frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
JLabel label = new JLabel("my label");
ImageIcon mouse = new ImageIcon("mouse.jpeg");
label.setIcon(mouse);
label.setHorizontalTextPosition(JLabel.CENTER);
label.setVerticalTextPosition(JLabel.BOTTOM);
frame.add(label);
frame.setVisible(true);
frame.setSize(500, 500);
}
}
Reference:
LabelTextPosition

Unable to add button to frame GUI

I am trying to add a simple JButton to a JPanel in my program. The problem is when I run the problem, I do not see any button at all.
This is my code:
import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class GuiStopwatch {
public GuiStopwatch() {
JPanel panel = new JPanel();
JButton Startbtn = new JButton("START");
panel.add(Startbtn);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Stopwatch");
frame.setSize(600, 600);
frame.setLayout(new FlowLayout());
frame.setVisible(true);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
}
}
May I know what did I do wrong and how do I fix this?
You are not adding the panel to the frame at any point.
EDIT
Here is the code you would need if you wanted it in a separate method:
import javax.swing.*;
import java.awt.*;
public class GuiStopwatch {
private static void stopwatch(JFrame frame) {
JPanel panel = new JPanel();
JButton Startbtn = new JButton("START");
panel.add(Startbtn);
frame.add(panel);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Stopwatch");
stopwatch(frame);
frame.setSize(600, 600);
frame.setLayout(new FlowLayout());
frame.setVisible(true);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
}
}
You could swap things, create everything you need for your frame on the constructor, what makes the code more organized and also you can use it in other Classes, putting on main method will limit what you can do and makes the code not organized
See here an example:
public GuiStopwatch() {
setTitle("Stopwatch");
setSize(600, 600);
// Create JButton and JPanel
JButton button = new JButton("START");
JPanel panel = new JPanel();
panel.add(button);
this.getContentPane().add(panel);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
GuiStopwatch guistopwatch = new GuiStopwatch();
}

I can't move the buttons to the bottom - JFrame

I tried using GridLayout and BorderLayout but could not make my Buttons go below the mouth of the dog (picture). After making the button below the dog mouth, how do I add function to the buttons? For Settings, how do I make it open another window after I clicked on it and I can close the settings window instead of the whole thing. Thank you!
Here is my code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class Codegi extends JFrame {
public Codegi() {
JFrame frame = new JFrame();
try {
frame.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("enter image description here[1]")))));
} catch (IOException e) {
e.printStackTrace();
}
frame.pack();
frame.setLayout(new FlowLayout());
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
p1.setOpaque(false);
p2.setOpaque(false);
p1.setLayout(new BorderLayout(0, 20));
//JButton b1 = new JButton("START");
//JButton b2 = new JButton("SETTINGS");
p2.add(p1);
p1.add(new JButton("SETTINGS"), BorderLayout.SOUTH);
p1.add(new JButton("START"), BorderLayout.CENTER);
//p1.add(b1);
//p1.add(b2);
frame.add(p2);
//design of button
//Font largefont = new Font("TimesRoman", Font.BOLD, 20);
//b1.setBackground(Color.ORANGE);
//b2.setBackground(Color.ORANGE);
//b1.setForeground(Color.BLACK);
//b2.setForeground(Color.BLACK);
//b1.setFont(largefont);
//b2.setFont(largefont);
frame.setTitle("Codegi:Programming made fun");
frame.setSize(498, 687);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(467, 627);
}
public static void main(String args[]) {
new Codegi();
}
}
Screenshot
Set the layout to null
view.setLayout(null);
and then set the position of buttons like below
btn.setBounds(starting x-coordinate,starting y-coordinate,btn width,btn height);
remove panels by setVisible(false);
and show new ones with setVisible(true); on btn click event

Java ScrollPane/JPanel

I have a JPanel with multiple Object(custom class extends Jpanel) objects in it. The JPanel has a grid layout with 7 rows and 1 column. I'm trying to add a JPanel with 7 object in it to another JScrollPane so I can scroll to view all of the objects, but it's doing strange things. The scroll bar doesn't show up no matter how many objects are in the JPanel. Any ideas? Thanks in advance.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneLayout;
public class Main {
#SuppressWarnings("deprecation")
public static void main(String[] args) {
JFrame frame = new JFrame("Test");
frame.setLayout(new BorderLayout());
JLabel title = new JLabel("Game", JLabel.CENTER);
title.setPreferredSize(new Dimension(60,60));
title.setBorder(BorderFactory.createLineBorder(Color.black,5));
frame.add(title,BorderLayout.NORTH);
frame.setSize(850,480);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Grid g = new Grid();
JPanel jp = new JPanel();
jp.setLayout(new GridLayout(1,3));
jp.add(g);
JPanel test = new JPanel();
test.setLayout(new GridLayout(7,1));
test.add(p1);
test.add(p2);
test.add(p3);
test.add(p4);
test.add(p5);
test.add(p6);
test.add(p7);
JScrollPane jsp = new JScrollPane(test);
jsp.setViewportView(test);
jsp.getVerticalScrollBar().setUnitIncrement(50);
jsp.setCorn
jsp.setVerticalScrollBarPolicy(22);
jp.add(jsp,BorderLayout.EAST);
frame.add(jp);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
The jp JPanel uses JPanel's default FlowLayout and this may prevent you from resizing your JScrollPane and seeing that it actually is working properly. Why not either add the JScrollPane to the JFrame's contentPane or make jp use a BorderLayout? Also you don't need to set the JScrollPane's viewportView as you're already doing this by passing "test" into its constructor.
Your code don't compile. Please look at the following code. It can scroll vertically and horizontally.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
class MyPanel extends JPanel {
MyPanel(Color c) {
setBackground(c);
}
}
public class Test {
public static void main(String[] args) {
JPanel panel = new JPanel();
// the size of this panel is larger than the frame
panel.setPreferredSize(new Dimension(500, 2000));
panel.setLayout(new GridLayout(7, 1));
// add 7 sub panels
panel.add(new MyPanel(Color.magenta));
panel.add(new MyPanel(Color.cyan));
panel.add(new MyPanel(Color.blue));
panel.add(new MyPanel(Color.green));
panel.add(new MyPanel(Color.yellow));
panel.add(new MyPanel(Color.orange));
panel.add(new MyPanel(Color.red));
JScrollPane scroll = new JScrollPane(panel);
scroll.setViewportView(panel);
scroll.getVerticalScrollBar().setUnitIncrement(50);
JFrame frame = new JFrame("Test");
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(scroll);
frame.setVisible(true);
}
}

Categories