在他开始正式教授该主题并介绍设置 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;
}
}