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.
Related
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.
I'm just starting out in java and trying to use some example code I found online to get started, but for some reason, I am unable to compile this code. I on Ubuntu 16.04 and I have the "default-jdk" installed.
Here's the code:
import java.awt.*;
import java.awt.event.WindowListener;
import javax.swing.*;
import java.io.*;
public class Test extends JFrame{
public static void main (String argv [])
{
new Test("Window Application");
}
public Test(String title)
{
super(title);
setSize(200, 100);
addWindowListener((WindowListener) new WindowDestroyer());
setVisible(true);
}
private class WindowDestroyer extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
}
When I try doing javac Test.java I get 2 cannot find symbol errors.
private class WindowDestroyer extends WindowAdapter
public void windowClosing(WindowEvent e)
From the Java 8 docs for WindowAdapter, it is defined as java.awt.event.WindowAdapter.
You need to import the class first:
import java.awt.event.WindowAdapter;
in addition to your other imports.
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowListener;
As a side note, you might be tempted to just do
import java.awt.event.*;
to avoid import errors like that in the future.
I suggest reading the discussions on Why is using a wild card with a Java import statement bad? to have an idea on the pro's and con's of doing so.
I can see, that you create simple Swing application window and close it close window. You do it in incorrect way. It is much better to use setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE) (only if you not plan to do smth. special before). And use SwingUtilities.invokeLater() to execute asynchronously on the AWT event dispatching thread:
public class Test extends JFrame {
public static void main(String... ars) {
SwingUtilities.invokeLater(() -> new Test().setVisible(true));
}
public Test() {
super("Window Application");
setSize(200, 100);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
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...
It's Giving me an error saying that "The method setContentPane(Container) in the type JFrame is not applicable for the arguments (GamePanel)"
Here is my Code:
package main;
import javax.swing.JFrame;
public class Game {
public static void main(String[] args){
JFrame window = new JFrame("Dragon Tales");
window.setContentPane(new GamePanel());
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setResizable(false);
}
}
I am following a tutorial exactly and his screen shows no errors at all.
Your GamePanel class does not extend any Swing GUI component such as Container or one of its children. Probably it should extend JPanel.
i.e.,
import javax.swing.JPanel;
public class GamePanel extends JPanel {
// .... etc
}
Please don't add the urgent or "help as soon as possible" bit. Yes your question is very important, but it is no more important than anyone else's.
Edit: Mad's link is worth putting in the answer: The Oracle Swing Tutorial.
Can someone convert this into Clojure, I don't know to do the line setMainWindow(argument) like things....
import com.vaadin.Application;
class something {
public void init() {
Window main = new Window("The Main Window");
setMainWindow(main);
addComponent(new WindowOpener("Window Opener", main));
}
}
Update:
package app;
import com.vaadin.Application;
import com.vaadin.ui.Button;
import com.vaadin.ui.Window;
/**
* The Application's "main" class
*/
#SuppressWarnings("serial")
public class MyVaadinApplication extends Application{
private Window window;
#Override
public void init(){
window = new Window("My Vaadin Application");
setMainWindow(window);
window.addComponent(new Button("Click Me"));
}
}
There is a "/lib/vaadin.jar" which contains all "com.vaadin.*" things.
I think setMainWindow(window); is from the extended class. I am not going to write that method.
Literal translation:
(defn init []
(let [main (Window. "The Main Window")]
(setMainWindow main)
(addComponent (WindowOpener. "Window Opener" main))))
Though it doesn't make much sense without the context.
See http://dev.vaadin.com/wiki/Articles/ClojureScripting. Also I would suggest http://www.odesk.com.