使用弹出窗口将项目添加到数组列表 [英] Using a popup window to add items to arraylist
本文介绍了使用弹出窗口将项目添加到数组列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用弹出式面板来输入信息以添加到我的数组列表中.我找到了这个代码
I am trying to use a popup sort of panel to enter information in order to add to my arraylist. I found this code
import javax.swing.*;
public class JOptionPaneMultiInput {
public static void main(String[] args) {
JTextField xField = new JTextField(15);
JTextField yField = new JTextField(15);
JTextField zField = new JTextField(15);
JPanel myPanel = new JPanel();
myPanel.add(new JLabel("Item Name:"));
myPanel.add(xField);
myPanel.add(Box.createVerticalStrut(15)); // a spacer
myPanel.add(new JLabel("Number in inventory:"));
myPanel.add(yField);
myPanel.add(Box.createVerticalStrut(15)); // a spacer
myPanel.add(new JLabel("Unit Price:"));
myPanel.add(zField);
int result = JOptionPane.showConfirmDialog(null, myPanel,
"Please Enter data into all boxes", JOptionPane.OK_CANCEL_OPTION);
if (result == JOptionPane.OK_OPTION) {
}
}
}
它自己工作并显示我需要显示的内容,但是如何让它与我的 actionListner 一起工作?我希望它在用户单击添加按钮时弹出.
which works on it's own and displays what I need displayed, however how do I get it to work with my actionListner? I want it to pop up when the user clicks on the add button.
我也不确定如何将此信息输入到我之前构建的存储数组列表中,但如果需要,我可以将其作为一个单独的问题.
I also am not sure how to input this information into my stored arraylist that was previously built, but I can make that a separate question if needed.
推荐答案
尝试,
JButton but = new JButton("Popup");
but.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
final JPanel myPanel = new PopupPanel();// Create a separate class extends JPanel
int result = JOptionPane.showConfirmDialog(null, myPanel,
"Please Enter data into both boxes", JOptionPane.OK_CANCEL_OPTION);
if (result == JOptionPane.OK_OPTION) {
}
}
});
这篇关于使用弹出窗口将项目添加到数组列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文