JLabel not displayed - java

I have a JLabel with an ImageIcon that isn't showing.
public class Lockscreen extends JFrame {
JPanel layer;
JButton signin;
ImageIcon heartlock;
String heartlockpath;
JLabel heartlockdisplay;
String arrowpath;
public Lockscreen(){
super("Startseite");
setSize(700, 400);
setLocation(100, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
arrowpath = "C:\\Users\\saydanan\\Pictures\\FürMyDiary\\Right_Arrow.png";
signin = new JButton();
signin.setSize(19, 12);
signin.setIcon(new ImageIcon(arrowpath));
signin.setLocation(250, 500);
heartlockpath = "C:\\Users\\saydananPicturesFürMyDiary\\082326-green-jelly-icon-business-lock-heart.png";
heartlock = new ImageIcon(heartlockpath);
heartlockdisplay = new JLabel();
heartlockdisplay.setIcon(heartlock);
heartlockdisplay.setLocation(250, 250);
heartlockdisplay.setSize(heartlock.getIconWidth(), heartlock.getIconHeight());
heartlockdisplay.setVisible(true);
layer = new JPanel();
layer.setBackground(Color.black);
/*layer.setLayout(...);*/
addEverything(layer);
getContentPane().add(layer);
setVisible(true);
}
public void addEverything(JPanel panel){
panel.add(signin);
panel.add(heartlockdisplay);
panel.repaint();
}
}

You need to add it, and then set it to be visible.
As in:
panel.add(heartlockdisplay);
heartlockdisplay.setVisible(true);
panel.setVisible(true);

Related

Adding a JLabel under a JPanel not working (JAVA)

I'm designing a long and signup page as a test for some java projects and whilst trying to append the JLabel under the existing panel, the text does not show up.
Here is the code:
//Setting Panel Color
int r1 = 172;
int g1 = 50;
int b1 = 50;
Color myFgColor = new Color(r1,g1,b1);
JPanel panel = new JPanel();
panel.setBounds(750,60,375,420);
panel.setBackground(myFgColor);
//Login and Sign Up Text
JLabel label = new JLabel("LOGIN");
label.setFont(new Font("Serif", Font.PLAIN, 14));
label.setForeground(Color.white);
panel.add(label);
gui.getContentPane().add(panel);
Is this what you are trying to see?
window
I didn't change anything, I just created a new JFrame and put your components...
The difference I see is that JFrame creates the contentPane, and you didn't:
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Window frame = new Window();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Window() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(1200, 575);
setResizable(false);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
//Setting Panel Color
Color myFgColor = new Color(172,50,50);
JPanel panel = new JPanel();
panel.setBounds(750,60,375,420);
panel.setBackground(myFgColor);
//Login and Sign Up Text
JLabel label = new JLabel("LOGIN");
label.setFont(new Font("Serif", Font.PLAIN, 14));
label.setForeground(Color.white);
panel.add(label);
getContentPane().add(panel);
//Setting Background Color
Color myBgColor = new Color(30,30,30);
getContentPane().setBackground(myBgColor);
//Centering Window
setLocationRelativeTo(null);
}
I recommend you using WindowBuilder to create frames, it's a visual designer and it makes it very easy

paint method causing other components not to show up

