Cannot import JFrame or Dimension - java

I already have the system library in my package and have tried resetting the metadata. What else can I do?
Errors with comments stating what errors they are.
package JFrameTest;
import java.awt.Dimension; //The package java.awt is not accessible
import javax.swing.JFrame; //The type javax.swing.JFrame is not accessible
public class Empty extends JFrame { //JFrame cannot be resolved to a type
public static void main (String[] args) {
new Main().setVisible(true); // Main cannot be resolved to a type
}

JavaFX was deleted from JDK, so you need to download JavaFX from here, here or use this guide. If you use maven just add the dependencies.

Related

How do I add an image with this code?

import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Image;
import javax.sound.midi.Patch;
import javax.swing.*;
this is were the code start
public class graficCar extends JComponent
{
private ImageIcon image1=null;
private JLabel label1=null;
private ImageIcon image2=null;
private JLabel label2=null;
graficCar(){
setLayout(new FlowLayout());
the problem here
image1=new ImageIcon(getClass().getResource("4596067.png"));
label1=new JLabel(image1);
add(label1);
}
the main
public static void main(String[] args)
{
graficCar g=new graficCar();
g.setDebugGraphicsOptions(JFrame.EXIT_ON_CLOSE);
g.setVisible(true);
}
As you mentioned, your png file is in C:\Users\user\Downloads folder. I bet class is somewhere else.
The problem is getResource() finds file in classpath by default. Here you could find details if you want to go deeper.
One solution is to put .png right next to your .java class but it's bad practice. More convenient way described here. The best practice is to create special folder for your resources in the project and then use relative path to it. How to do this is out of scope although it's described in many sites and in Stackoverflow too. Don't hesistate to use Search :)

Can't add a simple checkbox in frame 'java AWT'

I'm learning GUI programming in java AWT and am a bit stuck. I can't add a couple of check-boxes in a frame the code i'm trying is-
package awt2;
import java.awt.*;
import java.awt.event.*;
public class Checkbox {
public static void main(String args[]) {
Frame mainFrame= new Frame("Checkbox test");
Checkbox checkBox1= new Checkbox();
Checkbox checkBox2= new Checkbox();
checkBox1.setBounds(100,100,50,50);
checkBox2.setBounds(150,120,50,50);
mainFrame.add(checkBox1);
mainFrame.add(checkBox2);
mainFrame.setVisible(true);
}
}
The error reckons this on checkBox1.setBounds() and checkBox2.setBounds()-
The method setBounds(int, int, int, int) is undefined for the type Checkbox
And on mainFrame.add()-
The method add(Component) in the type Container is not applicable for the arguments (Checkbox)
Can someone explain what these errors are all about and how can i fix them? Also i'm using eclipse IDE and javac version 1.8.0_144
You got a problem because your class is named Checkbox,
which is hiding the java.awt.Checkbox class.
Just choose another class name, for example CheckboxTest:
package awt2;
import java.awt.*;
import java.awt.event.*;
public class CheckboxTest {
// your code
}

java, eclipse "The import laobjects cannot be resolved"

Using Eclipse Mars, I copied a tiny Java program from the Internet that displays a small window containing a "Click me!" JButton and a JLabel. Every time you click the button, the label is updated to show the total number of clicks. Trying to learn about Java packages, I added this package statement at the top:
package nineteen_3;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class Main {
public static void main(String [] args {
...
}
}
This code worked fine.
Learned the trick of making a JButton that looks like a JLabel, Decided to make a package of new objects beginning with KLabel, a modified JButton.
package laobjects;
import javax.swing.JButton;
public class KLabel extends JButton {
...
}
No compile errors.
Back in Main {}, I added this import statement for KLabel. Below are existing import statements:
import laobjects.KLabel;
... and get: The import laobjects cannot be resolved
And in the Main {} code:
KLabel lbl = new KLabel("Count: 0");
I'm getting KLabel cannot be resolved to a type
Both the Package and Project Explorers show 'KLabel' as hi-level entries:
KLabel
src
laobjects
KLabel.java
KLabel
label
KLabel(String)

Icon not showing when jar file is run

I'm a beginner in Java and I followed a tutorial to write this program (yes, I that much of a beginner) and it works perfectly when I run it on Eclipse. It also runs great on the computer I coded it on. However, if I send it to another computer (just the .jar file) and run it, it fails because it can't find the icon. Here is everything I've got. The icon I'm using is saved in the bin folder along with all the class files for the program. For privacy reasons, I replaced certain lines with "WORDS".
The tutorial I followed in two parts:
Part 1 - https://buckysroom.org/videos.php?cat=31&video=18027
Part 2 - https://buckysroom.org/videos.php?cat=31&video=18028
My main class (I called it apples cause the tutorial did).
import javax.swing.JFrame;
public class apples {
public static void main(String[] args) {
Gui go = new Gui();
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go.setSize(1920,1080);
go.setVisible(true);
}
}
And now my second class, "Gui":
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
public class Gui extends JFrame {
private JButton custom;
public Gui () {
super("WORDS");
setLayout(new FlowLayout());
Icon b = new ImageIcon(getClass().getResource("b.png"));
custom = new JButton(null, b);
custom.setToolTipText("WORDS");
add(custom);
HandlerClass handler = new HandlerClass();
custom.addActionListener(handler);
}
private class HandlerClass implements ActionListener {
public void actionPerformed(ActionEvent event) {
JOptionPane.showMessageDialog(null, String.format("WORDS", event.getActionCommand()));
JOptionPane.showMessageDialog(null, String.format("WORDS", event.getActionCommand()));
JOptionPane.showMessageDialog(null, String.format("WORDS", event.getActionCommand()));
JOptionPane.showMessageDialog(null, String.format("WORDS", event.getActionCommand()));
}
}
}
Thank you so much for helping!
It's worth reading Loading Images Using getResource where it's explained in detail along with loading images from jar as well.
You can try any one based on image location.
// Read from same package
ImageIO.read(getClass().getResourceAsStream("b.png"));
// Read from src/images folder
ImageIO.read(getClass().getResource("/images/b.png"))
// Read from src/images folder
ImageIO.read(getClass().getResourceAsStream("/images/b.png"))
Read more...

Java audio playing error

I am java newbie.
I was reading a tutorial book, and tried almost all code given as examples, and they all worked perfectly. But, when I tried this audio playing tutorial, even though I understood most of it, I still can't make it play. It gives me error, saying
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
at MouseClicker.main(MouseClicker.java:9)
Here is the code.
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.applet.*;
import java.net.URL;
public class MouseClicker extends Jframe{
AudioClip click;
public static void main(String[] args){
new MouseClicker();
}
public MouseClicker(){
this.setSize(400,400);
this.setTitle("Mouse Clicker");
this.addMouseListener(new Clicker());
URL urlClick = MouseClicker.class.getResource("hello.wav");
click = Applet.newAudioClip(urlClick);
this.setVisible(true);
}
private class Clicker extends MouseAdapter
public void mouseClicked(MouseEvent e){
click.play();
}
}
public class MouseClicker extends Jframe{
It's a JFrame, not a Jframe. (capital F)
Remember, Java is case sensitive!
You're missing an opening brace in the definition of the Clicker class
private class Clicker extends MouseAdapter {
^
A Java IDE can highlight these syntax errors.
Also ensure that the audio file hello.wav is located in the same location as MouseClicker.class (the bin folder in this case) so that it can be read as a resource.

Categories