Jbutton监听器没有被触发,为什么?

发布于 2024-11-08 14:47:33 字数 4057 浏览 0 评论 0原文

在尝试功能编码之前,我试图让基本的 GUI 正常工作并设置导航。基本上,我有一个登录表单,可以在用户正确输入登录详细信息时打开我的菜单页面。我有一个 AddProperty 页面,我希望在单击 Jbutton 时打开(这个新页面应该打开,菜单页面应该关闭)。我一直在使用《Head First Java》一书来尝试完成这项任务。

当我单击 J 按钮时,没有任何反应。这可能与我的代码结构有关,因为我是初学者,只是想熟悉代码。感谢您的关注!

这是菜单页面 (NavigationMenu.java):

/*
 * NavigationMenu.java
 *
 * Created on 18 May 2011, 12:56
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package Login;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
 *
 * @author Graeme Pearson
 */
public class NavigationMenu {

    /** Creates a new instance of NavigationMenu */
    public void NavigationMenu() 
    {
        JFrame menu = new JFrame("menuframe");
        menu.setVisible(true);
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        menu.setSize(180,240);
        menu.add(panel);

        menu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JButton AddProperty = new JButton("Add A Property");
        panel.add(AddProperty);

        //AddProperty gui = new AddProperty();
                   //gui.AddProperty();

        JButton CreateNewAccount = new JButton("Create New Account");
        panel.add(CreateNewAccount);
        JButton SearchProperty = new JButton("Search Property");
        panel.add(SearchProperty);
        JButton ViewPropertyDetails = new JButton("View Property");
        panel.add(ViewPropertyDetails);
        JButton Logout = new JButton("Logout");
        panel.add(Logout);

    }     
        public void actionPerformed(ActionEvent e)

        { 

        if (e.getActionCommand().equals("Add A Property")) 

                { 
                   AddProperty gui = new AddProperty();
                   gui.AddProperty();
                }
        //the user pressed "Add A Property"; do something
}
}

这是 AddProperty.java 的内容:

/**
 *
 * @author Graeme
 */
package Login;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.EmptyBorder;

public class AddProperty
{

    public void AddProperty()
    {

        JFrame frame = new JFrame("AddPropertyFrame");
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // having to set sizes of components is rare, and often a sign    
        // of problems with layouts.
        //frame.setSize(800,600);
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20,20));
        // make it big like the original
        panel.setBorder(new EmptyBorder(100,20,100,20));
        frame.add(panel);
        //panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));

        JLabel HouseNumber = new JLabel("A");
        panel.add(HouseNumber);
        JTextField HouseNumber1 = new JTextField(10);
        panel.add(HouseNumber1);

        JLabel HousePrice = new JLabel("B");
        panel.add(HousePrice);
        JTextField HousePrice1 = new JTextField(10);
        panel.add(HousePrice1);

        JLabel HouseType = new JLabel("C");
        panel.add(HouseType);
        JTextField HouseType1 = new JTextField(5);
        panel.add(HouseType1);

        JButton submit = new JButton("Submit");
        panel.add(submit);
        submit.addActionListener(new Action());

        // tell the GUI to assume its natural (minimum) size.
        frame.pack();
    }

    static class Action implements ActionListener{

        @Override
        public void actionPerformed (ActionEvent e)
        {
            // this should probably be a modal JDialog or JOptionPane.
            JFrame frame2 = new JFrame("Submitted");
            frame2.setVisible(true);
            frame2.setSize(200,200);
            JLabel label = new JLabel("You Have Submitted a New Property");
            JPanel panel = new JPanel();
            frame2.add(panel);
            panel.add(label);
        }
    }
}

I'm trying to get my basic GUI working and my navigation set up before attempting the coding for the functionality. Basically I have a login form that work and opens my menu page when the user correctly enters the login details. I have an AddProperty page I wish to open when the click on the Jbutton (this new page should open and the menu page closed). I have been using the Head First Java book to try and complete this task.

When I click on the Jbutton, nothing happens. It might be something to do with the structure of my code as I am a beginner and just trying to familiarize myself with the code. Thanks for looking!

Here's the menu Page (NavigationMenu.java):

