2

这个 Java 应用程序应该询问用户姓名,然后显示“欢迎,姓名”的文本。这是我的代码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);
    }

}

当我在文本框中输入我的名字,然后单击提交时,它只是说'欢迎,空'。actionlistener 似乎没有被触发。

4

2 回答 2

0

我认为您的代码中还有其他一些错误,因为这不会编译,但是假设您对这些错误进行了整理,则需要更新标有"Welcome, " + name. 因此,不要说name = inputLabel.getText();尝试类似的东西,outputField.setText("Welcome, " + inputLabel.getText()); 您需要将要显示消息的任何对象传递给 SwitchScreen 对象,但这应该可以。

您遇到的另一个问题是您将 JLabel 而不是 JTextField 传递给侦听器,因此即使它工作正常,显示也会是“欢迎,姓名:”

于 2013-06-24T15:44:58.890 回答
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();
    }
    

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

于 2013-06-24T16:01:28.260 回答