I have a simple GUI program I'm trying to get to work. When the user presses the bottom button I'm trying to get some shapes to paint. When I get rid of the if(buttonClicked) in paint() everything shows up fine but paint() seems to be auto-executed and the shapes appear without any button click. When I add surround the paint() body with if(buttonClicked), regardless of how the ButtonHandler class handles it, the rest of my components do not even show up in the frame. I have no idea why this is happening. Test the code with and without the if logic in paint() and you will see what's going on.
public class GUI extends JFrame {
//declare components
Container container;
JPanel centerPanel, northPanel, southPanel, eastPanel, westPanel, mouseClickPanel;
JLabel topLabel;
JTextArea textArea;
JButton buttonA, buttonB, drawButton;
boolean buttonClicked;
public GUI(String title) {
super(title); // invoke JFrame Constructor
container = getContentPane();
container.setLayout(new BorderLayout());
centerPanel = new JPanel();
centerPanel.setBackground(Color.CYAN);
northPanel = new JPanel();
northPanel.setBackground(Color.GREEN);
southPanel = new JPanel();
southPanel.setBackground(Color.MAGENTA);
eastPanel = new JPanel();
eastPanel.setBackground(Color.ORANGE);
westPanel = new JPanel();
westPanel.setBackground(Color.YELLOW);
mouseClickPanel = new JPanel();
mouseClickPanel.setBackground(Color.GRAY);
mouseClickPanel.setPreferredSize(new Dimension(400, 400));
topLabel = new JLabel("Press either button to make something happen");
topLabel.setFont(new Font("Calibri", Font.PLAIN, 16));
topLabel.setHorizontalAlignment(JLabel.CENTER);
textArea = new JTextArea(3, 20);
textArea.setEditable(false);
buttonA = new JButton("Press Here");
buttonB = new JButton("Press Here");
drawButton = new JButton("Press here");
container.add(centerPanel, BorderLayout.CENTER);
container.add(northPanel, BorderLayout.NORTH);
container.add(southPanel, BorderLayout.SOUTH);
container.add(eastPanel, BorderLayout.EAST);
container.add(westPanel, BorderLayout.WEST);
northPanel.add(topLabel, BorderLayout.NORTH);
centerPanel.add(buttonA);
centerPanel.add(textArea);
centerPanel.add(buttonB);
centerPanel.add(mouseClickPanel);
centerPanel.add(drawButton);
buttonClicked = false;
ButtonHandler buttonHandler = new ButtonHandler(buttonA, drawButton, textArea, buttonClicked);
buttonA.addActionListener(buttonHandler); // add actionListeners to buttonA and B
buttonB.addActionListener(buttonHandler);
drawButton.addActionListener(buttonHandler);
setSize(525, 600);
setVisible(true);
}
public void paint(Graphics g) {
if (buttonClicked) {
super.paint(g);
g.setColor(Color.RED);
g.fillOval(150, 150, 50, 50);
g.draw3DRect(200, 200, 50, 50, true);
}
}
}
Handler class:
public class ButtonHandler extends JFrame implements ActionListener {
private JButton buttonA, drawButton;
private JTextArea textArea;
boolean buttonClicked;
public ButtonHandler(JButton buttonA, JButton drawButton, JTextArea textArea, boolean buttonClicked) {
this.buttonA = buttonA;
this.textArea = textArea;
this.drawButton = drawButton;
this.buttonClicked = buttonClicked;
}
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == buttonA) {
textArea.setText(" <- You pressed left button");
}
else if (e.getSource() == drawButton) {
textArea.setText("You pressed button to draw rectangle");
buttonClicked = true;
repaint();
}
else {
textArea.setText("You pressed right button ->");
}
}
}
Take super.paint(g); out of the if statement. Have it as the first line. Otherwise, no painting at all (including the JPanel internals such as background) will happen unless the button is clicked.

Inserting image through event handling?

