【发布时间】:2020-03-20 10:41:31
【问题描述】:
我刚开始接触 Java 和编程。这是我的第二个程序,我有一个小问题。所以,首先,我编写了一个可以将单词翻译成摩尔斯电码的程序。现在我想添加带有 3 个元素的 JFrame。 JTextArea 将用于英语单词,JButton 将用于翻译,JLabel 将用于翻译摩尔斯电码。
问题是我无法访问已编程的代码。我试图在 ActionListener 中复制两个“for”,但随后程序什么也不做。所以问题是我怎样才能让这个翻译器在 JFrame 中工作?
非常感谢您的建议。我需要学习很多东西。 :-)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Main extends JFrame{
public Main(){
initComponents();
}
public void initComponents(){
this.setTitle("Translator");
this.setLocationRelativeTo(null);
this.getContentPane();
this.add(panel);
panel.add(text);
panel.add(go);
panel.add(see);
go.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String toTrans = text.getText();
System.out.println(toTrans);
//There I tried to copy my both "for"
System.out.println(see.getText());
}
});
this.pack();
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
JPanel panel = new JPanel();
JTextArea text = new JTextArea(1, 10);
JButton go = new JButton("Translate");
JLabel see = new JLabel("");
public static void main(String[] args) {
String toTranslate = "test".toLowerCase();
char[] english = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'l','m', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
'v', 'w', 'x', 'y', 'z'};
String[] morse = {"• — ", "— • • • ", "— • — •", "— • • ", "•", "• • — •", "— — •", "• • • •",
"• •", "• — — —", "— • —", "• — • •","— —", "— •", "— — —", "• — — •",
"— — • —", "• — •", "• • •", "—", "• • —", "• • • —", "• — —", "— • • —",
"— • — —", "— — • •"};
char[] chars = toTranslate.toCharArray();
String translated = "";
for(int i = 0; i < chars.length; i++)
for (int j = 0; j < english.length; j++)
if (english[j] == chars[i]){
translated = toTranslate + morse[j] + "";
}
System.out.println(toTranslate);
new Main().setVisible(true);
}
}
还有我试图做的,没有错误,但按钮什么也没做:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Main extends JFrame{
public Main(){
initComponents();
}
public void initComponents(){
this.setTitle("Translator");
this.setLocationRelativeTo(null);
this.getContentPane();
this.add(panel);
panel.add(text);
panel.add(go);
panel.add(see);
go.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String toTrans = text.getText();
System.out.println(toTrans);
for(int i = 0; i < chars.length; i++)
for (int j = 0; j < english.length; j++)
if (english[j] == chars[i]){
see.setText(morse[j] + "");
}
System.out.println(see.getText());
}
});
this.pack();
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
JPanel panel = new JPanel();
JTextArea text = new JTextArea(1, 10);
JButton go = new JButton("Translate");
JLabel see = new JLabel("");
char[] english = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'l','m', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
'v', 'w', 'x', 'y', 'z'};
String[] morse = {"• — ", "— • • • ", "— • — •", "— • • ", "•", "• • — •", "— — •", "• • • •",
"• •", "• — — —", "— • —", "• — • •","— —", "— •", "— — —", "• — — •",
"— — • —", "• — •", "• • •", "—", "• • —", "• • • —", "• — —", "— • • —",
"— • — —", "— — • •"};
char[] chars = text.getText().toCharArray();
public static void main(String[] args) {
new Main().setVisible(true);
}
}
【问题讨论】:
-
您尝试在 actionperformed 方法中输入的代码是什么,当您将它放在那里时出现的错误是什么
-
我试图像这样添加到动作监听器中:'for(int i = 0; i
-
有什么问题?,什么是错误,发布错误,在问题本身,还有这段代码
-
已编辑,很抱歉,感谢您的帮助。
-
你定义english和char数组的地方,也请贴出错误
标签: java swing for-loop jframe