0

我认为我的错误是我的 showGreeting() 方法。具体在试试。当用户按下“获取问候语”按钮时,我希望它显示一个对话框,上面写着“问候,名字 + MI + 姓氏”当用户将字段留空时,我创建的错误出现了,这很好,但我没有了解为什么我的正常问候语不起作用。

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


public class GreetingApp extends JFrame {
    private static final long serialVersionUID = 1L;
    private final JTextField firstNameField,middleNameField,lastNameField;
    private final JButton greetingButton;
    public GreetingApp() {
        super("Greetings");
        this.firstNameField = new JTextField(8);
        this.middleNameField = new JTextField(8);
        this.lastNameField = new JTextField(8);
        this.greetingButton = new JButton("Get Greeting"); 
        greetingButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(final ActionEvent ae){
                showGreeting();
            }
        });
        final Container mainPanel = getContentPane();
        mainPanel.setLayout(new BorderLayout());
        final JPanel inputPanel = new JPanel();
        final JPanel buttonPanel = new JPanel();
        inputPanel.setLayout(new GridLayout(3,3,2,2));
        buttonPanel.setLayout(new BorderLayout());
        JSeparator sep = new JSeparator();
        inputPanel.add(new JLabel("First Name: "),JLabel.LEFT_ALIGNMENT);
        inputPanel.add(firstNameField);
        inputPanel.add(new JLabel("MI: "),JLabel.LEFT_ALIGNMENT);
        inputPanel.add(middleNameField);
        inputPanel.add(new JLabel("Last Name: "),JLabel.LEFT_ALIGNMENT);
        inputPanel.add(lastNameField);
        mainPanel.add(inputPanel,BorderLayout.PAGE_START);
        buttonPanel.add(sep,BorderLayout.PAGE_START);
        buttonPanel.add(greetingButton);
        mainPanel.add(buttonPanel,BorderLayout.PAGE_END);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }
    private String getFullName() throws IllegalStateException{
        if(firstNameField.getText().trim().length() == 0){
            throw new IllegalArgumentException("First name cannot be blank");
        }
        if(middleNameField.getText().trim().length() == 0){
            throw new IllegalArgumentException("Middle name cannot be blank");
        }
        if(lastNameField.getText().trim().length() == 0){
            throw new IllegalArgumentException("Last name cannot be blank");
        }
        return "Greetings, "+this.firstNameField+" "+ this.middleNameField +". "+this.lastNameField+"!";
    }
    private void showGreeting(){

        try{
            String message = getFullName();
            JOptionPane.showMessageDialog(this, message, "Greetings",JOptionPane.PLAIN_MESSAGE);
       }catch(final IllegalArgumentException iae){
           JOptionPane.showMessageDialog(this,
                   iae.getMessage(),
                   "Error",
                   JOptionPane.ERROR_MESSAGE);
       }

    }
    public static void main(String[] args) {
        GreetingApp g = new GreetingApp();
    }

}

编辑:没关系,伙计们知道了,我必须添加一个 .getText() return "Greetings, "+this.firstNameField+" "+ this.middleNameField +". "+this.lastNameField+"!"; 在每个字段之后从它们实际获取文本

4

1 回答 1

0

没关系,伙计们知道了,我必须添加一个 .getText() return "Greetings, "+this.firstNameField+" "+ this.middleNameField +". "+this.lastNameField+"!"; 在每个字段之后从它们实际获取文本

于 2013-04-23T19:03:36.577 回答