从Java动作侦听器返回一个String

时间:2013-06-24 15:34:03

标签: java swing jbutton actionlistener jlabel

此Java应用程序应询问用户名,然后显示“welcome,name”的文本。这是Main.java的代码:

package NameGui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Main
{
    public static void main(String[] args)
    {
        JFrame form = new JFrame();
        FlowLayout layout = new FlowLayout();
        JPanel content = new JPanel();

        JPanel top = new JPanel();
        JLabel title = new JLabel();

        JPanel innerForm = new JPanel();
        JLabel inputLabel = new JLabel();
        JTextField input = new JTextField();

        JPanel bottom = new JPanel();
        JButton submit = new JButton();

        JFrame display = new JFrame();
        JLabel nameDisplay = new JLabel();

        Container formContainer = form.getContentPane();
        Container displayContainer = display.getContentPane();

        title.setText("Welcome! What is your name?");
        inputLabel.setText("Name:");
        submit.setText("Submit");
        form.setTitle("Your name");
        display.setTitle("Your name");
        String name = null;

        input.setText("Name Here");
        submit.addActionListener(
            new SwitchScreen(inputLabel, name, display, form));
        top.add(title);

        innerForm.add(inputLabel);
        innerForm.add(input);

        bottom.add(submit);

        content.add(top);
        content.add(innerForm);
        content.add(bottom);

        formContainer.add(content);
        formContainer.setLayout(layout);

        form.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        form.pack();
        form.setVisible(true);

        displayContainer.add(nameDisplay);

        nameDisplay.setText("Your name: " + Aname);
        display.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        display.pack();
        display.setVisible(false);
    }
}

以下是Action Listener的代码:

package NameGui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;

class SwitchScreen implements ActionListener
{
    JLabel inputLabel;
    String name;
    JFrame display;
    JFrame form;

    SwitchScreen(JLabel inputLabel, String name, JFrame display, JFrame form)
    {
        this.name = name;
        this.inputLabel = inputLabel;
        this.form = form;
        this.display = display;
    }
    public void actionPerformed(ActionEvent ae)
    {
        name = inputLabel.getText();
        form.setVisible(false);
        display.setVisible(true);
    }

}

当我在文本框中输入我的名字,然后我点击提交时,它只是说'Welcome,null'。 actionlistener似乎没有被解雇。

2 个答案:

答案 0 :(得分:0)

我认为你的代码中有一些其他的错误,因为这不会编译,但假设你将它们排序,你需要更新标有"Welcome, " + name的标签。所以不要说name = inputLabel.getText();尝试outputField.setText("Welcome, " + inputLabel.getText());之类的内容 您需要将要显示消息的对象传递给SwitchScreen对象,但这应该有效。

你遇到的另一个问题是你将JLabel而不是JTextField传递给Listener,所以即使它正常工作,显示也会是“欢迎,名字:”

答案 1 :(得分:0)

你做错了。你可以用这样的单帧做到这一点。

  • 我们的课程扩展JFrame并实施ActionListener

    public class MyGUI extends JFrame implements ActionListener
    {
    
        public MyGUI()
        {
            super("My Title");
            // Set the close operation to exit
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            // Add the components to the frame
            addComponents();
            // Pack it, show it in center and make it visible
            pack();
            setLocationRelativeTo(null);
            setVisible(true);
        }
    
    }
    
  • 现在添加UI组件作为变量。

    private JLabel   messageLabel = null;
    private JTextBox textBox      = null;
    private JButton  submitButton = null;
    
  • 现在我们制作addComponents()方法。

    private void addComponents()
    {
        // Set the layout of the frame
        setLayout(new FlowLayout());
        // Create the components
        messageLabel = new JLabel("Enter your name");
        textBox      = new JTextBox(20);
        submitButton = new JButton("Submit");
        // Add the ActionListener to the submit button
        submitButton.addActionListener(this);
        // Add these components to the frame and pack it
        add(messageLabel);
        add(textBox);
        add(submitButton);
        pack();
    }
    
  • 现在实施actionPerformed()

    ActionListener方法
    public void actionPerformed(ActionEvent e)
    {
        // We've attached it to only one button so no need
        // to check which button the user clicked on.
        messageLabel.setText("Welcome " + textBox.getText() + "!");
        // Remove the textBox and submitButton from the frame
        remove(textBox);
        remove(submitButton);
        // Pack the frame and invalidate it
        pack();
        invalidate();
    }
    
  • 最后,main()方法

    public static void main(String[] args)
    {
        new MyGUI();
    }
    

很抱歉答案很长,但希望这会有所帮助。