How can i get access to program from JFrame?

Sebastian :

I just started with Java and programming. This is my second program and I have a little problem. So, first of all, I wrote a program that can translate words to morse code. Now I want to add JFrame with 3 elements. JTextArea will be used for the English words, JButton will be used to translate and JLabel with translated Morse code.

Problem is that I can't get access to already programmed code. I tried to copy both "for" in ActionListener, but then the program does nothing. So the question is how can I get this translator working in JFrame?

I will really appreciate your advice. I need to learn a lot. :-)

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

    }
}

And there is what i tried to do, there is no Error, but button does nothing:

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

}

}

Butiri Dan :

1) Use the unicode "\u2022 \u2014 " instead of "• — "

2) Add chars = text.getText().toCharArray(); in actionPerformed to set the given text to your chars variable

3) Create the translation translated.append(morse[j]);

4) Set the translation to the label see.setText(translated.toString());

5) Refresh the frame 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();
}

and the frame variable is

public void initComponents() {
   JFrame frame = this;
   this.setTitle("Translator");

replace your chars array with

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=334260&siteId=1