gpt4 book ai didi

java - 似乎无法让现有的 JPanel 添加更多的 JPanel

转载 作者:行者123 更新时间:2023-11-29 05:52:16 25 4
gpt4 key购买 nike

我试图让我的程序添加更多内容行,最终允许它们输入点值 (x & y)。但是,当按下添加按钮时,我似乎无法让我的 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;
}
}

最佳答案

它们是开始添加的,但它是在其他组件之后开始添加的。

不要使用 setLocation(10,5) 尝试 setLocation(5, 75),它将把它放在该行的下面。

此外,在add 调用后调用gui.repaint() 请求重绘ui

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

关于java - 似乎无法让现有的 JPanel 添加更多的 JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13447008/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com