0

I'm trying to get the action listener and button called French to change the text in the Output field to say Bonjour but many errors come up in the console when I click the button. I would like to do this in as few lines as possible.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;

public class TranslatorFrame {

public static void main(String[] args) {

JFrame Words = new JFrame("Greetings Translator");
Words.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Did some research to find the line so the program 
//would appear in the center of the screen

Font Font1 = new Font("SansSerif",Font.BOLD,40);
JPanel WorkSpace = new JPanel();
JTextField Instructions = new JTextField("Enter \"Hello\" Here: ");
JTextField Input = new JTextField(); 
JPanel Center = new JPanel();
Center.setBackground(Color.lightGray);
Center.add(Instructions, BorderLayout.EAST);
Center.add(Input, BorderLayout.WEST);
JTextField Output = new JTextField("");
Output.setFont(Font1);
Output.setPreferredSize(new Dimension(195, 100));
JTextField Header = new JTextField();   
Header.setPreferredSize(new Dimension(195, 100));
Input.setPreferredSize(new Dimension(150,50));
Input.setFont(Font1);
Instructions.setPreferredSize(new Dimension(375,50));
Instructions.setBackground(Color.cyan);
//set the back ground color of my Instructions field so that it wouldn't
//look so gray and boring. There don't seem to be many preset colors to choose from. 
Instructions.setFont(Font1);
Instructions.setEditable(false);
JPanel ButtonsArea = new JPanel();
ButtonsArea.setBackground(Color.lightGray);
ButtonsArea.setPreferredSize(new Dimension(300,300));
JButton French = new JButton("French");
French.setPreferredSize(new Dimension(200, 150));
French.setFont(Font1);
French.addActionListener(new ChangeListener());

JButton German = new JButton("German");
German.setPreferredSize(new Dimension(200, 150));
German.setFont(Font1);
JButton Spanish = new JButton("Spanish");
Spanish.setPreferredSize(new Dimension(200, 150));
Spanish.setFont(Font1);
ButtonsArea.add(French, BorderLayout.EAST);
ButtonsArea.add(German, BorderLayout.CENTER);
ButtonsArea.add(Spanish, BorderLayout.WEST);
ButtonsArea.add(Output,BorderLayout.SOUTH);
//Tried many different things to get the buttons and textfields 
//to have space between them but nothing seems to work. 

//Header.setSize(100,100);
Header.setFont(Font1);
Header.setText("Welcome!");
Header.setEditable(false);
WorkSpace.setBackground(Color.LIGHT_GRAY);

class ChangeListener implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent arg0) {

    if (arg0.getSource()==French);
    Output.setText("Bonjour!");
    }}
Words.pack();
Words.setSize(new Dimension(1000, 600));
Words.setLocationRelativeTo(null); 
WorkSpace.add(Header);
Words.add(WorkSpace, BorderLayout.NORTH);
Words.add(Center, BorderLayout.CENTER);
/*Words.add(Instructions, BorderLayout.CENTER);
Words.add(Input, BorderLayout.CENTER);*/
Words.add(ButtonsArea, BorderLayout.SOUTH);
//Words.add(Output, BorderLayout.SOUTH);
Words.setVisible(true);
    }

}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Martin
  • 5
  • 2
  • I assume that `French` and `Output` are outside of the context for `ChangeListener`, so they are undefined... – MadProgrammer May 16 '15 at 01:27
  • 1) Always copy/paste error and exception output! 2) Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently. 3) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! 4) See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson May 16 '15 at 01:36
  • .. 5) `new Font("SansSerif",Font.BOLD,40)` would better be `new Font(Font.SANS_SERIF,Font.BOLD,40)` for compile time checking. – Andrew Thompson May 16 '15 at 01:37

1 Answers1

1
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class TranslatorFrame {

    public static void main(String[] args) {

        JFrame Words = new JFrame("Greetings Translator");
        Words.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // Did some research to find the line so the program
        // would appear in the center of the screen

        Font Font1 = new Font("SansSerif", Font.BOLD, 40);
        JPanel WorkSpace = new JPanel();
        JTextField Instructions = new JTextField("Enter \"Hello\" Here: ");
        JTextField Input = new JTextField();
        JPanel Center = new JPanel();
        Center.setBackground(Color.lightGray);
        Center.add(Instructions, BorderLayout.EAST);
        Center.add(Input, BorderLayout.WEST);
        final JTextField Output = new JTextField("");
        Output.setFont(Font1);
        Output.setPreferredSize(new Dimension(195, 100));
        JTextField Header = new JTextField();
        Header.setPreferredSize(new Dimension(195, 100));
        Input.setPreferredSize(new Dimension(150, 50));
        Input.setFont(Font1);
        Instructions.setPreferredSize(new Dimension(375, 50));
        Instructions.setBackground(Color.cyan);
        // set the back ground color of my Instructions field so that it
        // wouldn't
        // look so gray and boring. There don't seem to be many preset colors to
        // choose from.
        Instructions.setFont(Font1);
        Instructions.setEditable(false);
        JPanel ButtonsArea = new JPanel();
        ButtonsArea.setBackground(Color.lightGray);
        ButtonsArea.setPreferredSize(new Dimension(300, 300));
        final JButton French = new JButton("French");
        French.setPreferredSize(new Dimension(200, 150));
        French.setFont(Font1);
        French.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                if (arg0.getSource() == French)
                    ;
                Output.setText("Bonjour!");

            }
        });

        JButton German = new JButton("German");
        German.setPreferredSize(new Dimension(200, 150));
        German.setFont(Font1);
        JButton Spanish = new JButton("Spanish");
        Spanish.setPreferredSize(new Dimension(200, 150));
        Spanish.setFont(Font1);
        ButtonsArea.add(French, BorderLayout.EAST);
        ButtonsArea.add(German, BorderLayout.CENTER);
        ButtonsArea.add(Spanish, BorderLayout.WEST);
        ButtonsArea.add(Output, BorderLayout.SOUTH);
        // Tried many different things to get the buttons and textfields
        // to have space between them but nothing seems to work.

        // Header.setSize(100,100);
        Header.setFont(Font1);
        Header.setText("Welcome!");
        Header.setEditable(false);
        WorkSpace.setBackground(Color.LIGHT_GRAY);

        Words.pack();
        Words.setSize(new Dimension(1000, 600));
        Words.setLocationRelativeTo(null);
        WorkSpace.add(Header);
        Words.add(WorkSpace, BorderLayout.NORTH);
        Words.add(Center, BorderLayout.CENTER);
        /*
         * Words.add(Instructions, BorderLayout.CENTER); Words.add(Input,
         * BorderLayout.CENTER);
         */
        Words.add(ButtonsArea, BorderLayout.SOUTH);
        // Words.add(Output, BorderLayout.SOUTH);
        Words.setVisible(true);
    }

}
Marsha
  • 187
  • 1
  • 8