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.
Related
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.
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.
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.
This is the "main" class (doesn't contain the main method)
import javax.swing.*;
import java.awt.*;
//import java.lang.Object;
//import java.awt.event.ActionListener;
//import java.awt.event.;
public class Program {
public JFrame frame;
public JPanel header;
public JPanel text;
public JPanel body;
public JTextField input;
public JButton agregar;
public List listA;
public List listB;
public Program(String title) {
frame = new JFrame(title);
frame.setSize(500,600);
frame.setResizable(false);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
header = new JPanel();
header.setBackground(new Color(255,204,50));
header.setBounds(0,0,500,100);
text = new JPanel();
text.setBackground(new Color(255,204,100));
text.setBounds(0,100,500,50);
text.setLayout(null);
//Inicializando la "entrada"
input = new JTextField(20);
input.setBounds(50,13,300,25);
text.add(input);
agregar = new JButton();
agregar.setBounds(360,12,80,25);
agregar.setText("Agregar");
text.add(agregar);
//Listo
body = new JPanel();
body.setBackground(new Color(255,204,150));
body.setBounds(0,150,500,450);
//Lo que está dentro del body
listA = new List(20);
body.add(listA);
listB = new List(20);
body.add(listB);
//Listo
//Añadir todos los paneles al frame principal
frame.add(header);
frame.add(text);
frame.add(body);
}
}
And this is the MAIN class (This one contains the main method):
public class Main {
public static void main(String[] args) {
new Program("Ordenamiento Recursivo");
}
}
Each time I run the application, the UI components are presented differently, please see attached screen shot.
Well, thanks to everyone who responded the post, I finished the program and I'm very happy with the final result, here it is:
In case anyone wants to take a look at the code, here it is: Link
Problems:
You're call setVisible(true) on your JFrame before adding components and this will lead to unreliable drawing of your program's graphics and is why you are seeing different results. Don't do this, but rather call it after you've added all to the top-level Window.
As the others are saying, read up and learn to use the layout managers.
Different windows with the same code?
I think that is very simple and possible by implements CardLayout
I'd suggest don't opening a new Top-Level Container, only if is there really important reason then use JDialog or JOptionPane
Be sure to construct the GUI on the EDT. Not doing so can cause unpredictable results.
Call pack() after the components are added using layouts and then call setVisible(true).
You will need a layout manager for your form so setting the layout manager to null is not the thing to do.
Work in progress here ... https://gist.github.com/2510570
Couple of changes. Not quite finished yet, but check out the following
Have Program extend a JFrame.
Have set a layout manager.
Update
Finally I knocked this up in IntelliJ's form designer.
https://gist.github.com/2512197
Where you want to attach behaviour to the buttons search through the code for the comments that ask you to add code. Although I did this in the InteliJ Ultimate (this one that costs money) I think that no-cost free to download Community Edition UI designer also paints Swings GUIs. Very quick and easy. Netbeans also has a good GUI painter.
The Swing Tutorial on oracle.com is worth reviewing also.
So, for some reason when I try to use a for loop to initialize panels in chess board, it actually loops the loop itself. In other words, it doesn't go on forever, but it starts and completes again and again.
package chessgame;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ChessGame extends JFrame implements ActionListener{
public static final int WIDTH=800;
public static final int HEIGHT=800;
public static void main(String[] args) {
ChessGame gui = new ChessGame();
gui.setVisible(true);
}
public ChessGame(){
super("Chess Game Demo");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(8,8));
JPanel[] chessSquares = new JPanel[64];
for (int a=0;a<64;a++){
System.out.println(a);
}
}
}
I have included all potentially relevant code because I plan to use indices of chessSquares to color squares black and white. When I do this I also get a NullPointerException. I can understand why I'm getting that given the following error, but I cannot at all understand why a would be printed 0, 1....62, 63 over and over again. I am relatively new to Swing and have absolutely no idea why it does this. If anyone could explain that would be tremendously helpful. Thanks.
Don't put meaningful initialization in ChessGame's constructor, but instead override frameInit. When you do, also be sure to call super.frameInit(). See the javadoc or this tutorial.