单击它,如何使Jmenuitem打开Jtextfield?

发布于 2025-01-24 20:39:36 字数 2072 浏览 4 评论 0原文

我正在编写允许用户输入,搜索和删除参与者的代码。现在,我有一个带有Jmenuitems的Jmenu的Jframe。当我运行程序并让Jmenuitems打印一些东西时,它可以正常工作。但是,例如,使用AddItem Jmenuitem,单击添加处时,我希望它打开JTEXTFIELD。我没有工作的代码。谁能解释为什么?或提供一个解决方案,何时单击Jmenuitems,Jtextfields将打开?谢谢。

public class MyFrame extends JFrame implements ActionListener{

JMenuBar menuBar;
JMenu fileMenu;
JMenu exitMenu;
JMenuItem addItem;
JMenuItem searchItem;
JMenuItem groupItem;
JMenuItem removeItem;
JMenuItem exitItem;

MyFrame(){
    
    this.setTitle("Swim Lessons");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(250, 250);
    //this.setSize(1436, 810);
    this.setLayout(null);
    
    menuBar = new JMenuBar();
    
    fileMenu = new JMenu("File");
    exitMenu = new JMenu("Exit");
    
    addItem = new JMenuItem("Add Child");
    searchItem = new JMenuItem("Find Child");
    groupItem = new JMenuItem("Find Group");
    removeItem = new JMenuItem("Remove Child");
    exitItem = new JMenuItem("Exit");
    
    
    addItem.addActionListener(this);
    searchItem.addActionListener(this);
    groupItem.addActionListener(this);
    removeItem.addActionListener(this);
    exitItem.addActionListener(this);
    
    fileMenu.add(addItem);
    fileMenu.add(searchItem);
    fileMenu.add(groupItem);
    fileMenu.add(removeItem);
    exitMenu.add(exitItem);
    
    menuBar.add(fileMenu);
    menuBar.add(exitMenu);
    
    this.setJMenuBar(menuBar);
    
    this.setVisible(true);
    
}

@Override
public void actionPerformed(ActionEvent e) {
    if(e.getSource() == addItem) {
        JTextField name = new JTextField();
        name.setPreferredSize(new Dimension(100,100));
        this.add(name);
        this.setVisible(true);
    }
    if(e.getSource() == searchItem) {
        System.out.print("pee ");
    }
    if(e.getSource() == groupItem) {
        System.out.print("beep ");
    }
    if(e.getSource() == removeItem) {
        System.out.print("boop ");
    }
    if(e.getSource() == exitItem) {
        System.exit(0);
    }
}
}

旁注:我确实有我所有的进口和班级头。由于某种原因,此站点并没有让我将它们添加到代码部分。

I am writing code that lets the user enter, search for and remove participants. Right now I have the JFrame that has a JMenu on it with a few JMenuItems. When I ran the program and have the JMenuItems print something, it works fine. But, for example with the addItem JMenuItem, when addItem is clicked, I want it to open up a JTextField. The code I have isn't working. Can anyone explain why? Or offer a solution where when the JMenuItems are clicked JTextFields will open? Thank you.

public class MyFrame extends JFrame implements ActionListener{

JMenuBar menuBar;
JMenu fileMenu;
JMenu exitMenu;
JMenuItem addItem;
JMenuItem searchItem;
JMenuItem groupItem;
JMenuItem removeItem;
JMenuItem exitItem;

MyFrame(){
    
    this.setTitle("Swim Lessons");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(250, 250);
    //this.setSize(1436, 810);
    this.setLayout(null);
    
    menuBar = new JMenuBar();
    
    fileMenu = new JMenu("File");
    exitMenu = new JMenu("Exit");
    
    addItem = new JMenuItem("Add Child");
    searchItem = new JMenuItem("Find Child");
    groupItem = new JMenuItem("Find Group");
    removeItem = new JMenuItem("Remove Child");
    exitItem = new JMenuItem("Exit");
    
    
    addItem.addActionListener(this);
    searchItem.addActionListener(this);
    groupItem.addActionListener(this);
    removeItem.addActionListener(this);
    exitItem.addActionListener(this);
    
    fileMenu.add(addItem);
    fileMenu.add(searchItem);
    fileMenu.add(groupItem);
    fileMenu.add(removeItem);
    exitMenu.add(exitItem);
    
    menuBar.add(fileMenu);
    menuBar.add(exitMenu);
    
    this.setJMenuBar(menuBar);
    
    this.setVisible(true);
    
}

@Override
public void actionPerformed(ActionEvent e) {
    if(e.getSource() == addItem) {
        JTextField name = new JTextField();
        name.setPreferredSize(new Dimension(100,100));
        this.add(name);
        this.setVisible(true);
    }
    if(e.getSource() == searchItem) {
        System.out.print("pee ");
    }
    if(e.getSource() == groupItem) {
        System.out.print("beep ");
    }
    if(e.getSource() == removeItem) {
        System.out.print("boop ");
    }
    if(e.getSource() == exitItem) {
        System.exit(0);
    }
}
}

Side note: I do have all my imports and the class header. This site just isn't letting me add them to the code section for some reason.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我们只是彼此的过ke 2025-01-31 20:39:36

首先查看如何进行对话

if (e.getSource() == addItem) {
    String input = JOptionPane.showInputDialog(this, "Item description", "Add item", JOptionPane.PLAIN_MESSAGE);
    if (input != null) {
        System.out.println("You have entered " + input);
    }
}

t使用null布局,这些将返回困扰您

Start by taking a look at How to Make Dialogs

if (e.getSource() == addItem) {
    String input = JOptionPane.showInputDialog(this, "Item description", "Add item", JOptionPane.PLAIN_MESSAGE);
    if (input != null) {
        System.out.println("You have entered " + input);
    }
}

Don't use null layouts, these will come back to haunt you

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文