Swing GUI 和翻译器问题
Swing GUI and translator issues
所以我有一个 class 项目,我必须在其中创建一个翻译器,用户可以在其中输入英文文本,点击一个按钮,然后得到 Pig Latin translation.I 必须使用Swing GUI、FlowLayout 和 JTextArea,带有用于翻译的单独 JTextArea。我的程序打开了 GUI,它允许我输入一些东西,但是当我按下按钮时,没有任何反应。我浏览了一下,好像我记得将所有内容都添加到 JFrame,但我不明白为什么什么都没有弹出。
// This program will take text that is English and translate it to Pig Latin
//import all the necessary packages for the program
import java.util.Scanner;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
public class PigLatin extends JFrame implements ActionListener{
public static final int WIDTH = 500;
public static final int HEIGHT= 400;
private JButton button;
private JTextArea original;
private JTextArea translation;
private JLabel english;
private JLabel pig;
public static void main(String[]args)
{
PigLatin gui = new PigLatin();
gui.setVisible(true);
/*
Scanner scan = new Scanner("far");
Scanner scan1 = new Scanner("and");
Scanner scan2 = new Scanner("away");
while (scan.hasNext())
{
//prints out original words
System.out.println(scan.next());
System.out.println(scan1.next());
System.out.println(scan2.next());
}
*/
}
public PigLatin()
{ //titles the box, creates size and what to do when 'x' is clicked
super("Pig latin translator");
this.setSize(WIDTH,HEIGHT);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
this.setVisible(true);
//creates frame for everything to be added to
JPanel frame = new JPanel();
//creates labels for text boxes
english = new JLabel("English");
add(english);
//enters in origninal text
original = new JTextArea("Enter text here");
original.setEditable(true);
original.setLineWrap(true);
//adds the text area to the GUI
frame.add(original);
//create text area for translation
translation = new JTextArea();
translation.setEditable(false);
translation.setLineWrap(true);
//adds the text area to the GUI
frame.add(translation);
//adds the frame to the GUI
add(frame);
frame.setVisible(true);
//creates panel for the button
JPanel buttons = new JPanel();
buttons.setLayout(new FlowLayout());
//create translate button
button = new JButton("TRANSLATE TO PIG LATIN");
button.addActionListener(this);
buttons.add(button);
JButton clear = new JButton("Clear");
buttons.add(clear);
//adds the buttons JPanel to the GUI
add(buttons);
}
private int findWordEnd(String text) {
int indexOfSpace = text.indexOf(" ");
if (indexOfSpace == -1) // Happens if there is no space
return text.length();
else
return indexOfSpace;
}
private int findVowel(String vowel)
{
int i;
//looks for vowel
for(i=0; i<vowel.length();i++)
{
if(vowel.charAt(i)=='a'||vowel.charAt(i)=='e'||vowel.charAt(i)=='i'||vowel.charAt(i)=='o'||vowel.charAt(i)=='u')
return i;
}
//if no vowels
return vowel.length();
}
private String englishToPig(String text)
{
String translate = "";
while(!text.equals(""))
{
//finds the first word
int wordEndIndex = findWordEnd(text);
String word = text.substring(0, wordEndIndex);
//finds the start and end in Pig Latin
int vowelIndex = findVowel(word);
String start = word.substring(vowelIndex);
String end = "-"+word.substring(0, vowelIndex)+"ay";
//creates the translation
translate = translate+start+end;
//gets rid of first word, so translation can continue
text = text.substring(wordEndIndex).trim();
}
return translate;
}
public void actionPerformed(ActionEvent e){
if (e.getSource() == "TRANSLATE TO PIG LATIN") {
// Get the English they entered
String text = original.getText();
text = text.trim();
text = text.toLowerCase();
// Display the translation
translation.setText(englishToPig(text));
}
}
}
点击事件的来源是按钮。您感兴趣的是操作命令。
if (e.getActionCommand().equals("TRANSLATE TO PIG LATIN")) {
另请注意 equals()
而不是 ==
。但是,通常不需要对所有内容都使用相同的动作侦听器,然后在侦听器中的源之间找出。你也可以使用匿名 class,当保证来源是你感兴趣的来源时:
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Get the English they entered
String text = original.getText();
text = text.trim();
text = text.toLowerCase();
// Display the translation
translation.setText(englishToPig(text));
}
});
清除按钮同样可以有自己的按钮,因为它们的功能并不真正相关。
所以我有一个 class 项目,我必须在其中创建一个翻译器,用户可以在其中输入英文文本,点击一个按钮,然后得到 Pig Latin translation.I 必须使用Swing GUI、FlowLayout 和 JTextArea,带有用于翻译的单独 JTextArea。我的程序打开了 GUI,它允许我输入一些东西,但是当我按下按钮时,没有任何反应。我浏览了一下,好像我记得将所有内容都添加到 JFrame,但我不明白为什么什么都没有弹出。
// This program will take text that is English and translate it to Pig Latin
//import all the necessary packages for the program
import java.util.Scanner;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
public class PigLatin extends JFrame implements ActionListener{
public static final int WIDTH = 500;
public static final int HEIGHT= 400;
private JButton button;
private JTextArea original;
private JTextArea translation;
private JLabel english;
private JLabel pig;
public static void main(String[]args)
{
PigLatin gui = new PigLatin();
gui.setVisible(true);
/*
Scanner scan = new Scanner("far");
Scanner scan1 = new Scanner("and");
Scanner scan2 = new Scanner("away");
while (scan.hasNext())
{
//prints out original words
System.out.println(scan.next());
System.out.println(scan1.next());
System.out.println(scan2.next());
}
*/
}
public PigLatin()
{ //titles the box, creates size and what to do when 'x' is clicked
super("Pig latin translator");
this.setSize(WIDTH,HEIGHT);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
this.setVisible(true);
//creates frame for everything to be added to
JPanel frame = new JPanel();
//creates labels for text boxes
english = new JLabel("English");
add(english);
//enters in origninal text
original = new JTextArea("Enter text here");
original.setEditable(true);
original.setLineWrap(true);
//adds the text area to the GUI
frame.add(original);
//create text area for translation
translation = new JTextArea();
translation.setEditable(false);
translation.setLineWrap(true);
//adds the text area to the GUI
frame.add(translation);
//adds the frame to the GUI
add(frame);
frame.setVisible(true);
//creates panel for the button
JPanel buttons = new JPanel();
buttons.setLayout(new FlowLayout());
//create translate button
button = new JButton("TRANSLATE TO PIG LATIN");
button.addActionListener(this);
buttons.add(button);
JButton clear = new JButton("Clear");
buttons.add(clear);
//adds the buttons JPanel to the GUI
add(buttons);
}
private int findWordEnd(String text) {
int indexOfSpace = text.indexOf(" ");
if (indexOfSpace == -1) // Happens if there is no space
return text.length();
else
return indexOfSpace;
}
private int findVowel(String vowel)
{
int i;
//looks for vowel
for(i=0; i<vowel.length();i++)
{
if(vowel.charAt(i)=='a'||vowel.charAt(i)=='e'||vowel.charAt(i)=='i'||vowel.charAt(i)=='o'||vowel.charAt(i)=='u')
return i;
}
//if no vowels
return vowel.length();
}
private String englishToPig(String text)
{
String translate = "";
while(!text.equals(""))
{
//finds the first word
int wordEndIndex = findWordEnd(text);
String word = text.substring(0, wordEndIndex);
//finds the start and end in Pig Latin
int vowelIndex = findVowel(word);
String start = word.substring(vowelIndex);
String end = "-"+word.substring(0, vowelIndex)+"ay";
//creates the translation
translate = translate+start+end;
//gets rid of first word, so translation can continue
text = text.substring(wordEndIndex).trim();
}
return translate;
}
public void actionPerformed(ActionEvent e){
if (e.getSource() == "TRANSLATE TO PIG LATIN") {
// Get the English they entered
String text = original.getText();
text = text.trim();
text = text.toLowerCase();
// Display the translation
translation.setText(englishToPig(text));
}
}
}
点击事件的来源是按钮。您感兴趣的是操作命令。
if (e.getActionCommand().equals("TRANSLATE TO PIG LATIN")) {
另请注意 equals()
而不是 ==
。但是,通常不需要对所有内容都使用相同的动作侦听器,然后在侦听器中的源之间找出。你也可以使用匿名 class,当保证来源是你感兴趣的来源时:
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Get the English they entered
String text = original.getText();
text = text.trim();
text = text.toLowerCase();
// Display the translation
translation.setText(englishToPig(text));
}
});
清除按钮同样可以有自己的按钮,因为它们的功能并不真正相关。