どのように私はJFrameのからプログラムへのアクセスを得ることができますか?

セバスチャン:

私は、Javaとプログラミングを始めました。これは私の第二のプログラムであり、私は少し問題を抱えています。だから、最初にすべての、私はモールス信号に単語を翻訳することができ、プログラムを書きました。今、私は3つの要素とのJFrameを追加したいです。JTextAreaには、英語の単語のために使用され、JButtonのは、翻訳されたモールス信号に変換し、JLabelのために使用されます。

問題は、私はすでにプログラムされたコードへのアクセスを得ることができないということです。私はActionListenerの中で「用」の両方をコピーしようとしましたが、その後、プログラムは何もしません。質問があるので、どのように私は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) "\ u2022 \ u2014" の代わりにUnicodeを使用します

2)を追加chars = text.getText().toCharArray();してactionPerformed、あなたの文字変数に指定されたテキストを設定します

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 ", ... }

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=338381&siteId=1