0

在他开始正式教授该主题并介绍设置 GUI 的正确对象和语法之前,我一直在我的 Java 课上为我们的老师开发一个基本的 GUI 应用程序,让我自己去寻找答案。应用程序本身被调用StringApps但不完整,因为当我测试其中一个文本字段响应时,它没有给我一个我在代码中请求的消息对话框。这是以下代码和与之配套的 Utility 类:

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

public class StringApps extends JFrame
{
    private JLabel lblIntro;
    private JButton btnReverseString;
    private JButton btnVowelCounter;
    private JButton btnPalindromeTester;
    private JLabel lblReverseString;
    private JLabel lblVowelCounter;
    private JLabel lblPalindromeTester;
    private JTextField txtFldReverseString;
    private JTextField txtFldVowelCounter;
    private JTextField txtFldPalindromeTester;
    private JButton btnConfirm1;
    private JButton btnConfirm2;
    private JButton btnConfirm3;
    private ButtonListener listener;

    public StringApps()
    {
        Container cp = getContentPane();
        setLayout(new FlowLayout());
        setTitle("String Apps");
        lblIntro = new JLabel("Welcome to String Apps");
        btnReverseString = new JButton("String Reverser");
        btnVowelCounter = new JButton("Vowel Counter");
        btnPalindromeTester = new JButton("Palindrome Tester");
        btnReverseString.addActionListener(new ButtonListener());
        btnVowelCounter.addActionListener(new ButtonListener());
        btnPalindromeTester.addActionListener(new ButtonListener());
        cp.add(lblIntro);
        cp.add(btnReverseString);
        cp.add(btnVowelCounter);
        cp.add(btnPalindromeTester);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(600, 400);
    }//StringApps

    public static void main(String[]args)
    {
      StringApps foo = new StringApps();
    }//main

    private class ButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            Container cp = getContentPane();
            Object clicked = e.getSource();
            lblReverseString = new JLabel("Please enter your String here:");
            txtFldReverseString = new JTextField(15);
            btnConfirm1 = new JButton("Okay");
            lblVowelCounter = new JLabel("Please enter your String here:");
            txtFldVowelCounter = new JTextField(15);
            btnConfirm2 = new JButton("Okay");
            lblPalindromeTester = new JLabel("Please enter your String here:");
            txtFldPalindromeTester = new JTextField(15);
            btnConfirm3 = new JButton("Okay");

            if(clicked == btnReverseString)
            {
                cp.add(lblReverseString);
                cp.add(txtFldReverseString);
                cp.add(btnConfirm1);
                if(clicked == btnConfirm1)
                {
                    String fwd = txtFldReverseString.getText();
                    txtFldReverseString.selectAll();
                    String rev = StringUtil.reverseString(fwd);
                    JOptionPane.showMessageDialog(null, ("String Reversed: " + rev));
                }
            }//if
            else if(clicked == btnVowelCounter)
            {

            }
            else if(clicked == btnPalindromeTester)
            {
            }
        }//actionPerformed
    }//ButtonListener
}//StringApps(class)

实用程序类:

public class StringUtil
{
    public static String reverseString(String fwd)
    {
        String rev = "";
        for(int i = fwd.length() - 1; i >= 0; i--)
            rev += fwd.charAt(i);
        return rev;
    }
    public static int vowelCounter(String word)
    {
        int vowels = 0;
        word.toLowerCase();
        for(int i = word.length() - 1; i >= 0; i--)
        {
            if(word.charAt(i) == 'a'|| word.charAt(i) == 'e'|| word.charAt(i) == 'i'||word.charAt(i) == 'o'||word.charAt(i) == 'u')
                vowels++;
        }
        return vowels;
    }
    public static boolean palindromeTest(String word)
    {
        boolean palindrome;
        String rev, fwd, strip = "";
        word.toLowerCase();
        for(int i = 0; i <= word.length() - 1; i++)
            if((word.charAt(i) > 47 && word.charAt(i) < 58)||(word.charAt(i) > 96 && word.charAt(i) < 123))
                strip += word.charAt(i);
        fwd = strip;
        rev = reverseString(fwd);
        if(rev.equals(fwd))
            palindrome = true;
        else
            palindrome = false;
        return palindrome;
    }
}
4

1 回答 1

0

我认为您错过了 GUI 的工作原理。与控制台应用程序不同,它们不以线性方式运行,它们通常是事件驱动的。

单击或键入某些内容,引发一系列事件,然后您对其进行响应。

在 you中,您通过更新 UI 并检查源是否等于您刚刚创建和添加的按钮来ButtonListener响应事件……这不起作用有两个原因。btnReverseStringclicked

第一个是,每次actionPerformed创建 的新实例时btnConfirm1,意味着clickedandbtnConfirm1永远不可能等于和二,用户不能同时单击btnReverseStringAND btnConfirm1

首先看一下使用 Swing 创建 GUI如何制作对话框,这将允许您向正在运行的代码内联的用户提出问题(这似乎是您正在尝试做的事情;))

于 2013-11-09T02:49:28.760 回答