我怎样才能从 JFrame 访问程序?
How can i get access to program from JFrame?
我刚开始学习 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);
}
}
1) 使用 unicode "\u2022 \u2014 " 代替 "• — "
2) 在 actionPerformed
中添加 chars = text.getText().toCharArray();
以将给定文本设置为您的字符变量
3) 创建翻译 translated.append(morse[j]);
4) 将翻译设置为标签 see.setText(translated.toString());
5)刷新帧frame.pack();
@Override
public void actionPerformed(ActionEvent e) {
chars = text.getText().toCharArray();
StringBuilder translated = new StringBuilder();
System.out.println(chars);
for (int i = 0; i < chars.length; i++)
for (int j = 0; j < english.length; j++)
if (english[j] == chars[i]) {
translated.append(morse[j]);
}
see.setText(translated.toString());
System.out.println(see.getText());
frame.pack();
}
并且帧变量是
public void initComponents() {
JFrame frame = this;
this.setTitle("Translator");
将您的 chars 数组替换为
String[] morse = {"\u2022 \u2014 ", ... }
我刚开始学习 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);
}
}
1) 使用 unicode "\u2022 \u2014 " 代替 "• — "
2) 在 actionPerformed
中添加 chars = text.getText().toCharArray();
以将给定文本设置为您的字符变量
3) 创建翻译 translated.append(morse[j]);
4) 将翻译设置为标签 see.setText(translated.toString());
5)刷新帧frame.pack();
@Override
public void actionPerformed(ActionEvent e) {
chars = text.getText().toCharArray();
StringBuilder translated = new StringBuilder();
System.out.println(chars);
for (int i = 0; i < chars.length; i++)
for (int j = 0; j < english.length; j++)
if (english[j] == chars[i]) {
translated.append(morse[j]);
}
see.setText(translated.toString());
System.out.println(see.getText());
frame.pack();
}
并且帧变量是
public void initComponents() {
JFrame frame = this;
this.setTitle("Translator");
将您的 chars 数组替换为
String[] morse = {"\u2022 \u2014 ", ... }