0

我想在单击按钮时动态添加文本字段,但要获取的值和按钮在一个类中,而我要添加相邻的文本框和滑块的面板在不同的类中。代码是 -

public class TipSplitting extends JPanel 

JLabel lblNoOfGuests = new JLabel("No. Of guests");
    lblNoOfGuests.setBounds(10, 26, 95, 14);
    add(lblNoOfGuests);

private JTextField noofguests = new JTextField();
    noofguests.setBounds(179, 23, 86, 20);
    add(noofguests);
    noofguests.setColumns(10);
JButton btnTiptailoring = new JButton("TipTailoring");
    btnTiptailoring.setBounds(117, 286, 89, 23);
    add(btnTiptailoring);

public class TipTailoring extends JPanel {}

在这个类中,我需要根据 no 动态创建文本字段。进入。在变量noofguests和上一个类中的按钮的单击中。

4

1 回答 1

2

我真的看不出有什么问题,但这里有一些你所描述的简单演示代码。

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class TestDynamicallyAddedTextFields {

    private void initUI() {
        JFrame frame = new JFrame(TestDynamicallyAddedTextFields.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final JPanel textfieldContainerPanel = new JPanel();
        textfieldContainerPanel.setLayout(new GridBagLayout());
        JLabel nrOfGuests = new JLabel("Nr. of guests");
        final JFormattedTextField textfield = new JFormattedTextField();
        textfield.setValue(Integer.valueOf(1));
        textfield.setColumns(10);
        JButton add = new JButton("Add");
        add.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (textfield.getValue() != null) {
                    addTextFieldsToPanel((Integer) textfield.getValue(), textfieldContainerPanel);
                }
            }
        });
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEADING));
        panel.add(nrOfGuests);
        panel.add(textfield);
        panel.add(add);
        frame.add(panel, BorderLayout.NORTH);
        frame.add(new JScrollPane(textfieldContainerPanel));
        frame.setSize(300, 600);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    protected void addTextFieldsToPanel(Integer value, JPanel textfieldContainerPanel) {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.gridheight = 1;
        for (int i = 0; i < value; i++) {
            textfieldContainerPanel.add(new JTextField(20), gbc);
        }
        textfieldContainerPanel.revalidate();
        textfieldContainerPanel.repaint();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TestDynamicallyAddedTextFields().initUI();
            }
        });
    }

}

结果:

结果图片

于 2012-10-08T07:58:35.450 回答