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
Related
Image of white bar
import java.awt.FlowLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class DisplayImage {
public static void main(String avg[]) throws IOException
{
DisplayImage abc=new DisplayImage();
}
public DisplayImage() throws IOException
{
BufferedImage img=ImageIO.read(new File("SP.jpg"));
ImageIcon icon=new ImageIcon(img);
JFrame frame=new JFrame();
frame.setIconImage (new ImageIcon("SP.jpg").getImage());
frame.setResizable(false);
frame.setTitle("Demo");
frame.setLayout(new FlowLayout());
frame.setSize(1920, 1080);
JLabel lbl=new JLabel();
lbl.setIcon(icon);
frame.add(lbl);
frame.setUndecorated(true);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
I thought this would produce a full screen of the Jpanel image and no white bar. I used the undecorated Jpanel function to try to make this happen but the white line appeared.
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);
}
}
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 expect JPanel's setMinimumSize() to confine the resizing of JFrame too, but it doesn't.
The following is my example code:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
public class AutoResize{
public static void main(String[] args) {
JPanel leftPanel = new JPanel();
JPanel rightPanel = new JPanel();
leftPanel.setBackground(Color.RED);
rightPanel.setBackground(Color.BLUE);
leftPanel.setSize(500,400);
rightPanel.setSize(500,400);
Dimension d = new Dimension(450,300);
leftPanel.setMinimumSize(d);
rightPanel.setMinimumSize(d);
JSplitPane split;
split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, rightPanel);
split.setDividerLocation(400);
JFrame frame = new JFrame();
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(split, BorderLayout.CENTER);
frame.setSize(1000, 800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
If you resize JFrame into a very small size it produces this:
What I want is that JFrame couldn't be resized into a smaller region than JPanels' minimum size. Is there anyway to implement this?
All I need is adding frame.setMinimumSize(); , I feel dumb.
Thanks #Andrew Thompson #MadProgrammer and #user1803551
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
public class AutoResize{
private final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
private final double w = screenSize.getWidth();
private final double h = screenSize.getHeight();
private final Dimension d = new Dimension((int) w/3,(int) h/3);
public void run() {
JPanel leftPanel = new JPanel();
JPanel rightPanel = new JPanel();
leftPanel.setBackground(Color.RED);
rightPanel.setBackground(Color.BLUE);
//leftPanel.setSize(500,400);
//rightPanel.setSize(500,400);
leftPanel.setMinimumSize(d);
rightPanel.setMinimumSize(d);
JSplitPane split;
split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, rightPanel);
split.setDividerLocation(400);
JFrame frame = new JFrame();
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(split, BorderLayout.CENTER);
frame.setSize((int) w,(int) h);
//frame.pack();
frame.setMinimumSize(new Dimension((int) w/3*2,(int) h/3*2));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
AutoResize test = new AutoResize();
test.run();
}
}
Hi, I am making a program that has a JSplitPane and its background is transparent so I add two panels with a JScrollPane on the JSplitPane.. when I add the contents in the panel it doesn't appear.
This is the code that I was using:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JViewport;
import javax.swing.UIManager;
public class sample {
JFrame mainFrame;
JPanel mainPanel;
JPanel splitPaneLeftPanel;
JPanel splitPaneRightPanel;
JSplitPane splitPane;
JScrollPane rightPanelScroll;
JScrollPane leftPanelScroll;
JViewport viewport;
sample(){
Frame();
SplitPaneAndContent();
mainFrame.setVisible(true);
}
public void Frame(){
mainFrame = new JFrame();
mainFrame.setTitle("Sample");
mainFrame.setLayout(new BorderLayout());
mainFrame.setSize(1024,720);
mainFrame.setMaximumSize(new Dimension(1366,768));
mainFrame.setMinimumSize(new Dimension(800,600));
mainFrame.setResizable(true);
mainFrame.setLocationRelativeTo(null);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void SplitPaneAndContent(){
mainPanel = new JPanel(new BorderLayout());
splitPaneLeftPanel = new JPanel(new BorderLayout());
splitPaneRightPanel = new JPanel(new BorderLayout());
mainPanel.setBackground(Color.blue);
JScrollPane rightPanelScroll = new JScrollPane(splitPaneRightPanel);
JScrollPane LeftPanelScroll = new JScrollPane(splitPaneLeftPanel);
viewport = new JViewport();
rightPanelScroll.setViewport(viewport);
rightPanelScroll.getViewport().setOpaque(false);
JLabel Text = new JLabel("SAMPLE");
Text.setBackground(Color.red);
splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,LeftPanelScroll,rightPanelScroll);
splitPane.setDividerLocation(300);
splitPaneLeftPanel.setOpaque(false);
splitPaneRightPanel.setOpaque(false);
rightPanelScroll.setOpaque(false);
splitPane.setOpaque(false);
splitPaneRightPanel.add(Text, BorderLayout.CENTER);
mainPanel.add(splitPane, BorderLayout.CENTER);
mainFrame.add(mainPanel, BorderLayout.CENTER);
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
new sample();
}
catch (Exception e) {
System.out.println(e);
}
}
}