Getting an error with frame.add() not sure why - java

so im making a simple box from java and this is the code so far:
import java.awt.Canvas;
import javax.swing.JFrame;
public class Display {
public static final int WIDTH = 800;
public static final int LENGTH = 600;
public static void main(String[] args) {
Display game = new Display();
JFrame frame = new JFrame();
frame.setResizable(false);
frame.setVisible(true);
frame.setSize(WIDTH, LENGTH);
frame.add(game);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
im getting an error here "frame.add(game);" its says "The method add(Component) in the type Container is not applicable for the arguments (Display)"
How im not sure what im doing wrong, im using javaSE-1.6

You're trying to add an instance of Display, which isn't a Swing component, to your frame, hence the error. Looking at your imports, you probably meant to add a JPanel within your Display class (if there is one) to the frame.
Alternatively, your Display class needs to inherit from something like JComponent, if you want to add it directly. You shouldn't mix AWT and Swing components needlessly.

Your display needs to extend Component as that is what the add method expects as a parameter. It's probably a good idea to make it a JPanel.
You mention that you just want a basic window. This will do the trick:
import javax.swing.*;
public class Display extends JFrame {
public static void main(String[] args){
new Display();
}
public Display() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 200);
setVisible(true);
}
}

Related

Java Swing panels and graphics stopped displaying

