Java 按按钮添加新字段
我正在NetBeans中创建一个程序,用户可以在其中创建自己的CSV文件。我做了一个图形用户界面。当我按下按钮时,我希望一个新的JLabel和一个新的JTextField出现在现有的JLabel和JTextField下面,次数与按下按钮的次数相同。我该怎么做 我做了类似的事情,我正在使用with解决这个问题,这是我的代码: 编辑-您的问题引起了我的兴趣,我已根据您的需要更改了我的代码(当然只有基本代码,您需要改进)Java 按按钮添加新字段,java,netbeans,jform,Java,Netbeans,Jform,我正在NetBeans中创建一个程序,用户可以在其中创建自己的CSV文件。我做了一个图形用户界面。当我按下按钮时,我希望一个新的JLabel和一个新的JTextField出现在现有的JLabel和JTextField下面,次数与按下按钮的次数相同。我该怎么做 我做了类似的事情,我正在使用with解决这个问题,这是我的代码: 编辑-您的问题引起了我的兴趣,我已根据您的需要更改了我的代码(当然只有基本代码,您需要改进) 全局变量 private GroupLayout m_layout; priv
全局变量
private GroupLayout m_layout;
private SequentialGroup m_verticalSg;
private ArrayList<Component> m_labelList;
private ArrayList<Component> m_textFieldList;
private ParallelGroup m_horizontalPgLabels;
private ParallelGroup m_horizontalPgTextfields;
最后一步是向按钮添加一个ActionListener,以调用方法
addNewRow()
请随时询问我是否有不清楚的地方。我发现您的\u面板有错误,我应该在那里放什么?您的\u面板是占位符,应该用面板的名称替换。有什么问题吗?也许我能帮你。全局变量必须在类的开头,2。将方法
createLayout()
和addNewRow()
复制到类中,3。将ActionListener添加到构造函数中的JButton中,并在该Listener中调用方法addNewRow()
:我创建了一个包含JButton和JPanelies的测试框架,你不知道你让我有多高兴。让我知道我是否能报答你的恩惠!
private void createLayout()
{
m_layout = new GroupLayout(YOUR_PANEL);
YOUR_PANEL.setLayout(m_layout);
//This SequentialGroup is used for the VerticalGroup
m_verticalSg = m_layout.createSequentialGroup();
m_verticalSg.addContainerGap();
//Two ParallelGroups are used. One for all labels and the other one for all textfields
m_horizontalPgLabels = m_layout.createParallelGroup(GroupLayout.Alignment.LEADING);
m_horizontalPgTextfields = m_layout.createParallelGroup(GroupLayout.Alignment.LEADING);
//These component lists are used for linkSize() -> Equalize components width
m_labelList = new ArrayList<>();
m_textFieldList = new ArrayList<>();
m_layout.setHorizontalGroup(m_layout.createParallelGroup()
.addGroup(m_layout.createSequentialGroup()
.addContainerGap()
.addGroup(m_horizontalPgLabels)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) //Create gap between horizontal groups
.addGroup(m_horizontalPgTextfields)
.addContainerGap()));
m_layout.setVerticalGroup(m_layout.createParallelGroup().addGroup(m_verticalSg.addContainerGap()));
}
private void addNewRow()
{
if(m_layout == null)
createLayout();
Dimension dimLabel = new Dimension(100, 15);
Dimension dimTextfield = new Dimension(200, 20);
//Create a new label
JLabel lbl = new JLabel();
lbl.setText("Your text");
lbl.setIcon(null/*Your icon*/);
lbl.setSize(dimLabel);
lbl.setPreferredSize(dimLabel);
//Create a new textfield
JTextField txtField = new JTextField();
txtField.setSize(dimTextfield);
txtField.setPreferredSize(dimTextfield);
//Add components to arrays and increase index
m_labelList.add(lbl);
m_textFieldList.add(txtField);
//Create new ParallelGroup for the vertical SequentialGroup
ParallelGroup newVerticalParallelGroup = m_layout.createParallelGroup(GroupLayout.Alignment.LEADING);
newVerticalParallelGroup.addComponent(lbl);
newVerticalParallelGroup.addComponent(txtField);
m_verticalSg.addGroup(newVerticalParallelGroup);
m_verticalSg.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED);
//Add the new label to the horizontal label group
m_horizontalPgLabels.addComponent(lbl, GroupLayout.Alignment.CENTER);
//Add the new textfield to the horizontal textfield group
m_horizontalPgTextfields.addComponent(txtField);
m_layout.linkSize(SwingConstants.HORIZONTAL, m_labelList.toArray(new Component[m_labelList.size()]));
m_layout.linkSize(SwingConstants.HORIZONTAL, m_textFieldList.toArray(new Component[m_textFieldList.size()]));
}
jButton1.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
addNewRow();
}
});