Frage

I have been working on a basic GUI application in my Java class for our teacher before he had started formally teaching the subject and introducing the proper objects and syntax for setting up a GUI, leaving me on my own to scavenge for answers. The application itself is called StringApps but is incomplete, as when I was testing one of the textfield responses it was not giving me a message dialog that I had requested in the code. Here is the following code and the Utility class to go with it:

 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)

Utility 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;
    }
}
War es hilfreich?

Lösung

I think you miss understand how GUI's work. Unlike console applications, they don't run in a linear fashion, they are generally event driven.

The clicks or types something, a series of events are raised and you respond to them.

In you ButtonListener, you respond to the btnReverseString event by updating the UI and checking to see if the clicked source is equal to the button you just created and added...this isn't going to work for two reasons.

The first is, each time actionPerformed you create a new instance of btnConfirm1, meaning that clicked and btnConfirm1 could never be equal and two, the user has can't have clicked both btnReverseString AND btnConfirm1.

Start by taking a look at Creating a GUI with Swing and How to make dialogs, which will allow you to ask questions of the user inline to your running code (which is what it seems you are trying to do ;))

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top