我自己学习java GUI并试图创建一个简单的翻译器来翻译"你好吗?"到科莫埃斯塔斯?"使用翻译按钮。我的代码中有两个textAreas,我想在textArea1中输入英文句子,并在textArea2中获得结果。试图使翻译按钮像" ="计算器的按钮,但我失败了,也许我仍然不明白如何清楚地使用actionListener和actionPerformed。任何帮助将不胜感激。
我制作了main_Translator.java和Translator.java。如果可能的话,我想合并它们。不知道怎么回事..
//Translator.java
package Translator;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.*;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.border.Border;
import javax.swing.JButton;
import javax.swing.JComboBox;
public class Translator /*implements ActionListener*/{
JFrame frame = new JFrame();
JPanel panel = new JPanel();
public Translator(){
frame.setSize(600,800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setTitle("Translator");
frame.add(panel);
Border border = BorderFactory.createLineBorder(Color.BLACK,2);
panel.setBorder(border);
Color b=new Color(240,215,160);
panel.setBackground(b);
JLabel label1 = new JLabel("Input: ");
Font labelFont1 = new Font("Serif",Font.PLAIN,22);
label1.setFont(labelFont1);
panel.add(label1);
JTextArea textarea1 = new JTextArea(6,32);
panel.add(textarea1);
textarea1.setBackground(Color.WHITE);
Border tborder1 = BorderFactory.createLineBorder(Color.BLUE,1);
textarea1.setBorder(tborder1);
Font font1 = new Font("Serif", Font.PLAIN,22);
textarea1.setFont(font1);
textarea1.setForeground(Color.BLACK);
textarea1.setPreferredSize(new Dimension(2,10));
textarea1.setLineWrap(true);
JLabel label2 = new JLabel("Output: ");
label2.setFont(labelFont1);
panel.add(label2);
JTextArea textarea2 = new JTextArea(6,32);
panel.add(textarea2);
textarea2.setBackground(Color.WHITE);
Border tborder2 = BorderFactory.createLineBorder(Color.BLUE,1);
textarea2.setBorder(tborder2);
textarea2.setFont(font1);
textarea2.setForeground(Color.BLACK);
textarea2.setPreferredSize(new Dimension(2,10));
textarea2.setLineWrap(true);
JComboBox language = new JComboBox();
panel.add(language);
language.setFont(font1);
language.addItem("Español");
JButton transButton = new JButton("Translate");
transButton.setFont(font1);
transButton.setPreferredSize(new Dimension(120,30));
panel.add(transButton);
}
}
这是main_Translator.java。
//main_Translator.java
package Translator;
public class main_Translator {
public static void main(String[] args) {
Translator trans = new Translator();
}
}
//when I press a putton (Greetings)
答案 0 :(得分:0)
如果那是main_Translator.java
的全部内容,您只是想移动主方法,只需将其复制到构造函数下方的Translator.java
即可。它应该完全一样。