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
}
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.
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)
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.
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.
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.