I am encountering problems with event handling in java.
I want to add the image1 if button 1 is pressed, image2 if button 2 is pressed, et cetera.
This is my code till now; Can anyone help? This code doesn't compile.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.ImageIcon;
public class MyPanel extends JPanel {
private JLabel jcomp1;
private JButton jcomp2;
private JButton jcomp3;
private JButton jcomp4;
private JButton jcomp5;
private JButton jcomp6;
private JButton jcomp7;
private JButton jcomp8;
private JButton jcomp9;
private ImageIcon image1;
private ImageIcon image2;
private ImageIcon image3;
private ImageIcon image4;
private ImageIcon image5;
private ImageIcon image6;
private ImageIcon image7;
private ImageIcon image8;
public MyPanel() {
//construct components
image1 = new ImageIcon(getClass().getResource("hang1.jpg"));
image2 = new ImageIcon(getClass().getResource("hang2.jpg"));
image3 = new ImageIcon(getClass().getResource("hang3.jpg"));
image4 = new ImageIcon(getClass().getResource("hang4.jpg"));
image5 = new ImageIcon(getClass().getResource("hang5.jpg"));
image6 = new ImageIcon(getClass().getResource("hang6.jpg"));
image7 = new ImageIcon(getClass().getResource("hang7.jpg"));
image8 = new ImageIcon(getClass().getResource("hang8.jpg"));
jcomp1 = new JLabel (image1);
jcomp2 = new JButton ("1");
jcomp3 = new JButton ("2");
jcomp4 = new JButton ("3");
jcomp5 = new JButton ("4");
jcomp6 = new JButton ("5");
jcomp7 = new JButton ("6");
jcomp8 = new JButton ("7");
jcomp9 = new JButton ("8");
//events
jcomp2.setActionCommand("1");
jcomp3.setActionCommand("2");
jcomp4.setActionCommand("3");
jcomp5.setActionCommand("4");
jcomp6.setActionCommand("5");
jcomp7.setActionCommand("6");
jcomp8.setActionCommand("7");
jcomp9.setActionCommand("8");
jcomp2.addActionListener(new ButtonClickListener());
jcomp3.addActionListener(new ButtonClickListener());
jcomp4.addActionListener(new ButtonClickListener());
jcomp5.addActionListener(new ButtonClickListener());
jcomp6.addActionListener(new ButtonClickListener());
jcomp7.addActionListener(new ButtonClickListener());
jcomp8.addActionListener(new ButtonClickListener());
jcomp9.addActionListener(new ButtonClickListener());
//adjust size and set layout
setPreferredSize(new Dimension(624, 537));
setLayout(null);
//add components
add(jcomp2);
add(jcomp3);
add(jcomp4);
add(jcomp5);
add(jcomp6);
add(jcomp7);
add(jcomp8);
add(jcomp9);
// set component bounds (only needed by Absolute Positioning)
jcomp1.setBounds(15, 10, 595, 350);
jcomp2.setBounds(35, 375, 100, 25);
jcomp3.setBounds(190, 375, 100, 25);
jcomp4.setBounds(320, 375, 100, 25);
jcomp5.setBounds(465, 375, 100, 25);
jcomp6.setBounds(35, 450, 100, 25);
jcomp7.setBounds(190, 450, 100, 25);
jcomp8.setBounds(320, 450, 100, 25);
jcomp9.setBounds(465, 450, 100, 25);
}
class ButtonClickListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals("1")) {
jcomp1.set(image1);
}
}
}
public static void main (String[] args) {
JFrame frame = new JFrame("MyPanel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new MyPanel());
frame.pack();
frame.setVisible (true);
}
}
I believe you are trying to get something like this:
public class MyPanel extends JPanel {
private JLabel label;
private JButton[] buttons = new JButton[8];
private ImageIcon[] images = new ImageIcon[8];
public MyPanel() {
JPanel buttonPanel = new JPanel(new GridLayout(2, 4, 15, 10));
for (int i = 0; i < images.length; i++) {
images[i] = new ImageIcon(getClass().getResource(i+1 + ".png"));
buttons[i] = new JButton(String.valueOf(i+1));
buttons[i].setActionCommand(String.valueOf(i+1));
buttons[i].addActionListener(new ButtonClickListener());
buttonPanel.add(buttons[i]);
}
label = new JLabel(images[0]);
setLayout(new BorderLayout());
add(label);
add(buttonPanel, BorderLayout.PAGE_END);
}
class ButtonClickListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
label.setIcon(images[Integer.parseInt(e.getActionCommand()) - 1]);
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("MyPanel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new MyPanel());
frame.pack();
frame.setVisible(true);
}
}
Notes:
Don't forget to change the image file name.
You can play with the layout manager to get what you want.
I removed setPreferredSize(new Dimension(624, 537)); because you didn't specify a resize behavior which would make this line meaningless. pack() would take care of the sizes for you.
Set the icon of the button or label. To do this, you need to create an ImageIcon, which has multiple constructors to create from a filename or a loaded image.
jcomp1.setIcon(new ImageIcon(...));

Jpanel is not appearing in JFrame

