点击JButton添加新的JTextFields

时间:2018-03-29 16:55:52

标签: java jbutton add textfield jtextfield

我正在使用发票系统,我想在每次点击添加新按钮时创建新字段。

每次都需要在下面的代码中添加字段。

字段需要显示在各自的列下。

    JPanel panel_2 = new JPanel();
    panel_2.setBounds(10, 256, 990, 303);
    panel.add(panel_2);
    panel_2.setLayout(null);

    code = new JTextField();
    code.setBounds(10, 11, 86, 20);
    panel_2.add(code);
    code.setColumns(10);
    code.setEditable(false);

    desc = new JTextField();
    desc.setBounds(106, 11, 345, 20);
    panel_2.add(desc);
    desc.setColumns(10);
    desc.setEditable(false);

    quantity = new JTextField("0");
    quantity.setBounds(461, 11, 86, 20);
    panel_2.add(quantity);
    quantity.setColumns(10);
    quantity.setEditable(false);

    price = new JTextField("0");
    price.setBounds(557, 11, 106, 20);
    panel_2.add(price);
    price.setColumns(10);
    price.setEditable(false);

    individualTotal = new JTextField();
    individualTotal.setBounds(673, 11, 106, 20);
    panel_2.add(individualTotal);
    individualTotal.setColumns(10);
    individualTotal.setEditable(false);

以下是我设置的按钮:

    JButton newEntry = new JButton("+");
    newEntry.setBackground(Color.PINK);
    newEntry.setForeground(Color.BLUE);
    newEntry.setFont(new Font("Tahoma", Font.BOLD, 15));
    newEntry.setBounds(10, 204, 57, 20);
    panel.add(newEntry);
    newEntry.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            code.setEditable(true);
            desc.setEditable(true);
            quantity.setEditable(true);
            price.setEditable(true);
            individualTotal.setEditable(true);
          } 
    });
    newEntry.setEnabled(false);

    JButton minusEntry = new JButton("-");
    minusEntry.setBackground(Color.PINK);
    minusEntry.setForeground(Color.RED);
    minusEntry.setFont(new Font("Wide Latin", Font.BOLD, 16));
    minusEntry.setBounds(77, 205, 57, 20);
    panel.add(minusEntry);
    minusEntry.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            code.setEditable(false);
            desc.setEditable(false);
            quantity.setEditable(false);
            price.setEditable(false);
            individualTotal.setEditable(false);
        } 
    });
    minusEntry.setEnabled(false);

我知道在这个网站上某处必须有答案,但我似乎找不到它。

请注意我是Java开发的新手

1 个答案:

答案 0 :(得分:0)

创建自己的JPanel子类,代表一个"数据表"或表格或任何

public DataPanel extends JPanel{


   private JTextField field1 = new JTextField();
   private JTextField field2 = new JTextField();
   // ..... and so on

   public DataPanel(YourDataObject data){
     field1.setText(data.getValue1());
     field2.setText(data.getValue2());
     // ... and so on
     // then add all of your text fields to the panel
     add(field1);
     add(field2);
     // .... and so on
   }
}

然后在按钮上单击,将面板添加到要在

上显示的组件
onClick(SomeEvent event){
  yourComponent.add(new DataPanel(yourDataObject));
}