0

I want to add textfields dynamically on the click of a button but the value to be fetched and the button are in one class and the panel where i want to add the textboxes and sliders adjacent to the are in a different class. Code is -

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 {}

In this class I need to create the text fields dynamically according to the no. entered. In the variable noofguests and the click of the button in the previous class.

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 に答える