[英]How to create multiple JPanels with JTextField input?
I am currently working on my school project to practice vocabulary, I have a method in my GUI that creates new vocabulary and the name of the list, I wanted to create a button that adds more Panels with input fields just this prototype image.我目前正在做我的学校项目来练习词汇,我的 GUI 中有一个创建新词汇和列表名称的方法,我想创建一个按钮来添加更多带有输入字段的面板,只是这个原型图像。
My idea is that when the user clicks AddMoreButton
it will add one JPanel just like P
Panel, then the user can write vocabulary to send it to my database, is it possible to create something that?, I tried looping the P
panel but it did not not change, any help would be appreciated.我的想法是,当用户单击AddMoreButton
时,它将像P
Panel 一样添加一个 JPanel,然后用户可以编写词汇表将其发送到我的数据库,是否可以创建一些东西?,我尝试循环P
面板但它确实不会改变,任何帮助将不胜感激。
private JPanel SetUpCreate() {
JPanel createPanel = new JPanel();
nameListInput = new JTextField(INPUT_FIELD_WIDTH);
termInput = new JTextField(INPUT_FIELD_WIDTH);
defintionInput = new JTextField(INPUT_FIELD_WIDTH);
p = new JPanel();
doneCreate = new JButton("Done");
doneCreate.addActionListener(new DoneCreateButtonAction());
addMoreButton = new JButton("Add");
addMoreButton.addActionListener(new AddMorePanelsListener());
p.setBorder(new BevelBorder(BevelBorder.RAISED));
p.add(termInput);
p.add(defintionInput);
JScrollPane pane = new JScrollPane(p);
createPanel.add(nameListInput);
createPanel.add(p);
createPanel.add(pane);
createPanel.add(doneCreate);
return createPanel;
}
private class DoneCreateButtonAction implements ActionListener {
public DoneCreateButtonAction() {
super();
}
public void actionPerformed(ActionEvent e) {
String namelist = nameListInput.getText();
String termglosa = termInput.getText();
String defintionglosa = defintionInput.getText();
try {
if (model.createWordList(namelist) && (model.createGlosa(termglosa, defintionglosa))) {
cl.show(cardPanel, "home");
}
} catch (IOException e1) {
JOptionPane.showMessageDialog(frame, "skapelsen av listan fungerar ej.");
}
}
}
private class AddMoreButtonAction implements ActionListener {
public AddMoreButtonAction() {
super();
}
public void actionPerformed(ActionEvent e) {
}
}
As your code is not an Minimal Reproducible Example , I cannot provide further assistance than this:由于您的代码不是Minimal Reproducible Example ,因此我无法提供更多帮助:
Red part: Your main JPanel
with BoxLayout Green part: another JPanel
with your JTextField
in it.红色部分:您的主JPanel
和 BoxLayout 绿色部分:另一个JPanel
,其中包含您的JTextField
。 Purple part: JScrollPane
Blue parts: custom JPanels
with 2 panes in them, one on top for the number, one on the bottom for both JTextField
s and icon, so I would say GridBagLayout
or BoxLayout
+ FlowLayout
Orange part: JPanel
with GridBagLayout
or FlowLayout
紫色部分: JScrollPane
蓝色部分:自定义JPanels
,其中有 2 个窗格,一个在顶部用于数字,一个在底部用于JTextField
和图标,所以我会说GridBagLayout
或BoxLayout
+ FlowLayout
橙色部分:带有GridBagLayout
或FlowLayout
的JPanel
Each time you clic on the +
icon, you just create a new instance of the custom blue JPanel
and that's it.每次单击+
图标时,您只需创建自定义蓝色JPanel
的新实例,仅此而已。
What I understand from your question is that you want to add another panel every time the user clicks the Add button and the panel to add contains fields for entering a word and its definition.我从您的问题中了解到,您希望每次用户单击“添加”按钮时添加另一个面板,并且要添加的面板包含用于输入单词及其定义的字段。
I see JScrollPane
appears in the code you posted in your question.我看到JScrollPane
出现在您在问题中发布的代码中。 I think this is the correct implementation.我认为这是正确的实现。 In the below code, every time the user clicks the Add button I create a panel that contains the fields for a single word definition.在下面的代码中,每次用户单击“添加”按钮时,我都会创建一个面板,其中包含单个单词定义的字段。 This newly created panel is added to an existing panel that uses GridLayout
with one column.这个新创建的面板被添加到现有面板中,该面板使用GridLayout
和一列。 Hence every time a new word definition panel is added, it is placed directly below the last word panel that was added and this GridLayout
panel is placed inside a JScrollPane
.因此,每次添加新的单词定义面板时,它都会直接放置在添加的最后一个单词面板的下方,并且此GridLayout
面板放置在JScrollPane
内。 Hence every time a word definition panel is added, the GridLayout
panel height increases and the JScrollPane
adjusts accordingly.因此,每次添加单词定义面板时, GridLayout
面板高度都会增加, JScrollPane
也会相应调整。
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
public class MorPanel implements ActionListener, Runnable {
private static final String ADD = "Add";
private JFrame frame;
private JPanel vocabularyPanel;
@Override
public void run() {
showGui();
}
@Override
public void actionPerformed(ActionEvent actionEvent) {
String actionCommand = actionEvent.getActionCommand();
switch (actionCommand) {
case ADD:
vocabularyPanel.add(createWordPanel());
vocabularyPanel.revalidate();
vocabularyPanel.repaint();
break;
default:
JOptionPane.showMessageDialog(frame,
actionCommand,
"Unhandled",
JOptionPane.ERROR_MESSAGE);
}
}
public JButton createButton(String text) {
JButton button = new JButton(text);
button.addActionListener(this);
return button;
}
public JPanel createButtonsPanel() {
JPanel buttonsPanel = new JPanel();
buttonsPanel.add(createButton(ADD));
return buttonsPanel;
}
private JScrollPane createMainPanel() {
vocabularyPanel = new JPanel(new GridLayout(0, 1));
vocabularyPanel.add(createWordPanel());
JScrollPane scrollPane = new JScrollPane(vocabularyPanel);
return scrollPane;
}
private JPanel createWordPanel() {
JPanel wordPanel = new JPanel();
JLabel wordLabel = new JLabel("Enter Term");
JTextField wordTextField = new JTextField(10);
JLabel definitionLabel = new JLabel("Enter Term Definition");
JTextField definitionTextField = new JTextField(10);
wordPanel.add(wordLabel);
wordPanel.add(wordTextField);
wordPanel.add(definitionLabel);
wordPanel.add(definitionTextField);
return wordPanel;
}
private void showGui() {
frame = new JFrame("Vocabulary");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(createMainPanel(), BorderLayout.CENTER);
frame.add(createButtonsPanel(), BorderLayout.PAGE_END);
frame.setSize(480, 200);
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new MorPanel());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.