Java 似乎无法使现有JPanel添加更多JPanel

Java 似乎无法使现有JPanel添加更多JPanel,java,swing,jpanel,Java,Swing,Jpanel,我试图允许我的程序添加更多的内容行,最终允许他们输入点值(x&y)。但是,当按下add按钮时,我似乎无法让我的JPanel实际添加任何内容 下面是我试图添加到整个JPanel中的类(JPanel扩展): class Coordinates extends JPanel { public String x; public String y; public int id; public Coordinates(int spot) { super(); x = "

我试图允许我的程序添加更多的内容行,最终允许他们输入点值(x&y)。但是,当按下add按钮时,我似乎无法让我的JPanel实际添加任何内容

下面是我试图添加到整个JPanel中的类(JPanel扩展):

class Coordinates extends JPanel
{
  public String x;
  public String y;
  public int id;

  public Coordinates(int spot)
  {
    super();
    x = "0";
    y = "0";
    id = spot;
    setLayout(null);
    setSize(440,30);
    setLocation(10,5);
    JLabel num = new JLabel("Point #" + (id + 1) + ":");
    num.setLocation(0,0);
    num.setSize(40,20);
    add(num);
  }
}
下面是添加按钮的AbstractAction扩展,按下按钮时应该添加它:

class AddACT extends AbstractAction
{
  public void actionPerformed(ActionEvent e)
  {
    pointList.add(new Point());
    gui.add(new Coordinates(1));
  }
}
如果有人需要大量的代码块,下面是整个窗口的代码:

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

public class PointWindow
{
  private ArrayList<Point> pointList = new ArrayList<Point>();

  public PointWindow()
  {
    initialize();
  }

  public void initialize()
  {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("Set New Points");
    frame.setContentPane(createGUI());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(440,600);
    frame.setLocationRelativeTo(null);
    frame.setResizable(false);
    frame.setVisible(true);
  }

  public JPanel createGUI()
  {
    final JPanel gui = new JPanel();
    gui.setLayout(null);

    Font boldTitle = new Font("Arial", Font.BOLD, 30);

    class Line extends JPanel
    {
      protected void paintComponent(Graphics g)
      {
        super.paintComponent(g);
        g.drawLine(0,0,430,0);
      }
    }

    class Coordinates extends JPanel
    {
      public String x;
      public String y;
      public int id;

      public Coordinates(int spot)
      {
        super();
        x = "0";
        y = "0";
        id = spot;
        setLayout(null);
        setSize(440,30);
        setLocation(10,5);
        JLabel num = new JLabel("Point #" + (id + 1) + ":");
        num.setLocation(0,0);
        num.setSize(40,20);
        add(num);
      }
    }

    final JButton add = new JButton("Add");
    final JButton remove = new JButton("Remove");
    final JButton makeGraph = new JButton("Generate Graph!");

    final JLabel numPointsL = new JLabel("0");

    class AddACT extends AbstractAction
    {
      public void actionPerformed(ActionEvent e)
      {
        pointList.add(new Point());
        gui.add(new Coordinates(1));
      }
    }

    class RemoveACT extends AbstractAction
    {
      public void actionPerformed(ActionEvent e)
      {

      }
    }

    class MakeGraphACT extends AbstractAction
    {
      public void actionPerformed(ActionEvent e)
      {

      }
    }

    JPanel line1 = new JPanel();
    line1.setLayout(null);
    line1.setLocation(5,5);
    line1.setSize(440,35);
    gui.add(line1);

    JLabel text1 = new JLabel("Point Manager");
    text1.setFont(boldTitle);
    text1.setLocation(0,0);
    text1.setSize(300,35);
    line1.add(text1);

    add.setLocation(220,0);
    add.setSize(100,33);
    add.addActionListener(new AddACT());
    line1.add(add);

    remove.setLocation(330,0);
    remove.setSize(100,33);
    add.addActionListener(new RemoveACT());
    line1.add(remove);

    JPanel line2 = new JPanel();
    line2.setLayout(null);
    line2.setLocation(5,45);
    line2.setSize(440,30);
    gui.add(line2);

    JLabel text2 = new JLabel("Current number of points:");
    text2.setLocation(2,1);
    text2.setSize(165,20);
    line2.add(text2);

    numPointsL.setLocation(172,1);
    numPointsL.setSize(40,20);
    line2.add(numPointsL);

    makeGraph.setLocation(222,0);
    makeGraph.setSize(206,22);
    makeGraph.addActionListener(new MakeGraphACT());
    line2.add(makeGraph);

    Line l1 = new Line();
    l1.setLocation(5,29);
    l1.setSize(420,1);
    line2.add(l1);

    return gui;
  }
}
import java.util.*;
导入javax.swing.*;
导入java.awt.event.*;
导入java.awt.*;
公共类点窗口
{
private ArrayList pointList=new ArrayList();
公共点窗口()
{
初始化();
}
公共无效初始化()
{
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame=新JFrame(“设置新点”);
setContentPane(createGUI());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
框架设置尺寸(440600);
frame.setLocationRelativeTo(空);
frame.setresizeable(false);
frame.setVisible(true);
}
公共JPanel createGUI()
{
最终JPanel gui=新JPanel();
setLayout(空);
Font boldTitle=新字体(“Arial”,Font.BOLD,30);
类行扩展了JPanel
{
受保护组件(图形g)
{
超级组件(g);
g、 抽绳(0,0430,0);
}
}
类坐标扩展了JPanel
{
公共字符串x;
公共字符串y;
公共int id;
公共坐标(int spot)
{
超级();
x=“0”;
y=“0”;
id=点;
setLayout(空);
设置尺寸(440,30);
设定位置(10,5);
JLabel num=新JLabel(“点#“+(id+1)+”:”;
num.setLocation(0,0);
设置大小(40,20);
添加(num);
}
}
最终JButton add=新JButton(“add”);
最终JButton移除=新JButton(“移除”);
final JButton makeGraph=新JButton(“生成图形!”);
最终JLabel numPointsL=新JLabel(“0”);
类AddACT扩展了AbstractAction
{
已执行的公共无效操作(操作事件e)
{
添加(新点());
添加(新坐标(1));
}
}
类RemoveACT扩展了AbstractAction
{
已执行的公共无效操作(操作事件e)
{
}
}
类MakeGraphACT扩展了AbstractAction
{
已执行的公共无效操作(操作事件e)
{
}
}
JPanel line1=新的JPanel();
line1.setLayout(空);
第1行。设置位置(5,5);
第1行:设置尺寸(440,35);
gui.add(第1行);
JLabel text1=新的JLabel(“点管理器”);
text1.setFont(粗体标题);
text1.设置位置(0,0);
text1.设置大小(300,35);
第1行。添加(文本1);
添加设置位置(220,0);
增加设置尺寸(100,33);
add.addActionListener(新的AddACT());
第1行。添加(添加);
移除。设置位置(330,0);
移除。设置尺寸(100,33);
add.addActionListener(newRemoveAct());
第1行。添加(删除);
JPanel line2=新的JPanel();
line2.setLayout(空);
第2行。设置位置(5,45);
line2.设置尺寸(440,30);
gui.add(第2行);
JLabel text2=新的JLabel(“当前点数:”);
text2.设置位置(2,1);
text2.设置大小(165,20);
第2行。添加(文本2);
numPointsL.setLocation(172,1);
numPointsL.setSize(40,20);
第2行。添加(numPointsL);
makeGraph.setLocation(222,0);
makeGraph.setSize(206,22);
addActionListener(新的MakeGraphACT());
第2行。添加(makeGraph);
第l1行=新行();
l1.设定位置(5,29);
l1.设置尺寸(420,1);
第2行。添加(l1);
返回gui;
}
}

它们是begin添加的,但它是begin添加到其他组件后面的

不要使用
setLocation(10,5)
尝试
setLocation(5,75)
,这样会将其放置在行下方

另外,在
add
调用之后调用
gui.repaint()
,请求重新绘制ui


更重要的是,使用布局管理器。

不要担心Point类,它还没有完全编码。简单,使用适当的布局管理器也可以看到这一点。好的,所以我必须使用布局管理器来实际向现有JPanel添加内容?有什么特别的原因我不能只使用add()方法,或者它在java中就是这样工作的吗?不,你不“必须”使用布局管理器,问题是,你为什么不这样做?您已经直接找到了不应该使用
null
layout的原因。好的,我会调查一下。然而,原来的问题仍然存在。我已将
设置位置(10,5)
更改为
设置位置(100100)
,但窗口中仍然没有显示任何内容。有什么想法吗?尝试在
add