So I am trying to get a JFrame to display a JPanel that has 5 other JPanels in it. I dont have any syntax errors and all that displays is a very small screen. I have been at this all day and have yet to find a solution.
public class addressPanel extends JPanel {
private JTextField nameT;
private JTextField addressT;
private JTextField cityT;
private JTextField stateT;
private JTextField zipCodeT;
private JTextField phoneNumberT;
private JLabel Title;
private JLabel addressTitle;
private JLabel nameL;
private JLabel addressL;
private JLabel stateL;
private JLabel cityL;
private JLabel zipCodeL;
private JLabel phoneNumberL;
private JLabel orderType;
private JRadioButton takeOut;
private JRadioButton delivery;
private JButton clear;
private JButton submit;
private JPanel addressTextPanel;
private JPanel addressLabelPanel;
private JPanel orderTypePanel;
private JPanel titlePanel;
private JPanel buttonsPanel;
public JPanel addressTextPanel() {
nameT = new JTextField(1);
addressT = new JTextField(2);
cityT = new JTextField(3);
stateT = new JTextField(4);
zipCodeT = new JTextField(5);
phoneNumberT = new JTextField(6);
Font font = new Font(Font.SERIF, Font.PLAIN, 24);
nameT.setFont(font);
addressT.setFont(font);
cityT.setFont(font);
stateT.setFont(font);
zipCodeT.setFont(font);
phoneNumberT.setFont(font);
JPanel addressTextPanel = new JPanel();
addressTextPanel.setPreferredSize(new Dimension(125, 250));
addressTextPanel.setLayout(new BoxLayout(addressTextPanel, BoxLayout.Y_AXIS));
addressTextPanel.add(nameT);
addressTextPanel.add(addressT);
addressTextPanel.add(cityT);
addressTextPanel.add(stateT);
addressTextPanel.add(zipCodeT);
addressTextPanel.add(phoneNumberT);
return addressTextPanel;
}
public JPanel addressLabelPanel() {
nameL = new JLabel("Name:");
addressL = new JLabel("Address:");
cityL = new JLabel("City:");
zipCodeL = new JLabel("Zip Code:");
stateL = new JLabel("State:");
phoneNumberL = new JLabel("Phone Number:");
nameL.setFont(nameL.getFont().deriveFont(24.0f));
addressL.setFont(addressL.getFont().deriveFont(24.0f));
cityL.setFont(cityL.getFont().deriveFont(24.0f));
zipCodeL.setFont(zipCodeL.getFont().deriveFont(24.0f));
stateL.setFont(stateL.getFont().deriveFont(24.0f));
phoneNumberL.setFont(phoneNumberL.getFont().deriveFont(24.0f));
JPanel addressLabelPanel = new JPanel();
addressLabelPanel.setPreferredSize(new Dimension(125, 250));
addressLabelPanel.setLayout(new BoxLayout(addressLabelPanel, BoxLayout.Y_AXIS));
addressLabelPanel.add(nameL);
addressLabelPanel.add(addressL);
addressLabelPanel.add(cityL);
addressLabelPanel.add(stateL);
addressLabelPanel.add(zipCodeL);
addressLabelPanel.add(phoneNumberL);
return addressLabelPanel;
}
public JPanel orderTypePanel() {
orderType = new JLabel("Order Type:");
takeOut = new JRadioButton("Take Out");
delivery = new JRadioButton("Delivery");
orderType.setFont(takeOut.getFont().deriveFont(24.0f));
takeOut.setFont(takeOut.getFont().deriveFont(24.0f));
delivery.setFont(delivery.getFont().deriveFont(24.0f));
JPanel orderTypePanel = new JPanel();
orderTypePanel.setPreferredSize(new Dimension(250, 125));
orderTypePanel.setLayout(new BoxLayout(orderTypePanel, BoxLayout.Y_AXIS));
orderTypePanel.add(orderType);
orderTypePanel.add(takeOut);
orderTypePanel.add(delivery);
return orderTypePanel;
}
public JPanel titlePanel() {
Title = new JLabel("Pizza Order Form");
addressTitle = new JLabel("Address");
Title.setFont(Title.getFont().deriveFont(36.0f));
addressTitle.setFont(addressTitle.getFont().deriveFont(36.0f));
JPanel titlePanel = new JPanel();
titlePanel.setPreferredSize(new Dimension(500, 100));
titlePanel.setLayout(new BoxLayout(titlePanel, BoxLayout.Y_AXIS));
titlePanel.add(Title);
titlePanel.add(addressTitle);
return titlePanel;
}
public JPanel buttonsPanel() {
clear = new JButton("Clear");
submit = new JButton("Submit");
clear.setFont(clear.getFont().deriveFont(24.0f));
submit.setFont(submit.getFont().deriveFont(24.0f));
JPanel buttonsPanel = new JPanel();
buttonsPanel.setPreferredSize(new Dimension(500, 100));
buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.X_AXIS));
buttonsPanel.add(clear);
buttonsPanel.add(submit);
return buttonsPanel;
}
public addressPanel() {
JPanel addressParent = new JPanel(new BorderLayout());
addressParent.add(new titlePanel(), BorderLayout.NORTH);
addressParent.add(new orderTypePanel(), BorderLayout.WEST);
addressParent.add(new addressLabelPanel(), BorderLayout.CENTER);
addressParent.add(new addressTextPanel(), BorderLayout.EAST);
addressParent.add(new buttonsPanel(), BorderLayout.SOUTH);
}
public static void main(String[] args) {
// Create Main Panel
JFrame frame = new JFrame("");
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
frame.getContentPane().add(new addressPanel());
// Color background = new Color(238,233,191);
// frame.getContentPane().setBackground(background);
frame.pack();
frame.setVisible(true);
}
}
Read your code. The program creates a JFrame. It creates an instance of addressPanel (which should be named AddressPanel). ANd it adds this addressPanel instance to the frame conent pane.
Now what is added to the addressPanel? Nothing:
public addressPanel()
{
JPanel addressParent = new JPanel(new BorderLayout());
addressParent.add (new titlePanel(), BorderLayout.NORTH);
addressParent.add (new orderTypePanel(), BorderLayout.WEST);
addressParent.add (new addressLabelPanel(), BorderLayout.CENTER);
addressParent.add (new addressTextPanel(), BorderLayout.EAST);
addressParent.add (new buttonsPanel(), BorderLayout.SOUTH);
}
The constructor of addressPanel creates another panel (addressParent), adds plenty of things to this addressParent panel, but doesn't add anything to this, the addressPanel. So the addressPanel is empty.
Please respect the Java naming conventions to make your code readable. Classes start with an uppercase letter.