/*
 * NavigationMenu.java
 *
 * Created on 18 May 2011, 12:56
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package Login;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
 *
 * @author Graeme Pearson
 */
public class NavigationMenu {

    /** Creates a new instance of NavigationMenu */
    public void NavigationMenu() 
    {
        JFrame menu = new JFrame("menuframe");
        menu.setVisible(true);
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        menu.setSize(180,240);
        menu.add(panel);

        menu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JButton AddProperty = new JButton("Add A Property");
        panel.add(AddProperty);

        //AddProperty gui = new AddProperty();
                   //gui.AddProperty();

        JButton CreateNewAccount = new JButton("Create New Account");
        panel.add(CreateNewAccount);
        JButton SearchProperty = new JButton("Search Property");
        panel.add(SearchProperty);
        JButton ViewPropertyDetails = new JButton("View Property");
        panel.add(ViewPropertyDetails);
        JButton Logout = new JButton("Logout");
        panel.add(Logout);

    }     
        public void actionPerformed(ActionEvent e)

        { 

        if (e.getActionCommand().equals("Add A Property")) 

                { 
                   AddProperty gui = new AddProperty();
                   gui.AddProperty();
                }
        //the user pressed "Add A Property"; do something
}
}

Here's the contents of AddProperty.java:

/**
 *
 * @author Graeme
 */
package Login;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.EmptyBorder;

public class AddProperty
{

    public void AddProperty()
    {

        JFrame frame = new JFrame("AddPropertyFrame");
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // having to set sizes of components is rare, and often a sign    
        // of problems with layouts.
        //frame.setSize(800,600);
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20,20));
        // make it big like the original
        panel.setBorder(new EmptyBorder(100,20,100,20));
        frame.add(panel);
        //panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));

        JLabel HouseNumber = new JLabel("A");
        panel.add(HouseNumber);
        JTextField HouseNumber1 = new JTextField(10);
        panel.add(HouseNumber1);

        JLabel HousePrice = new JLabel("B");
        panel.add(HousePrice);
        JTextField HousePrice1 = new JTextField(10);
        panel.add(HousePrice1);

        JLabel HouseType = new JLabel("C");
        panel.add(HouseType);
        JTextField HouseType1 = new JTextField(5);
        panel.add(HouseType1);

        JButton submit = new JButton("Submit");
        panel.add(submit);
        submit.addActionListener(new Action());

        // tell the GUI to assume its natural (minimum) size.
        frame.pack();
    }

    static class Action implements ActionListener{

        @Override
        public void actionPerformed (ActionEvent e)
        {
            // this should probably be a modal JDialog or JOptionPane.
            JFrame frame2 = new JFrame("Submitted");
            frame2.setVisible(true);
            frame2.setSize(200,200);
            JLabel label = new JLabel("You Have Submitted a New Property");
            JPanel panel = new JPanel();
            frame2.add(panel);
            panel.add(label);
        }
    }
}

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

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

发布评论

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

评论(2

梦与时光遇 2024-11-15 14:47:33

应该是这样

public class NavigationMenu implements ActionListener{

,您应该将侦听器添加到按钮:

AddProperty.addActionListener(this);

It should be

public class NavigationMenu implements ActionListener{

and you should add the listener to the button:

AddProperty.addActionListener(this);
谁与争疯 2024-11-15 14:47:33

首先,您的 NavigationMenu 菜单的构造函数定义错误。

它不应该返回 void。构造函数返回的值

public NavigationMenu()
{
etc...
}

的 AddProperty 类不同

与现在查看其余部分 。 :)

要解决其余问题,您需要为按钮分配一个操作侦听器

AddProperty.addActionListener(this);

,并且 NavigationMenu 必须实现 ActionListener 接口。 IE

public class NavigationMenu 
implements ActionListener

firstly, your constuctor for NavigationMenu menu is wrongly defined.

it should not be returning void. constructors do not return values

public NavigationMenu()
{
etc...
}

same with AddProperty class

looking at the rest now. :)

to fix the rest you need to assign an action listener to the button

AddProperty.addActionListener(this);

and the NavigationMenu must implement the ActionListener inteface. i.e.

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