I'm new to Java Swing (and fairly new to Java in general) and I've been messing around with Swing for a while, but after making some changes my panels stopped displaying and I don't know why.
When using isDisplayable() on my JPanel object it returns false. After more investigation my program does not seem to display graphics at all.
Even a simple piece of code doesn't work for me anymore:
public class window extends JFrame {
window(){
setBounds(100,100,1200,700);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBackground(Color.black);
setVisible(true);
}
public static void main(String[] args) throws IOException{
new window();
}
I have no idea what I changed for this to happen, but all I get is a blank a completely blank window.
Any help is appreciated a lot!
See the code comments.
import java.awt.Color;
import javax.swing.*;
public class BlackWindow extends JFrame {
BlackWindow(){
setBounds(100,100,400,100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// sets the frame black, not the content pane
//setBackground(Color.BLACK);
getContentPane().setBackground(Color.BLACK);
setVisible(true);
}
public static void main(String[] args) {
new BlackWindow();
}
}
This example also speaks to a few general principles we follow when using Swing's JFrame:
Don't extend the JFrame class, just use an instance of one.
Don't do anything significant to the coloring or styling of a frame, instead color (for example) a JPanel and add it to the frame.

Undefined Type for JFrame

so i was getting a error in Eclipse for my java program to make a new JFrame and i couldnt quiet figure out my it wouldnt load the correct data to set it to be visable and to set the title, location, size, and what it should do when someone closes the JFrame so id like some help if i could please
import javax.swing.*;
import java.awt.*;
public class JFrame
{
public static void main(String[] args)
{
JFrame JF = new JFrame();
JF.setTitle("Test");
JF.setSize(400, 200);
JF.setLocation(200, 300);
JF.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
JF.setVisible(true);
}
}
Thanks for the help in advance
You named your own class JFrame, thus hiding the standard javax.swing.JFrame class.
Choose another name for your class.

Jpanel not showing in JFrame

I am writing this code so that it will add JPanels and will change the background color when clicked. The code in the class that runs the JFrame class is:
import javax.swing.*;
public class project9Driver {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
Project9 w=new Project9();
w.setVisible(true);
w.setSize(900, 900);
}
}
The Project9 class is:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class Project9 extends JFrame implements MouseListener{
JPanel panes[]=new JPanel[64];
public void Project9(){
JFrame mainFrame=new JFrame();
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container mainBox=mainFrame.getContentPane();
mainBox.setLayout(new GridLayout(8,8));
mainBox.addMouseListener(this);
for(int i=0;i<=63;i++){
panes[i].setBackground(Color.WHITE);
mainBox.add(panes[i]);
}
}
public void paint(Graphics g){
super.paintComponents(g);
}
public void mouseClicked(MouseEvent e) {
for(int i=0;i<=64;i++){
if(e.getSource()==panes[i]){
Random xR=new Random();
Random yR=new Random();
Random zR=new Random();
int x=xR.nextInt(255),y=yR.nextInt(255),z=zR.nextInt(255);
panes[i].setBackground(new Color(x,y,z));
}
}
}
}
Whenever I try to run the program, it comes up with an empty GUI window. What am I missing?
Project9 is a Frame, and you're creating another Frame inside of Project9 and you're not showing it, and becouse of that, just Project9 (w) is draw on screen, but it doesn't have anything.
You have to use "this" instead of another frame.
public class Project9 extends JFrame implements MouseListener{
JPanel panes[]=new JPanel[64];
public void Project9(){
//JFrame mainFrame=new JFrame(); delete this.
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container mainBox= this.getContentPane();
Three answer all point out different problems with your code.
You should start with a proper working example. The TopLevelDemo.java code from the Swing tutorial on Using Top Level Containers will show you the basics and the proper way to create GUI components.
You should not even be extending JFrame. It is important that all components are created on the Event Dispatch Thread which is why the invokeLater() code is used in the tutorial.
Use the tutorial for examples of using all Swing components. Other example will show you how to extend JPanel for a more complex GUI. You don't need to extend JFrame.
The original problem I noticed with the code is:
public void paint(Graphics g){
super.paintComponents(g);
}
Don't override the paint() method. There is no reason to do this!
The paint() method is for painting and you are not doing any custom painting.
First problem is this that you are using constructor Project9 w=new Project9();
and in class Project9 you are defining method public void Project9(){ you should remove void keyword.
public Project9(){
//JFrame mainFrame=new JFrame(); delete this.
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container mainBox= this.getContentPane();

Java JPanel Drawing rectangle drawRect(); what to do next, or which component of Java will suit better?

I have drawn a rectangle using a JPanel
My main objective is to store my Requirement Engineering chapter into a JPanel or a JFrame
import java.awt.*;
import javax.swing.*;
class RequirementEngineering extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent();
g.drawRect(10,10,60,60);
g2.drawString("Feasibility study", 20, 20); //rectangle is my main objective, I will look after the string later
}
public static void main(String[] args)
{
}
}
how do I display the JPanel? I know, JApplet doesn't require a main method, but how do we represent JPanel in main() method?
I have this doubt for long, and other posts are confusing me further, Could I have a direct question
My main question being "How to add JFrame to JPanel" pertaining my current coding
thanks in advance
see if you need to use a Window based app, you can do as:
JPanel customPanel = new RequirementEngineering();
JFrame frame = new JFrame("my window");
frame.getContentPane().add(customPanel );
frame.setSize(300,200);
frame.setVisible(true);
If you need in Applet,
public class JAppletExample extends JApplet {
public void init() {
Container content = getContentPane();
JPanel customPanel = new RequirementEngineering();
content.add(customPanel );
}
And you can run it using appletViewer or in any Web Browser such as IE.

JFrame does not appear

I'm using the Intelij idea platform.
I have the following code:
package GUI.test;
import javax.swing.*;
public class Frame extends JFrame{
Frame(){}
public void main (String[] args){
new Frame();
}
}
I expected to see a JFrame after compiling this code, but was nothing appeared. What kind of problem can it be?
Frames are not visible by default--use the
setVisible(true);
method in order to display frames.
You also might want to take a look at other options such as
setSize(int width, int height);
method to resize a frame,
setLocation(int xLoc, int yLoc);
to move the frame, and
setTitle(String title);
to set the title of the component.
Aside, it is good practice to use a variable to hold components so they can be manipulated when needed.
You might want to add this
Frame()
{
setVisible(true);
setSize(100,100);
}
package GUI.test;
import javax.swing.*;
public class Frame extends JFrame{
private myFrame;
public Frame()
{
myFrame = new JFrame("put a title here"); //title not necessary but it's there if you want it
myFrame.setSize(400,400); // sets the window size
myFrame.setVisible(true); // toggles the frame to be visible inside the window
myFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // this will terminate the VM once the *last* JFrame is closed, so you can have multiple frames open and just close one
}
public void main (String[] args){
new Frame();
}
}
Thanks a lot for answering. I wrote an example without details. I have added: setVisible(true);
setSize(100,100);
to a class constructor. But i havent seen form jet.
Have to say about one feature. I'm not running method main in class Frame, i'm compiling this class Frame. In my previous exersises with java i allways run main method, but when i tried to work with GUI form and used swing library, ability to run method main have dissapiared.
If you haven't cought what i mean, i'd send a screenshot.

Categories

Resources