首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java按钮actionEvent

Java按钮actionEvent
EN

Stack Overflow用户
提问于 2014-05-18 17:15:10
回答 2查看 101关注 0票数 0

很抱歉打扰了大家。

总体问题:我试图打开一个对话框,让用户输入一些东西,然后关闭它

问题:-函数没有被调用(我认为)-主要问题是当我使用调试时,它工作良好,所以我很难追踪这个问题。

我在使用JButtons时遇到了问题,它在调试中工作,但在正常运行时不起作用。这可能是因为我使用了无限循环。网上有人建议我使用SwingUtilities,但这不起作用(至少我不这么认为。

代码语言:javascript
复制
/**
 *
 * @author Deep_Net_Backup
 */
public class butonTest extends JFrame  {
String name;
boolean hasValue;

//name things
private JLabel m_nameLabel;
private JTextField m_name;

//panel
private JPanel pane;

//button
private JButton m_submit;

//action listener for the button submit
class submitListen implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        submit();
        System.out.println("Test");
    }
}

//constructor
public butonTest(){
    //normal values
    name = null;
    hasValue = false;
    //create the defauts
    m_nameLabel = new JLabel("Name:");
    m_name = new JTextField(25);
    pane = new JPanel();
    m_submit = new JButton("Submit");
    m_submit.addActionListener(new submitListen());
    //

    setTitle("Create Cat");
    setSize(300,200);
    setResizable(false);

    //add components
    pane.add(m_nameLabel);
    pane.add(m_name);

    pane.add(m_submit);

    add(pane);
    //last things
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

}

//submit
private void submit()
{
    System.out.println("submit");
    name = m_name.getText();
    hasValue = true;
}

//hasValue
public boolean hasValue()
{
    return(hasValue);

}

//get the text name
public String getName()
{
    return(name);
}

public void close()
{
    setVisible(false);
    dispose();
}

public static void main(String[] args)
{

    /* Test 1
    boolean run = true;
    String ret = new String();
    butonTest lol = new butonTest();

    while(run)
    {
        if(lol.hasValue())
        {
            System.out.println("Done");
            run = false;
            ret = new String(lol.getName());
            lol.close();
        }
    }



    System.out.println(ret);*/

    //Tset 2
    /*
    SwingUtilities.invokeLater(new Runnable(){
        @Override
        public void run() {
            butonTest lol = new butonTest();
            if(lol.hasValue())
            {
                System.out.println(lol.getName());
            }
        }
    });*/

}

}

编辑:它是如何不工作的:当我运行Test时,程序将打印测试并提交,然后它应该将hasValue更改为true。这将(希望)允许if语句运行以打印完成。这是不可能的。

编辑2:我刚刚为进一步测试2张打印添加了几行代码,这似乎解决了问题(但这很糟糕) System.out.println("hasValue“+ hasValue);hasValue()函数System.out.println(”设置为真“)的->;-> submit()函数

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-05-18 17:36:35

你做的事情太复杂了。与其将侦听器作为单独的类,不如将其作为匿名类。这样,您就可以获得外部类(butonTest.this)的句柄,并在它上调用任何您想要的方法。

代码语言:javascript
复制
m_submit.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
        submit();
        System.out.println("Test");
        butonTest.this.close();
    }
});

我不知道你想用无限循环做什么。无论如何,在显示对话框之前,它都会运行到完成。

它将有助于阅读一些关于Swing中的事件处理是如何工作的:)

票数 1
EN

Stack Overflow用户

发布于 2014-05-18 17:36:52

恐怕构造函数butonTest()和submit()方法不在类内(公共类butonTest扩展JFrame)。

你需要让他们进入你的课堂:

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23724691

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档