d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Halp With Error Message > Simple Java Program
Add Reply New Topic New Poll
Member
Posts: 24,584
Joined: Jan 16 2006
Gold: 22,346.68
Nov 5 2012 08:23pm
The program is simple, my instructions are as follows:
Write a GUI application that translates the Latin words to English. The window should have three buttons, one for each latin word. When the user clicks a button, the program displays the English translation in a label.

My error message reads:
Lab7.java:13: error: invalid method declaration; return type required
public LatinTranslatorGUI()
^
1 error

And finally, my code.
Code
  import javax.swing.*;
  import java.awt.event.*;

  public class Lab7 extends JFrame
  {
     private JButton button1;
     private JButton button2;
     private JButton button3;
     private JPanel panel;
     private final int WINDOW_WIDTH = 320;
     private final int WINDOW_HEIGHT = 70;
 
     public LatinTranslatorGUI()
     {
        super("Latin Translator");
        setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        setDefaultCLoseOperation(JFrame.EXIT_ON_CLOSE);
     
        button1 = new JButton("Sinister");
        button2 = new JButton("Dexter");
        button3 = new JButton("Medium");
     
        button1.addActionListener(new ButtonListener());
        button2.addActionListener(new ButtonListener());
        button3.addActionListener(new ButtonListener());
     
        panel = new JPanel();
        panel.add(button1);
        panel.add(button2);
        panel.add(button3);
        add(panel);
        setVisible(true);
     }

     private class ButtonListener implements ActionListener
     {
        public void actionPerformed(ActionEvent e)
        {
           if (e.getSource() == button1)
              JOptionPane.showMessageDialog(null, "Left");
           else if (e.getSource() == button2)
              JOptionPane.showMessageDialog(null, "Right");
           else if (e.getSource() == button3)
              JOptionPane.showMessageDialog(null, "Center");
       
        }
     
        public static void main(String[] args)
        {
           LatinTranslatorGUI latinTranslate = new LatinTranslatorGUI();
        }
     
     }
  }
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 5 2012 08:34pm
public LatinTranslatorGUI()

that's a constructor, not a method. constructors need to have the same name as your class. So you have a few options:
1) change it to a method
2) rename the constructor
3) rename the class
Member
Posts: 24,584
Joined: Jan 16 2006
Gold: 22,346.68
Nov 5 2012 08:45pm
I'm dumb, I posted the wrong code and error....I can't get it to auto end the run when I close the box. Here is my code and error:
Code
import javax.swing.*;
import java.awt.event.*;

public class Lab7 extends JFrame
{
private JButton button1;
private JButton button2;
private JButton button3;
private JPanel panel;
private final int WINDOW_WIDTH = 320;
private final int WINDOW_HEIGHT = 70;

public Lab7()
{
super("Latin Translator");
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setDefaultCLoseOperation(JFrame.EXIT_ON_CLOSE);

button1 = new JButton("Sinister");
button2 = new JButton("Dexter");
button3 = new JButton("Medium");

button1.addActionListener(new ButtonListener());
button2.addActionListener(new ButtonListener());
button3.addActionListener(new ButtonListener());

panel = new JPanel();
panel.add(button1);
panel.add(button2);
panel.add(button3);
add(panel);
setVisible(true);
}
private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == button1)
JOptionPane.showMessageDialog(null, "Left");
else if (e.getSource() == button2)
JOptionPane.showMessageDialog(null, "Right");
else if (e.getSource() == button3)
JOptionPane.showMessageDialog(null, "Center");

}
}

public static void main(String[] args)
{
Lab7 latinTranslate = new Lab7();
}

}



Lab7.java:18: error: cannot find symbol
setDefaultCLoseOperation(JFrame.EXIT_ON_CLOSE);
^
symbol: method setDefaultCLoseOperation(int)
location: class Lab7
1 error

LMFAO LOL nevermind got it...had the CL capped in CloseOperation

This post was edited by Morbs on Nov 5 2012 08:48pm
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll