gpt4 book ai didi

java - 添加行不起作用

转载 作者:行者123 更新时间:2023-12-01 18:49:29 24 4
gpt4 key购买 nike

我正在尝试创建一个项目列表,当我点击 + 按钮时,会添加一个新行。问题是,尽管调用了用于添加项目的函数,但由于某种原因面板永远不会更新。这是我的代码:

    public static GridBagConstraints getContraint() {
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(3, 3, 3, 3);
c.weightx = 1.0/3;
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.CENTER;
c.gridy = 1;
return c;
}
public static void addRow(JPanel j, GridBagConstraints c) {
c.gridy++;
System.out.println("Test");
for (int h = 0; h < 3; h++) {
c.gridx = h;
JTextField f = new JTextField();
j.add(f, c);
}
}
public static JPanel createLayout(int rows) {
final JPanel product = new JPanel(new BorderLayout());
final JPanel list = new JPanel(new GridBagLayout());
String[] lables = {"School ", "Advanced #", "Novice # "};
double weight = .3333333333333;

final GridBagConstraints c = getContraint();
for (int j = 0; j < lables.length; j++) {
c.gridx = j;
JLabel f = new JLabel(lables[j]);
list.add(f, c);
}

for (int i = 0; i < rows; i++) {
addRow(list, c);
}
// c.gridy++;
// c.gridx = 0;
// c.gridwidth = GridBagConstraints.REMAINDER;
// c.anchor = GridBagConstraints.NORTHWEST;
// c.fill = GridBagConstraints.NONE;

JPanel b = new JPanel();
JButton add = new JButton("+");
add.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
addRow(list, c);
list.repaint();

}
});
b.add(add);
JButton delete = new JButton("-");
b.add(delete);
product.add(b, BorderLayout.SOUTH);
product.add(list, BorderLayout.CENTER);
return product;
}
public static void printDebates(ArrayList<Judge> d) {
for (Judge j : d) {
System.out.printf("Judge: %s ", j.toString() + (j.getDebates().get(0).isAdvanced() ? 'A' : 'N'));
for (Debate de : j.getDebates()) {
System.out.printf("Round: %s ", de != null ? de.toString() : "null");
}
System.out.print("\n");
}
}
public static void main(String[] args) throws IOException {
JFrame frame = new JFrame("Debate Calculator");
JPanel debates = new JPanel();
frame.add(createLayout(5), BorderLayout.NORTH);
frame.pack();
frame.setVisible(true);
}

最佳答案

添加新组件后,JPanel j 需要重新验证重新绘制:

j.revalidate();
j.repaint();

注意:JTable 组件已提供添加行的功能

关于java - 添加行不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16268981/

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