0

私は先生のために Java クラスで基本的な GUI アプリケーションに取り組んでいましたが、彼が正式に主題を教え始め、GUI を設定するための適切なオブジェクトと構文を紹介し始めたので、私は自分で答えを探す必要がありました。アプリケーション自体は呼び出されますStringAppsが、テキストフィールドの応答の 1 つをテストしていたときに、コードで要求したメッセージ ダイアログが表示されなかったため、不完全です。次のコードとそれに付随する 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