So I'm new to Java learning some basics from videos on YouTube, I'm learning to make a GUI/Window and at the moment I'm trying to get iamges to be displayed but I'm not sure if code is wrong/old or the images are not in the right spot/location. Here's what I've written up so far. Help would be much appreciated. Please and Thank you.
import java.awt.*;
import javax.swing.*;
public class FirstGUI extends JFrame {
private static Object out;
private JLabel label;
private JButton button;
private JTextField textfield;
private ImageIcon image1;
private JLabel label1;
private ImageIcon image2;
private JLabel label2;
public FirstGUI() {
setLayout (new FlowLayout());
label = new JLabel("Hi, I'm a label!");
add(label);
textfield = new JTextField(15);
add(textfield);
button = new JButton("Click me!");
add(button);
button = new JButton("No, CLICK ME!!");
add(button);
label = new JLabel("This is the end of the program?");
add(label);
image1 = new ImageIcon(getClass().getResource("Apiary.png"));
label1 = new JLabel(image1);
image2 = new ImageIcon(getClass().getResource("bee.png"));
label2 = new JLabel(image2);
}
public static void main(String[] args) {
FirstGUI gui = new FirstGUI();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//* gui.setSize(400, 400);
gui.setVisible(true);
gui.setTitle("Hello World");
gui.pack();
}
}
What I get in the errors are:
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.(Unknown Source)
at FirstGUI.(FirstGUI.java:39)
at FirstGUI.main(FirstGUI.java:50)
First you're not adding the labels to the frame, so even if that executes it won't show the image icons. So don't forget to add the labels to the frame:
add(label1);
add(label2);
Second I tried your code and it worked fine for me, it only printed the error you mentioned when I didn't import the image icon to the package i'm working in.
For this you need to do this:
Right click on your src package-> import-> General -> File System and then click next and select the directory that contains the images, click Ok and then add the images you specified in the code.
Related
I'm trying to add a .gif image to a JButton, but can't seem to get the image to load when i run the code. I've included a screenshot. Included is the frame that's created. I'd really appreciate any help that can be provided. Stack is telling me I can't enter images yet, so it created a link for it. I'm also going to enclose the actual code here:
package java21days;
import javax.swing.*;
import java.awt.*;
public class ButtonsIcons extends JFrame {
JButton load, save, subscribe, unsubscribe;
public ButtonsIcons() {
super("Icon Frame");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
//Icons
ImageIcon loadIcon = new ImageIcon("load.gif");
ImageIcon saveIcon = new ImageIcon("save.gif");
ImageIcon subscribeIcon = new ImageIcon("subscribe.gif");
ImageIcon unsubscribeIcon = new ImageIcon("unsubscribe.gif");
//Buttons
load = new JButton("Load", loadIcon);
save = new JButton("Save", saveIcon);
subscribe = new JButton("Subscribe", subscribeIcon);
unsubscribe = new JButton("Unsubscribe", unsubscribeIcon);
//Buttons To Panel
panel.add(load);
panel.add(save);
panel.add(subscribe);
panel.add(unsubscribe);
//Panel To A Frame
add(panel);
pack();
setVisible(true);
} //end ButtonsIcon Constructor
public static void main(String[] arguments) {
ButtonsIcons ike = new ButtonsIcons();
}
} //end ButtonsIcon Class
enter image description here
The easiest way is.
Label or Jbutton and what ever else supports HTML 3.5
JLabel a = new JLabel("");
add that to your container.
Haven't figured out how to enter code sorry
This is my homework.
My directions:
Class GuessGame: This class contains the main function which creates a JFrame object, sets up the JFrame’s size, visibility and exit.
Class GuessGameFrame: GuessGameFrame class extends JFrame. So the GuessGameFrame class has all variables and methods defined in class JFrame.
I am not 100% sure what my problem is but I believe the JPanel is not working correctly
Any help would greatly be appreciated, if I left out anything important please let me know and I can add more detail.
main class:
public class GuessGame {
public static void main(String[] args){
GuessGameFrame localGuessGameFrame = new GuessGameFrame();
localGuessGameFrame.setVisible(true); //set visible
localGuessGameFrame.setSize(380,175); // set size 380 width 175 height
localGuessGameFrame.setDefaultCloseOperation(localGuessGameFrame.EXIT_ON_CLOSE); // needed to enable the red X button to close the program
}
}
extended class:
import javax.swing.*;
import java.awt.*;
public class GuessGameFrame extends JFrame
{
private int lastDistance; // distance between last guess and number
private Color background; // background color of application
private JFrame f;
static JPanel p;
private JButton b1;
private JLabel l1;
private JLabel l2;
JTextField t1 = new JTextField(8);
private JLabel l3;
//declare constructor
public GuessGameFrame()
{
//create our JFrame
f = new JFrame("Guessing game");
//create our panel
p = new JPanel();
p.setBackground(Color.MAGENTA); // background color is magenta
//create our labels
l1 = new JLabel("I have a number between 1 and 1000.");
l2 = new JLabel("Can you guess my number? Enter your first Guess:.");
l3 = new JLabel("Guess result appears here.");
//create our button
b1 = new JButton("New Game");
//add button and label to panel
p.add(l1); // Adds label 1
p.add(l2); // adds label 2
p.add(t1); // adds textfield 1
p.add(l3); // adds label 3
p.add(b1); // adds button 1
//add panel to JFrame
f.add(p);
}
}
Screenshot of the output window when executed:
I expect it to look like this:
You need remove JFrame f in GuessGameFrame class because GuessGameFrame is already a JFrame:
And update your code:
f = new JFrame("Guessing game"); to this.setTitle("Guessing game");
f.add(p); to this.getContentPane().add(p);
So, i have a code in Java that is says its successfull but i get errors that isn't even from my project. I can't do anything about it though.... I've tried importing only the needed parts and i still get same errors. I don't even know what a "container" is. I just finished the Java basics and moved on to this. Now i have a second computer (a macbook air) and it's got same code but it doesn't get any errors at all. Might it be me IDE, Java JDK or something? Or just something weird with the imported files? Thanks.
Code :
package windowsgui;
import javax.swing.*;
import java.awt.*;
public class WindowsGUI extends JFrame {
private JLabel label;
private JButton button;
private JTextField testfield;
public WindowsGUI() {
setLayout (new FlowLayout());
label = new JLabel("This is a label");
add(testfield);
button = new JButton("This is a button");
add(button);
}
public static void main (String args[]) {
WindowsGUI gui = new WindowsGUI();
gui.setSize(600, 400);
gui.setResizable(false);
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setVisible(true);
}
}
Error :
run:
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1091)
at java.awt.Container.add(Container.java:1003)
at javax.swing.JFrame.addImpl(JFrame.java:564)
at java.awt.Container.add(Container.java:415)
at windowsgui.WindowsGUI.<init>(WindowsGUI.java:19)
at windowsgui.WindowsGUI.main(WindowsGUI.java:27)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)
label = new JLabel("This is a label");
add(label);
testfield = new JTextField("This is text Field");
add(testfield);
button = new JButton("This is a button");
add(button);
you are not initialize the testfield, but you try to add the testfield so it gives null pointerException. Solution : initialize the testfield and then add it.
I am new to Java; but I'm having a blast. I feel like I'm missing something really simple; but I can't figure it out.
I want the RadioButtons to be displayed inside the JFrame."
public class HelloWorldFrame extends JFrame
{
private TitledBorder title;
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");
ButtonGroup bG = new ButtonGroup();
bG.add(button1);
bG.add(button2);
bG.add(button3);
label.add(button1);
label.add(button2);
label.add(button3);
helloWorld.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
//The main class starts here
public class HelloWorldApp
{
public static void main(String[] args)
{
JFrame helloWorld = new HelloWorldFrame();
helloWorld.setVisible(true);
}
}
The first question is why? Why do you want to add the radio buttons to a JLabel?
Having said that, you can set the labels layout manager to something other then it's default value of null...
label.setLayout(new FlowLayout());
label.add(button1);
label.add(button2);
label.add(button3);
Next...your class extends from JFrame, but in your constructor, you are creating another JFrame, this means that when you do...
JFrame helloWorld = new HelloWorldFrame();
helloWorld.setVisible(true);
Nothing will be displayed, because nothing has been added to the frame.
Instead, make your class extend from something like JFrame and then add that to your JFrame in main
Updated
I just did some testing and doing this (adding buttons to a label) won't work well, as the JLabel calculates it's preferred size based on the text and icon, not it's contents (like something like JPanel would)...just saying...
I'm trying to set the components of this application to a set location using setLocation so far i haven't been able to move the components. There is more code but its get calling and setting this code for the most part. any ideas?
import javax.swing.*;
public class HangmanPanel extends JPanel {
private static final long serialVersionUID = -5793357804828609325L;
public HangmanPanel(){
JLabel heading = new JLabel("Welcome to the Hangman App");
JButton Button = new JButton("Ok");
//Button.addActionListener();
JLabel tfLable = new JLabel("Please Enter a Letter");
JTextField text = new JTextField(10);
String input = text.getText();
heading.setLocation(50, 20);
tfLable.setLocation(20, 100);
text.setLocation(320, 50);
Button.setLocation(230, 100);
this.add(heading);
this.add(tfLable);
this.add(text);
this.add(Button);
}
}
You should not use setLocation() to layout Swing components, it is much better to use a Layout. Please have a look at these Layout Tutorials.