java - 将Figure添加到JPanel,然后添加到另一个JPanel,然后添加到JFrame

标签 java swing graphics jframe jpanel

对于我的项目,我必须在一帧上显示三个不同的图形。我认为可以做到这一点的方法是将一个主面板添加到框架中,然后将其他三个框架添加到主框架中。第一个是在某些位置填充的四个圆圈,第二个是图表,第三个是时钟。目前我只是想让第一个起作用,以便我可以将其应用到其他两个。我的程序可以编译,但是当我运行它时,我只是得到一个空白窗口。是什么原因导致它不显示图形? 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。这是它的图片 Fans with Whitespace

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

最佳答案

我建议您将 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());
    }

}

这没有道理。您已从 JPanel 扩展了 FourFans,然后在...中创建了另一个 JPanel。相反,只需尝试添加 Fan >直接到它...例如...

class FourFans extends JPanel {

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

}

根据问题的更改进行更新

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

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

panelMain.setLayout(new BorderLayout());

您将来可能需要将其更改为 GridLayout,但这将帮助您入门;)

关于java - 将Figure添加到JPanel,然后添加到另一个JPanel,然后添加到JFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19919317/

相关文章:

java - 如何将具有共享主键的数据库模式转换为 hibernate ?

java - 具有存在子句的 hibernate 条件

java - BufferedImage 上的 JButton 透明度

java - 在数据更改时刷新 Jframe

r - 居中条形图

dictionary - 在位置缩放时将 SVG 元素保持为固定大小

java - 旋转玩家面对行星

java - 在 Java 中使用 static 关键字的 5 种方法

java - 将 JPanel 添加到 JFrame 中?

winforms - 如何创建一个像 VS 2010 一样形状奇特的高质量闪屏?