I want 2 JLabels to show on the same JFrame in Java

I can't get the JLabel pieces to both show up on the same JFrame. I've tried resizeing; but it didn't work. Do I need to place the JLabel's in a JPanel? I want both JLabels' to show on the same JFrame. Any help is appreciated. Thanks.
public class HelloWorldFrame extends JFrame
{
private TitledBorder title;
private TitledBorder title2;
public HelloWorldFrame()
{
super("Hello World! ");
JFrame helloWorld = new JFrame();
JLabel label = new JLabel();
title = BorderFactory.createTitledBorder("Language");
title.setTitleJustification(TitledBorder.LEFT);
label.setBorder(title);
add(label);
setSize(300, 200);
JRadioButton button1 = new JRadioButton("English");
JRadioButton button2 = new JRadioButton("French");
JRadioButton button3 = new JRadioButton("Spanish");
label.setLayout(new FlowLayout());
label.add(button1);
label.add(button2);
label.add(button3);
JLabel label2 = new JLabel();
title2 = BorderFactory.createTitledBorder("Greeting");
title2.setTitleJustification(TitledBorder.LEFT);
label2.setBorder(title2);
label2.setSize(100, 100);
add(label2);
helloWorld.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
//The main begins below
import javax.swing.JFrame;
public class HelloWorldApp
{
public static void main(String[] args)
{
JFrame helloWorld = new HelloWorldFrame();
helloWorld.setVisible(true);
}
}
You need layout manager like in this example.
The structure you should have is : JFrame > JPanel > JLabel.
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JLabel label_1 = new JLabel("some text");
JLabel label_2 = new JLabel("other text");
panel.add(label_1);
panel.add(label_2);
frame.add(panel);
frame.pack();
frame.setVisible(true);
In your code you have some JButton: add them to the JPanel, not to the JLabel !

Categories