简体   繁体   中英

Adding Figure to JPanel, then to another JPanel and then a JFrame

For my project I have to display three different figures on one frame. The way that I thought that I could do this is by having one main panel that is added to the frame and then three other frames added to the main frame. The first is four circles that are filled in at certain spots, the second is a graph, and the third a clock. Currently I am just trying to make the first one work so that I can apply that to the other two. My program compiles but when I run it I just get a blank window. What is causing it not to show the graphic? The n00832607 class is named that way and is lowercase because of a formality with turning projects in on my schools system, please ignore that.

import java.awt.*;
import javax.swing.*;
import java.util.*;

public class n00832607 extends JFrame {

private JPanel panelMain;
private FourFans panelfans;


public n00832607() {


//Main Panel
panelMain = new JPanel();
add(panelMain);

//Creating Fans
panelfans = new FourFans();
panelMain.add(panelfans);


}



 //Main Method

public static void main(String[] args) {
n00832607 frame = new n00832607();
frame.setVisible(true);
frame.setSize(400, 400);
frame.setTitle("Project 6");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 


}   
}    
//n00832607 Class Finish


class FourFans extends JPanel
{
  public FourFans()
  {
  JPanel panel = new JPanel();
  panel.setLayout(new GridLayout(2, 2));
  panel.add(new Fan());
  panel.add(new Fan());
  panel.add(new Fan());
  panel.add(new Fan());
  }

}

//Responsible for creating Fan

class Fan extends JPanel {

protected void paintComponent(Graphics g)
{
  super.paintComponent(g);
  int height = getHeight();
  int width = getWidth();
  g.setColor(Color.BLACK);
  g.drawOval((int)(0.09 * width), (int)(0.09*height), (int)(0.825 * height), (int)    (0.825 * height));
  g.fillArc((int)(0.1 * width), (int)(0.1 * height), (int)(0.8 * width), (int)(0.8 * height), 20, 30);
  g.fillArc((int)(0.1 * width), (int)(0.1 * height), (int)(0.8 * width), (int)(0.8 * height), 110, 30);
  g.fillArc((int)(0.1 * width), (int)(0.1 * height), (int)(0.8 * width), (int)(0.8 * height), 200, 30);
  g.fillArc((int)(0.1 * width), (int)(0.1 * height), (int)(0.8 * width), (int)(0.8 * height), 290, 30);
 }
}

EDIT I've got what you suggested to work and now the fans show up, they are very small but I can try to deal with that later since they actually show up now. I have tried to apply this to the next figure but it isn't seeming to work. The first part now looks like

public class n00832607 extends JFrame {

private JPanel panelMain;
private FourFans panelfans;
private SquareFunction panelfunction;

public n00832607() {



//Main Panel
panelMain = new JPanel();
add(panelMain);

//Creating Fans
panelfans = new FourFans();
panelMain.add(panelfans);

//Creating Function
panelfunction = new SquareFunction();
panelMain.add(panelfunction);

}



//Main Method

public static void main(String[] args) {
n00832607 frame = new n00832607();            
frame.setSize(400, 400);
frame.setTitle("Project 6");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
frame.setVisible(true);          

}   
}    

This is the code for SquareFuction that I am working with. When I run the fans show up small and there is plenty of whitespace where the SquareFunction could show up in. Here is a picture of it 带空白的粉丝

class SquareFunction extends JPanel
{
  public SquareFunction()
  {
     add(new GraphMaker());
  }
}

//Responsible for creating the graph

class GraphMaker extends JPanel {

protected void paintComponent(Graphics g)
{
  super.paintComponent(g);
  double scaleFactor = 0.1;
  Polygon p = new Polygon();
  int y = 0;
  for(int x = -100; x <= 100; x++)
  {
     if(y < 200 - (int)(scaleFactor * x * x))
     {
        y = 200 - (int)(scaleFactor * x * x);
     }
     p.addPoint(x+200,200-(int)(scaleFactor * x * x));
  }
  g.setColor(Color.BLACK);
  g.drawLine(20, y+1, getWidth() - 50, y+1);
  g.drawLine(200, 0, 200, y+1);
  g.drawString("X", 345, 200);
  g.drawString("Y", 200, 9);
  g.setColor(Color.BLUE);
  g.drawPolygon(p);
  g.setColor(Color.WHITE);
  g.drawLine(50, 200 - (int)(scaleFactor * 100 * 100), 300, 200 - (int)(scaleFactor * 100 * 100));
}
}

I would suggest you move the setVisible call to last line in the method and try adding one of your panels to it, for example...

public static void main(String[] args) {
    n00832607 frame = new n00832607();
    frame.setSize(400, 400);
    frame.setTitle("Project 6");
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

This ensures that you've finished modifying the frame BEFORE you try and use it

Next...

class FourFans extends JPanel {

    public FourFans() {
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(2, 2));
        panel.add(new Fan());
        panel.add(new Fan());
        panel.add(new Fan());
        panel.add(new Fan());
    }

}

This doesn't make sense. You've extended FourFans from JPanel then created another JPanel within in...Instead, simply try adding the Fan s directly to it...for example...

class FourFans extends JPanel {

    public FourFans() {
        setLayout(new GridLayout(2, 2));
        add(new Fan());
        add(new Fan());
        add(new Fan());
        add(new Fan());
    }

}

Updated based on changes to the question

The main reason you're getting the result your are is because panelMain is using a FlowLayout by default.

Try changing it to use a BorderLayout instead, for example...

panelMain.setLayout(new BorderLayout());

You may need to change this in the future to a GridLayout , but this will get you started ;)

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