I'm just wondering why this 100x100 px .gif image isn't showing up on the screen. The image is in the same directory, so the program should have no problem finding it. Does anybody know how to solve this problem?
import java.awt.*;
import java.awt.image.ImageObserver;
import java.io.File;
import javax.imageio.*;
import javax.swing.*;
public class Window extends JFrame{
//the pictures
ImageIcon guy = new ImageIcon("tester.gif");
JLabel pn = new JLabel(guy);
JPanel panel = new JPanel();
Window(){
super("Photuris Lucicrescens");
//Important
setSize(700,600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(panel);
setVisible(true);
//Decoration
Image customIcon = Toolkit.getDefaultToolkit().getImage("iconImage.gif");
setIconImage(customIcon);
//Adding the image
add(pn);
}
}
The problem is that you add two components to the JFrame. When you add a Component to a JFrame, it actually adds it to its content pane. By default, the content pane uses the BorderLayout as its LayoutManager. If you don't set a constraint, the component is considered to be in the center. Therefore, here you have two components that are in the center and receives the same bounds from the LayoutManager, resulting in only one component to be shown, the other being hidden. This is why you see the JPanel and not the JLabel.
If you want to see the JLabel, then don't add that panel to the frame.
Other remarks:
setVisible() should be invoked after you have created your component hierarchy.
I try it on my computer and image is showing up on icon. If you want show the image on background try this :
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.*;
public class Caine extends JFrame{
//the pictures
ImageIcon guy = new ImageIcon("tester.gif");
JLabel pn = new JLabel(guy);
JPanel panel = new JPanel();
Caine(){
super("Photuris Lucicrescens");
//Important
setSize(700,600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(panel);
setVisible(true);
JLabel im = new JLabel(new ImageIcon("iconImage.gif"));
setIconImage(customIcon);
panel.add(im);
add(pn);
}
}
Related
private void setupGUI(){
// Setup Frame
f = new JFrame("Shape Image Generator");
f.setBounds(500, 150, 450, 350);
f.setLayout(new GridLayout(8,1));
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
}
I create the frame above, then 8 panels. I create various components and add them to the panels and everything works fine. Until I created an ImageIcon and added it to a label and added that label to the 8th panel. The image used is 140x129 pixels. The problem is, only the top.... maybe 1/4 of the image is showing. If I change the frames dimensions in the code, more empty space is created between each panel, but only a slight bit more of the image is shown, so the image is still off of the screen. I'd say the window is easily adding 10 pixels of spacing for every 1 more pixel of the image it shows. If I drag the corners of the window to expand it, the same thing happens. If the window is maximized I still can only see a little over half of my now very stretched image.
Things I tried:
None of my components have preferred dimensions set, but I tried setting a preferred dimension for the label then panel that contains the ImageIcon and it only added the difference between the image and preferred size in gray space above the image, pushing it further offscreen. So, I undid that.
Adding the label containing the ImageIcon to a different panel which was not the 8th and last panel, in this case, the image is still cut off, but at the point that it gets cut off, the components on the panel underneath it appear (over top of the background coloring which cuts off the image).
Exhaustively Googling this situation with about 30 different ways of phrasing it and not finding a solution.
(row1 - row8 are JPanels, I didn't include the coding for them)
ImageIcon iconStart = createImageIcon("/images/ShapeClipart.png", "Shapes");
JLabel imgLabel = new JLabel();
row8.add(imgLabel);
// Add image to image label
imgLabel.setIcon(iconStart);
// Add panels to frame
f.add(row1);
f.add(row2);
f.add(row3);
f.add(row4);
f.add(row5);
f.add(row6);
f.add(row7);
f.add(row8);
f.setVisible(true);
Window at execution
Window when stretched
edit:
adding f.pack() makes a very tall skinny window (the windows height taller than my screen) but it still looks like when I manually expand the window (empty space between panels, image partially offscreen), even if I take out f.setBounds and only use f.setLocation.
You are using a GridLayout. This gives all of the enclosed panels the same amount of space. In this case it is a vertical grid.
You should probably use something a bit different. I might try a BorderLayout in the JFrame and put the a panel containing the top seven panels (in a GridLayout) into the CENTER, and then put the JLabel into the SOUTH portion of the JFrame.
There are other ways to lay it out, but this is the first I could think of.
GridLayout makes each cell in the grid the same size and the size of each cell is determined by the largest Component contained in the grid.
In your code, the icon is the largest component and you also have only one column in your grid so every row has the same height as your icon.
Since you also limit the size of your JFrame by calling method setBounds(), the Swing infrastructure cuts off the icon so that all the components fit into the bounds you specified.
One alternative, but not the only one, is to use BoxLayout since it uses the preferred size of each of its contained components.
Here is a sample GUI that matches the screen capture that you posted and uses BoxLayout.
import static javax.swing.WindowConstants.EXIT_ON_CLOSE;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.net.URL;
import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class Shapes23 implements Runnable {
private JFrame frame;
#Override // java.lang.Runnable
public void run() {
showGui();
}
private JPanel createEighthRow() {
JPanel eighthRow = new JPanel();
URL url = getClass().getResource("paint-bursht.jpg");
Icon ico = new ImageIcon(url);
JLabel label = new JLabel(ico);
eighthRow.add(label);
return eighthRow;
}
private JPanel createFifthRow() {
JPanel fifthRow = new JPanel();
JTextField textField = new JTextField(20);
fifthRow.add(textField);
return fifthRow;
}
private JPanel createFirstRow() {
JPanel firstRow = new JPanel();
JLabel label = new JLabel("2D Shapes");
firstRow.add(label);
return firstRow;
}
private JPanel createFourthRow() {
JPanel fourthRow = new JPanel();
fourthRow.add(createRadioButton("Sphere"));
fourthRow.add(createRadioButton("Cube"));
fourthRow.add(createRadioButton("Cone"));
fourthRow.add(createRadioButton("Cylinder"));
fourthRow.add(createRadioButton("Torus"));
return fourthRow;
}
private JPanel createMainPanel() {
JPanel mainPanel = new JPanel();
BoxLayout layout = new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS);
mainPanel.setLayout(layout);
mainPanel.add(createFirstRow());
mainPanel.add(createSecondRow());
mainPanel.add(createThirdRow());
mainPanel.add(createFourthRow());
mainPanel.add(createFifthRow());
mainPanel.add(createSixthRow());
mainPanel.add(createSeventhRow());
mainPanel.add(createEighthRow());
return mainPanel;
}
private JRadioButton createRadioButton(String text) {
JRadioButton radioButton = new JRadioButton(text);
return radioButton;
}
private JPanel createSecondRow() {
JPanel secondRow = new JPanel();
secondRow.add(createRadioButton("Circle"));
secondRow.add(createRadioButton("Rectangle"));
secondRow.add(createRadioButton("Square"));
secondRow.add(createRadioButton("Triangle"));
return secondRow;
}
private JPanel createSeventhRow() {
JPanel seventhRow = new JPanel();
JButton button = new JButton("Enter");
seventhRow.add(button);
return seventhRow;
}
private JPanel createSixthRow() {
JPanel sixthRow = new JPanel();
JTextField textField = new JTextField(20);
sixthRow.add(textField);
return sixthRow;
}
private JPanel createThirdRow() {
JPanel thirdRow = new JPanel();
JLabel label = new JLabel("3D Shapes");
thirdRow.add(label);
return thirdRow;
}
private void showGui() {
frame = new JFrame("Shape Image Generator");
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.add(createMainPanel(), BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Shapes23());
}
}
Here is a screen capture of how it looks. Note that I couldn't find the same icon as in your screen capture so I just used a different one.
My first panel's layout is BorderLayout and my second panel's layout is GridBagLayout. I don't know how to show them both at the same time.
I already tried adding two panels to on another panel.
Adding both to another panel is the way to go! But you have to make the right choice of LayoutManager for this "parent" panel. Let me give you an example:
The JFrame's content pane (where you add all your Components to) can be setup with a LayoutManager of your choice. See this runnable example, which creates two JPanels of 100x100 pixels in different colors. The panels are using the LayoutManagers you mentioned, but the main content pane of the JFrame is set to a BoxLayout (horizontal, but you can also set it to vertical!).
You can do this to any other panel, too. A panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS)); is enough. The below example just uses the content pane, but you can adapt it to your needs:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TwoPanels extends JFrame {
private static final long serialVersionUID = 1L;
private static final Dimension DEFAULT_DIMENSION = new Dimension(100, 100);
public static void main(String[] args) {
new TwoPanels();
}
public TwoPanels() {
//create panel 1
JPanel panel1 = new JPanel(new BorderLayout());
panel1.setPreferredSize(DEFAULT_DIMENSION);
panel1.setBackground(Color.RED);
//create panel 2
JPanel panel2 = new JPanel(new GridBagLayout());
panel2.setPreferredSize(DEFAULT_DIMENSION);
panel2.setBackground(Color.GREEN);
//set content pane layout
setLayout(new BoxLayout(this.getContentPane(), BoxLayout.X_AXIS));
//add to content pane
add(panel1);
add(panel2);
//setup and display window
pack();
setVisible(true);
}
}
It looks like this:
EDIT: It's a little unclear from your question that you actually want to stack overlaying panels. You might find what you need here: https://docs.oracle.com/javase/tutorial/uiswing/components/layeredpane.html
I'm having trouble getting my custom panel to show up using JLayeredPanel. I'm currently trying to create a black rectangle on the first layer, and on the second layer, I have a custom JPanel which is a slideshow of images. The slideshow will not display at all, it only works when I add it to the frame. Any thoughts?
Code:
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
public class GUI extends JFrame {
private Container pane;
private JPanel emptySlideShow;
private JLayeredPane layeredPane;
public GUI(){
this.setVisible(true);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
this.setBounds(0,0,screenSize.width, screenSize.height);
// //Set up the content pane
// pane = this.getContentPane(); //get the content pane to place components
// pane.setLayout(null); //use absolute positioning (using Insets)
// pane.setBackground(new Color(236, 236, 236)); //color the background
//
// //Set up the main menu bar
this.emptySlideShow = new JPanel();
this.emptySlideShow.setBounds(0,0,(int)Toolkit.getDefaultToolkit().getScreenSize().getWidth(),250);
this.emptySlideShow.setBackground(new Color(0,0,0));
layeredPane = new JLayeredPane();
layeredPane.setLayout(null);
layeredPane.add(this.emptySlideShow,new Integer(0));
ArrayList<Image> slides = new ArrayList<Image>();
Image image1 = new ImageIcon("pictures/slide1.png").getImage();
slides.add(image1);
Image image2 = new ImageIcon("pictures/slide2.png").getImage();
slides.add(image2);
ArrayList<String> transitions = new ArrayList<String>();
transitions.add("PanLR");
transitions.add("FadeOut");
this.add(new SlideShow(slides,transitions));
layeredPane.add(new SlideShow(slides,transitions),new Integer(1));
this.add(layeredPane);
}
}
A guess since we don't have all of the pertinent code -- but you need to specify a component's size and position when adding it to the JLayeredPane, and so perhaps you're not setting the SlideShow's size before adding it (it's position will by default be 0, 0).
Question though: Why are you trying to add a SlideShow JPanel to the GUI twice? Once to the JLayeredPane, and the other to the JFrame itself?
Simple. You are adding the components (including JLayeredPane) to the frame AFTER you invoke
this.setVisible(true);
Move this line to the end of the constructor and you should be fine or if you really want to keep this line at the top, you should call revalidate() to update the JFrame and components.
Been having a hard time adding JPanels to JFrame. Am pretty much new on java, always used C++
I need to do 4 Panels inside one Frame.
Here is my Code, just started today..
package project2;
import javax.swing.JOptionPane;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import javax.swing.*;
import java.awt.Container;
import java.awt.Dimension;
public class GUI extends JFrame
{
private JPanel Checks; //Panel to Hold Checks
private JPanel Transactions;
private JPanel History;
private JPanel Graphics;
private JLabel CLabel;
public GUI()
{
super ( "UTB Check-In");
JPanel Checks = new JPanel(); //set up panel
CLabel = new JLabel("Label with text");
Checks.setBackground(Color.red);
Checks.setLayout( new BoxLayout(Checks,BoxLayout.LINE_AXIS));
add(Checks);
// JPanel Transactions = new JPanel();
// Transactions.setToolTipText("Electronic Transactions");
//Transactions.setBackground(Color.blue);
// add(Transactions);
}
}
I was trying to put Transaction and Checks one side from the other with different colors,in this case blue and red it doesnt stay in the middle it those one or the other.
One of my Colleagues told me that the BoxLayout(or any layout) needed to be implemented with the size..something to that extend. am not really sure I been reading
http://docs.oracle.com/javase/tutorial/uiswing/layout/box.html
But I still do not get it completely. If somebody can help me out
thanks!
Your code fail cause you are adding directly to the JFrame which have by default BorderLayout. You are setting BoxLayout to the wrong panel.
You have to setLayout() to the top component(jframe) that you are adding or as i prefer adding to a jpanel rather than directly to the jframe to acomplish what you want to do.
Example:
public GUI()
{
super ( "UTB Check-In");
JPanel parent = new JPanel();
parent.setLayout(new BoxLayout(parent,BoxLayout.LINE_AXIS));
add(parent);
JPanel Checks = new JPanel(); //set up panel
CLabel = new JLabel("Label with text");
Checks.setBackground(Color.red);
parent.add(Checks);
JPanel Transactions = new JPanel();
Transactions.setToolTipText("Electronic Transactions");
Transactions.setBackground(Color.blue);
parent.add(Transactions);
}
By the way, in Java variables starts with lowerCase as a code convention.
I made a quick research to solve this problem but until now I found nothing regarding to this. I have one image into one TabbedPane object but when I try to align this image on the center of the label inside the TabbedPane it "Doesn't" work. The center alignment in this case works only for horizontal view but I want to be in the center of both vertical and horizontal. Check out the sample below:
import java.awt.BorderLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import com.sh.st.gui.MainScreen;
public class test {
JTabbedPane tabbedPane = new JTabbedPane();
JFrame mainFrame = new JFrame();
public static void main (String[] args){
test t = new test();
}
public test(){
JPanel entrance = new JPanel();
JLabel lbImage1;
JMenuBar bar;
JMenu file, registerQuery;
ImageIcon Logo= new ImageIcon("rsc/img/imagem.jpg");
lbImage1= new JLabel(Logo, JLabel.CENTER);
entrance.add(lbImage1);
tabbedPane.addTab("Entrance", null, entrance);
mainFrame.getContentPane().add( tabbedPane, BorderLayout.CENTER);
bar= new JMenuBar();
file= new JMenu("File");
registerQuery= new JMenu("Request");
mainFrame.setVisible(true);
}
}
I guess its not so hard to do what I want but until now as I said, I found nothing, anyone could help please? thanks in advance
The JLabel alignment will only center horizontally due to the potitioning characteristics of its parent container. In this case it is the default layout for JPanel which is FlowLayout. This layout manager does not facilitate easy vertical alignment.
Provided that the JLabel lbImage1 will be the only component on the JPanel entrance, then GridLayout can be used to center the JLabel both horizontally and vertically:
entrance.setLayout(new GridLayout());