简体   繁体   English

当我单击“添加”按钮时,我想添加文本字段。 但它不起作用。 帮我

[英]I want to add text field when i clicked the button “add”. But it doesn't work. Help me

this is my code.这是我的代码。 I want to let the user add a new text field as they want, but it only can add one text field.我想让用户根据需要添加一个新的文本字段,但它只能添加一个文本字段。 will loop for Participant gonna work? Participant的循环会起作用吗?

 import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JLabel; import java.awt.Font; import javax.swing.JTextField; import javax.swing.JButton; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JTextArea; import java.awt.Color; import java.awt.SystemColor; public class General { private JFrame frame; private JTextField textFirst; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { General window = new General(); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the application. */ public General() { initialize(); } /** * Initialize the contents of the frame. */ private void initialize() { frame = new JFrame(); frame.setBounds(100, 100, 450, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(null); JLabel lblInsertParticipantsTerm = new JLabel("Insert Participants Term:"); lblInsertParticipantsTerm.setFont(new Font("Tahoma", Font.BOLD, 12)); lblInsertParticipantsTerm.setBounds(34, 32, 169, 14); frame.getContentPane().add(lblInsertParticipantsTerm); textFirst = new JTextField(); textFirst.setBounds(23, 52, 180, 20); frame.getContentPane().add(textFirst); textFirst.setColumns(10); JButton button = new JButton("ADD"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { JTextField newTextField = new JTextField(20); frame.getContentPane().add(newTextField); frame.validate(); frame.repaint(); } }); button.setBounds(205, 51, 53, 23); frame.getContentPane().add(button); JTextArea textArea = new JTextArea(); textArea.setBackground(SystemColor.control); textArea.setBounds(23, 52, 180, 199); frame.getContentPane().add(textArea); } }

You need to have a layout manager.您需要有一个布局管理器。 Do the following:请执行下列操作:

frame.getContentPane().setLayout(new FlowLayout());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AJAX更新面板不起作用。 请帮助我找出我在哪里错...? - AJAX Update Panel Doesn't Work. Please help me finding where am I wrong…? 当我添加样式时,HTML 按钮不起作用 - HTML button doesn't work when I add style 我想通过单击 + 按钮添加相同的文本字段,但问题是只有第一个 + 按钮工作其他 + 按钮不工作? 需要帮助 - I want to add same text field by clicking + button but problem is only first + button is working other + button is not working? help needed 单击按钮时,我想使用 JavaScript 插入文本。 但是 ```.value()``` 似乎不起作用 - I want to insert text using JavaScript when I click on button. But ```.value()``` doesn't seems to work 我想将表格信息添加到表格信息中,但它不起作用 - I want to add Table information to Form information, but it doesn't work 我想使用ajax获取数据表单数据库,但它不起作用。 我应该改变什么? - i want to get data form database using ajax, but it doesn't work. what should i change? 当我点击添加到购物车时,该项目是文本印刷。 当点击其他添加到购物车按钮时,文本被覆盖 - when i clicked on add to cart the item is text is priniting. when clicked on other add to cart button the text is overwriting 我正在尝试使用正则表达式,但它不起作用。 给我一个提示 - I'm trying to use a regular expression but it doesn't work. Give me a hint 在AnimateCC中,当我从库中添加按钮时,它不起作用,而是连续地在所有状态下运行 - In AnimateCC, when I add a button from the library it doesn't work and instead runs through all of the states continuously 我尝试向其中添加Bootstrap下拉按钮, <a>但下拉菜单无法正常工作。</a> <a>我该怎么做呢?</a> - I tried to add a Bootstrap dropdown button to <a> but the dropdown menu wont work. How do I do this?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM