Everything works in the program when the libraries with import javax.swing.* and import java.awt.*,but it doesn't work when something from these libraries is typed that doesn't offer awt and swing options in drop-down menu.
Example:
import javax.swing.*;
import java.awt.*;
public class MainFrame extends JFrame{
public static MainFrame instance = null;
private MainFrame() {
inicijalizacija();
}
private void inicijalizacija() {
Toolkit kt=Toolkit.getDefaultToolkit();
Dimension velicina=kt.getScreenSize();
int visina=velicina.height;
int sirina=velicina.width;
setSize(visina/2,sirina/2);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("GeRuDok");
}
public static MainFrame getInstance() {
if(instance==null) {
instance=new MainFrame();
}
return instance;
}
}
e.g Toolkit cannot be released here as awt, but offers it as com.sun.javafx.tk and like others(Dimension,getScreenSize...)
I thought it had something to do with the content assistant, but it doesn't work either.
It may have something to do with the Java version 1.8 that the project is in, but I really have no idea how to fix it.
I'd appreciate it.
Related
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);
}
}
How can I embed a PApplet into a JApplet ?
I wanted to add it to a JPanel inside the applet, but I couldn't.
If any of you know how I can do this. ??
As of Processing 3, you can no longer do this. PApplet no longer extends Applet, so it can't be treated as a component.
For 95% of users, this is okay. Applets are dead anyway, so you really shouldn't be using them. If at all possible, you should try deploying with Processing.js.
If you need to execute a Processing sketch from Java code, then you should use the PApplet.main("YourSketchNameHere"); function to launch it. Processing will take care of the window for you.
If you really need to treat a PApplet as a component, then you're going to have to go through its PSurface. The PSurface class contains a getNative() function that returns an object that can be treated as a component. But that's overkill for most Processing users.
Prior to Processing 3, this code should have worked for you as #Kevin has explained. So, if your question is directed towards understanding a legacy code here is what you will need to know:
import javax.swing.JFrame;
import javax.swing.JPanel;
class MyPApplet extends PApplet implements ActionListener{
#Override
public void setup() {
super.setup();
// setup
}
#Override
public void draw() {
// my draw code
}
}
public class PAppletDemo {
public static void main(String[] args) {
final JFrame frame = new JFrame("PApplet in Java Application");
JPanel panel = new JPanel();
//create an instance of your processing applet
final MyPApplet applet = new MyPApplet();
applet.init();
panel.add(applet); // From processing 3, this will give you error that applet is not a Component
frame.add(panel);
frame.setSize(applet.getSize().width, applet.getSize().height +200);
frame.setVisible(true);
}
}
To circumvent this, you will need to use PSurface getNative() function. Please refer to the example and discussion given on this link.
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.