Java 添加行不工作
我正在尝试创建一个项目列表,当我点击Java 添加行不工作,java,swing,user-interface,Java,Swing,User Interface,我正在尝试创建一个项目列表,当我点击+按钮时,会添加一个新行。问题是,尽管要添加项的函数被调用,但由于某些原因,面板从未更新。这是我的代码: public static GridBagConstraints getContraint() { GridBagConstraints c = new GridBagConstraints(); c.insets = new Insets(3, 3, 3, 3); c.weightx = 1.0/3
+
按钮时,会添加一个新行。问题是,尽管要添加项的函数被调用,但由于某些原因,面板从未更新。这是我的代码:
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);
}
publicstaticgridbagconstraints getcontaint(){
GridBagConstraints c=新的GridBagConstraints();
c、 插图=新插图(3,3,3,3);
c、 权重x=1.0/3;
c、 填充=GridBagConstraints.HORIZONTAL;
c、 锚点=GridBagConstraints.CENTER;
c、 gridy=1;
返回c;
}
公共静态void addRow(JPanel j,gridbagc){
c、 gridy++;
系统输出打印(“测试”);
对于(int h=0;h<3;h++){
c、 gridx=h;
JTextField=新的JTextField();
j、 添加(f,c);
}
}
公共静态JPanel createLayout(int行){
最终JPanel产品=新JPanel(新BorderLayout());
最终JPanel列表=新JPanel(新GridBagLayout());
String[]标签={“学校”、“高级”、“新手”};
双倍重量=.3333;
final GridBagConstraints c=getControint();
对于(int j=0;j
添加新组件后,JPanel
j
需要进行重新验证
和重新喷漆
:
j.revalidate();
j.repaint();
注意:
JTable
组件已经提供了添加行的功能添加新组件后,JPanel
j
需要进行重新验证
和重新绘制
:
j.revalidate();
j.repaint();
注意:
JTable
组件已经提供了添加行的功能@camickr有趣的不是,这是关于此代码的第三个问题,至少第三次建议了JTable
)@MadProgrammer,我没有阅读他关于这个主题的第一个问题,也没有在他的第二个问题中考虑JTable。但你是对的,4个答案中有3个是表中建议的。希望OP能得到提示,而不是试图重新发明轮子。@camickr不好笑,这是关于此代码的第三个问题,至少第三次JTable
被建议;)@MadProgrammer,我没有阅读他关于这个主题的第一个问题,也没有在他的第二个问题中考虑JTable。但你是对的,4个答案中有3个是表中建议的。希望OP能得到提示,而不是试图重新发明轮子。