简体   繁体   中英

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM