0

对于我的项目,我必须在一帧上显示三个不同的图形。我认为可以做到这一点的方法是将一个主面板添加到框架中,然后将其他三个框架添加到主框架中。第一个是在特定位置填充的四个圆圈,第二个是图表,第三个是时钟。目前我只是想让第一个工作,以便我可以将其应用于其他两个。我的程序可以编译,但是当我运行它时,我只是得到一个空白窗口。是什么导致它不显示图形?n00832607 类是这样命名的,并且是小写的,因为在我的学校系统上提交项目的手续,请忽略这一点。

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

编辑 我有你建议的工作,现在粉丝出现了,他们非常小,但我可以稍后尝试处理这个问题,因为他们现在真的出现了。我试图将其应用于下一个数字,但它似乎不起作用。第一部分现在看起来像

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

}   
}    

这是我正在使用的 SquareFuction 的代码。当我运行时,风扇显示得很小,并且 SquareFunction 可以出现在很多空白处。这是它的图片有空格的粉丝

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));
}
}
4

1 回答 1

1

我建议您将setVisible调用移至方法中的最后一行,并尝试向其中添加一个面板,例如...

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

这可确保您在尝试使用框架之前已完成修改框架

下一个...

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

}

这没有意义。您已经扩展FourFansJPanel然后在...中创建了另一个JPanel...相反,只需尝试将Fans 直接添加到它...例如...

class FourFans extends JPanel {

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

}

根据对问题的更改进行更新

您获得结果的主要原因是因为默认panelMain使用 a FlowLayout

尝试将其更改为使用 aBorderLayout代替,例如...

panelMain.setLayout(new BorderLayout());

您将来可能需要将其更改为 a GridLayout,但这会让您开始;)

于 2013-11-12T01:38:23.503 回答