在我开始正式教授该主题并介绍用于设置GUI的正确对象和语法之前,我一直在为我们的老师在Java课程中开发一个基本的GUI应用程序,让我自己去寻找答案。应用程序本身称为StringApps
但不完整,因为当我测试其中一个文本字段响应时,它没有给我一个我在代码中请求的消息对话框。以下是代码和实用程序类:
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;
}
}
答案 0 :(得分:0)
我想您错过了解GUI的工作方式。与控制台应用程序不同,它们不以线性方式运行,它们通常是事件驱动的。
点击或输入内容,会引发一系列事件,然后您回复它们。
在您ButtonListener
中,您通过更新用户界面并检查btnReverseString
来源是否与您刚创建并添加的按钮相等来回复clicked
事件...这不会有两个原因。
首先,每次actionPerformed
您创建btnConfirm1
的新实例,意味着clicked
和btnConfirm1
永远不会相等而且只有两个,用户可以没有点击btnReverseString
和btnConfirm1
。
首先查看Creating a GUI with Swing和How to make dialogs,这样您就可以向用户询问内联运行代码的问题(这是您正在尝试做的事情;))