繁体   English   中英

如何从 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 "\• \— " 而不是 "• — "

2) 添加chars = text.getText().toCharArray(); actionPerformed中将给定的文本设置为您的 chars 变量

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");

用你的字符数组替换

String[] morse = {"\u2022 \u2014 ", ... }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM