【问题标题】:Adding Figure to JPanel, then to another JPanel and then a JFrame将 Figure 添加到 JPanel,然后添加到另一个 JPanel,然后添加到 JFrame
【发布时间】:2013-11-12 01:35:04
【问题描述】:

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

【问题讨论】:

    标签: java swing graphics jframe jpanel


    【解决方案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());
        }
    
    }
    

    这没有意义。您已经从JPanel 扩展了FourFans,然后在...内创建了另一个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 默认使用 FlowLayout

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

    panelMain.setLayout(new BorderLayout());

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

    【讨论】:

    • 我像你说的那样移动了 setVisible。在我写的代码开头 add(panelMain) 为什么不起作用?
    • 成功了!通过此更改,这部分代码似乎不再被使用。私人JPanel panelMain;私人 FourFans panelfans; public n00832607() { //主面板 panelMain = new JPanel();添加(面板主); //创建粉丝 panelfans = new FourFans(); panelMain.add(panelfans); } 这是对的还是我应该把它留在里面?
    • 很抱歉,该评论中的代码看起来很草率。我不知道如何在评论中更好地格式化它。
    • 你可能不需要frame.add(new FourFans());
    • 我的错,你应该删除frame.add(new FourFans());,因为你实际上已经在构造函数中添加了它。在这个阶段我会质疑panelMain 的必要性,但如果你以后要添加更多东西,你应该可以保留它......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-02
    • 2011-09-13
    相关资源
    最近更新 更多