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.
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
I cannot run any java programs through the command prompt (Windows 10) but I can run them through the IDE (IntelliJ) I am using. I am currently testing it using a very basic JFrame program which I will provide below. I am able to compile the .java file just fine with the javac command but whenever I try and run it using the java command, I get the error:
"Error: Could not find or load main class JavaTestProgram.java
Caused by: java.lang.ClassNotFoundException: JavaTestProgram.java"
I have tried to fix the PATH variable under system settings and tried using different IDE's and text editors.
import java.awt.FlowLayout;
import javax.swing.*;
public class JavaTestProgram {
public static void main(String s[]) {
JFrame frame = new JFrame("JFrame Example");
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
JLabel label = new JLabel("This is a label!");
JButton button = new JButton();
button.setText("Press me");
panel.add(label);
panel.add(button);
frame.add(panel);
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Based on the error message provided, it looks like it's because you're trying to use a class name of JavaTestProgram.java. The class name should be simply JavaTestProgram.
NOTE: My English isn't the best so Please Don't mind too much Grammar Mistakes.
Hey there, Java Starter here, Anyways i was Testing a mini "beta" version of the Program i'm planning to code, So i made a TextField And it wont go Under my JLabel i made, i tried to use BorderLayout.PAGE_END to get it under / at the bottom but it won't get it. Here's the Code:
package test;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TextTest {
private static TextField field;
private static void createGUI() {
Font a = new Font(null, Font.BOLD, 0);
Font size = a.deriveFont(20f);
JLabel test = new JLabel("");
test.setPreferredSize(new Dimension(200,200));
test.setText("<html> Welcome to the EMOJI Translator! Type the <br> Emoji in the Text Area And hit Enter! and it will say What the emoji means! <html>");
test.setFont(size);
field = new TextField(2);
field.setSize(new Dimension(200,200));
field.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String test = field.getText();
String search1 = ":D";
if(test.equals(search1)) {
System.out.println("This is an Happy Smiley.");
}
}});
JFrame b = new JFrame("TEST");
b.setLocationRelativeTo(null);
b.setPreferredSize(new Dimension(350,350));
b.setLayout(new FlowLayout());
b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b.getContentPane().add(field, BorderLayout.PAGE_END);
b.getContentPane().add(test, BorderLayout.CENTER);
b.pack();
b.setVisible(true);
}
public static void main(String[] args) {
createGUI();
}
}
Here's a Link to the screenshot of how it ended looking in my Computer:
http://imgur.com/y988zUx
If you know Whats wrong please respond to this question.
You are trying to use properties of BorderLayout for a gui make with FlowLayout. In Java, you cannot mix different layout managers, by doing this the layout properties will be ignored.
You should set you layout manager to BorderLayout, so it accepts your properties:
b.setLayout(new BorderLayout());
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.
[UPDATE] i fixed the problem. user "Hovercraft Full Of Eels" was right, i changed 3D graphics settings prefer to nvidea driver and problem fixed
i have problem and can't understand what is matter.
i am creating simple GUI window and putting JTextField object on it.
when i run project and enter text in text field it does not displays properly.
i already tryed to reinstal java, but can't fix problem.
can anyone help me?
here is my GUI window image
here is my code (no exception thrown, nor error was occured!)
package test;
import javax.swing.*;
import java.awt.*;
public class MainClass {
public static void main(String[] args) {
new Form().Run();
}
}
class Form{
JFrame form = new JFrame();
JButton btn = new JButton();
JTextField txt = new JTextField();
public Form(){
form.setLayout(new FlowLayout());
form.setSize(500, 500);
form.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
btn.setText("caption");
form.add(btn);
txt.setColumns(10);
form.add(txt);
}
public void Run(){
form.setVisible(true);
}
}
Try to change your "Look and Feel" settings in your GUI-Class, there should be the string "Nimbus". Change it to "Windows" and try it again. The second thing you can try is to set a Border to your JTextfield, probably it works.
The code for changing or to set a Border on your JTextfield:
JTextField your_field = new JTextField(10);
your_field.setBorder(BorderFactory.createMatteBorder(0, 0, 5, 0, Color.black));