Hi all I'm confused about how to get my button to do something when it is clicked, my understanding after searching is that the following should work:
However im getting an illegal start of of expression for my method header and also a ';' expected at the the last }
Anyone see something I don't?? -
-
aButton.addActionListener(this);
-
-
-
-
public void actionPerformed(ActionEvent e)
-
{
-
String aString = inputArea.getText();
-
if(aString.equals("exit"))
-
{
-
socket.close();
-
System.exit();
-
}
-
else
-
{
-
outStream.println(aString);
-
-
}
-
}
-
-
}
-
12 7579 JosAH 11,448
Recognized Expert MVP
Anyone see something I don't??
Nope, because you're seeing a whole lot of context (where the syntax errors
come from) which we don't see. As it is we can't answer your question.
kind regards,
Jos
Sorry Jos, just didn't want to ask you to read everything if you didn't need to -
-
/*
-
* Main.java
-
*
-
* Created on 28 October 2008, 13:19
-
*
-
* To change this template, choose Tools | Options and locate the template under
-
* the Source Creation and Management node. Right-click the template and choose
-
* Open. You can then make changes to the template in the Source Editor.
-
*/
-
-
package clientside;
-
-
import java.io.*;
-
import java.net.*;
-
import java.util.*;
-
import javax.swing.*;
-
import java.awt.*;
-
-
public class Main implements ActionListener
-
{
-
public static void main(String[] args)
-
{
-
try
-
{
-
JFrame aFrame = new JFrame();
-
aFrame.setSize(500, 500);
-
aFrame.setTitle("Input your message");
-
aFrame.setLayout(new FlowLayout());
-
aFrame.setVisible(true);
-
-
JButton aButton = new JButton();
-
aButton.setText("Press to send message");
-
aButton.setSize(200, 25);
-
aButton.addActionListener(this);
-
aFrame.add(aButton);
-
JButton aButton2 = new JButton();
-
aButton2.setText("Press to clear message");
-
aButton2.setSize(200, 25);
-
aFrame.add(aButton2);
-
aButton2.setVisible(true);
-
JTextField inputArea = new JTextField(50);
-
inputArea.setVisible(true);
-
aFrame.add(inputArea);
-
-
Socket socket = new Socket("192.168.0.6", 4243);
-
PrintStream outStream = new PrintStream(socket.getOutputStream());
-
-
-
public void actionPerformed(ActionEvent e)
-
{
-
String aString = inputArea.getText();
-
if(aString.equals("exit"))
-
{
-
socket.close();
-
System.exit();
-
}
-
else
-
{
-
outStream.println(aString);
-
-
}
-
}
-
-
}
-
catch (Exception anException)
-
{
-
System.out.println("Error: " + anException);
-
}
-
}
-
}
-
-
-
JosAH 11,448
Recognized Expert MVP
You're curly brackets are incorrect; as it is now you're trying to define a method
inside another method (which doesn't work of course). Check them all.
kind regards,
Jos
I had a feeling it was something like that but couldnt find any answers
So i can't have a actionperformed method in a main method?
if not, should i end the main method and have the try/catch in the main method and then have my actionperformed method before the end of the class bracket??
I ve just tried the above and it's giving me a load cannot find symbols so there obviously out of scope, how can i account for this if i can 't have a method in a method?? -
-
/*
-
* Main.java
-
*
-
* Created on 28 October 2008, 13:19
-
*
-
* To change this template, choose Tools | Options and locate the template under
-
* the Source Creation and Management node. Right-click the template and choose
-
* Open. You can then make changes to the template in the Source Editor.
-
*/
-
-
package clientside;
-
-
import java.io.*;
-
import java.net.*;
-
import java.util.*;
-
import javax.swing.*;
-
import java.awt.*;
-
-
public class Main implements ActionListener
-
{
-
public static void main(String[] args)
-
{
-
try
-
{
-
JFrame aFrame = new JFrame();
-
aFrame.setSize(500, 500);
-
aFrame.setTitle("Input your message");
-
aFrame.setLayout(new FlowLayout());
-
aFrame.setVisible(true);
-
-
JButton aButton = new JButton();
-
aButton.setText("Press to send message");
-
aButton.setSize(200, 25);
-
aButton.addActionListener(this);
-
aFrame.add(aButton);
-
JButton aButton2 = new JButton();
-
aButton2.setText("Press to clear message");
-
aButton2.setSize(200, 25);
-
aFrame.add(aButton2);
-
aButton2.setVisible(true);
-
JTextField inputArea = new JTextField(50);
-
inputArea.setVisible(true);
-
aFrame.add(inputArea);
-
-
Socket socket = new Socket("192.168.0.6", 4243);
-
PrintStream outStream = new PrintStream(socket.getOutputStream());
-
}
-
catch (Exception anException)
-
{
-
System.out.println("Error: " + anException);
-
}
-
}
-
-
public void actionPerformed(ActionEvent e)
-
{
-
String aString = inputArea.getText();
-
if(aString.equals("exit"))
-
{
-
socket.close();
-
System.exit();
-
}
-
else
-
{
-
outStream.println(aString);
-
-
}
-
}
-
-
}
-
-
-
JosAH 11,448
Recognized Expert MVP
I had a feeling it was something like that but couldnt find any answers
So i can't have a actionperformed method in a main method?
if not, should i end the main method and have the try/catch in the main method and then have my actionperformed method before the end of the class bracket??
I ve just tried the above and it's giving me a load cannot find symbols so there obviously out of scope, how can i account for this if i can 't have a method in a method??
If you need variables in a method that you also need in another method and you
can't call one method from the other, make those variables private variables, then
they're global to your object or, when you make them static variables, global to the
class you're defining.
Every class has the following structure: -
class Name extends APossibleOtherClass implements PossibleInterfaces {
-
<static variables>
-
<static initialization code>
-
<static methods>
-
<variables>
-
<initialization code>
-
<constructors>
-
<methods>
-
}
-
The <static variables> or <variables> section can be classes or interfaces again.
None of the methods are nested but the order of static/non-static methods/variables
is more or less arbitrary but I do suggest you keep a more or less consistent
order in them.
kind regards,
Jos
So by your kind response Jos, my variables, fram, textfield, button, etc should go after the start of the static method so there accesible everywhere,
Ive tried to put them after public sttaic void main(String[] args) but before the try and also made them private. still same symbol error. I ve also tried other places for my variables without luck.
I really want to complete this gui (it's my first) tonight and i really am trying before asking questions. Could you help me by telling where in the class i should put JTextField inputArea = new JTextField(50); for instance and ill figure out the rest.
Regards
Brendan
right, got the symbol errors sorted but im back to being stuck with the actionperform() and addactionlisten er.
here's my update code -
-
/*
-
* Main.java
-
*
-
* Created on 28 October 2008, 13:19
-
*
-
* To change this template, choose Tools | Options and locate the template under
-
* the Source Creation and Management node. Right-click the template and choose
-
* Open. You can then make changes to the template in the Source Editor.
-
*/
-
-
package clientside;
-
-
import java.io.*;
-
import java.net.*;
-
import java.util.*;
-
import javax.swing.*;
-
import java.awt.*;
-
-
public class Main implements ActionListener
-
{
-
public JFrame aFrame;
-
public JButton aButton;
-
public JButton aButton2;
-
public JTextField inputArea;
-
static Socket socket;
-
static PrintStream outStream;
-
-
public Main()
-
{
-
aFrame = new JFrame();
-
aFrame.setSize(500, 500);
-
aFrame.setTitle("Input your message");
-
aFrame.setLayout(new FlowLayout());
-
aFrame.setVisible(true);
-
-
aButton = new JButton();
-
aButton.setText("Press to send message");
-
aButton.setSize(200, 25);
-
aButton.addActionListener(this);
-
aFrame.add(aButton);
-
-
aButton2 = new JButton();
-
aButton2.setText("Press to clear message");
-
aButton2.setSize(200, 25);
-
aFrame.add(aButton2);
-
aButton2.setVisible(true);
-
-
inputArea = new JTextField(50);
-
inputArea.setVisible(true);
-
aFrame.add(inputArea);
-
}
-
public static void main(String[] args)
-
{
-
try
-
{
-
socket = new Socket("192.168.0.6", 4243);
-
outStream = new PrintStream(socket.getOutputStream());
-
}
-
catch (Exception anException)
-
{
-
System.out.println("Error: " + anException);
-
}
-
}
-
public void actionPerformed(ActionEvent e)
-
{
-
String aString = inputArea.getText();
-
if(aString.equals("exit"))
-
{
-
socket.close();
-
System.exit(1);
-
}
-
else
-
{
-
outStream.println(aString);
-
aString = inputArea.getText();
-
}
-
}
-
-
}
-
-
-
here's the errors :
init:
deps-jar:
Compiling 1 source file to C:\ClientSide\b uild\classes
C:\ClientSide\s rc\clientside\M ain.java:19: cannot find symbol
symbol: class ActionListener
public class Main implements ActionListener
C:\ClientSide\s rc\clientside\M ain.java:64: cannot find symbol
symbol : class ActionEvent
location: class clientside.Main
public void actionPerformed (ActionEvent e)
C:\ClientSide\s rc\clientside\M ain.java:39: addActionListen er(java.awt.eve nt.ActionListen er) in javax.swing.Abs tractButton cannot be applied to (clientside.Mai n)
aButton.addActi onListener(this );
3 errors
JosAH 11,448
Recognized Expert MVP
The classes it couldn't find are in packages you didn't include. See the first article
in this section, titled "Read This First"; it has a link from which you can download
the complete API documentation; download and install it, it has great value.
kind regards,
Jos
Thanks Jos,
Just coming back to paste my succesful code!!!!!!!!!!! ! (in case anyone has similar problems in future) -
-
/*
-
* Main.java
-
*
-
* Created on 28 October 2008, 13:19
-
*
-
* To change this template, choose Tools | Options and locate the template under
-
* the Source Creation and Management node. Right-click the template and choose
-
* Open. You can then make changes to the template in the Source Editor.
-
*/
-
-
package clientside;
-
-
import java.io.*;
-
import java.net.*;
-
import java.util.*;
-
import javax.swing.*;
-
import java.awt.*;
-
import java.awt.event.*;
-
-
-
public class Main implements ActionListener
-
{
-
public static JFrame aFrame;
-
public static JButton aButton;
-
public static JButton aButton2;
-
public static JTextField inputArea;
-
static Socket socket;
-
static PrintStream outStream;
-
-
public Main()
-
{
-
aFrame = new JFrame();
-
aFrame.setSize(500, 500);
-
aFrame.setTitle("Input your message");
-
aFrame.setLayout(new FlowLayout());
-
aFrame.setVisible(true);
-
-
aButton = new JButton();
-
aButton.setText("Press to send message");
-
aButton.setSize(200, 25);
-
aButton.addActionListener(this);
-
aFrame.add(aButton);
-
-
aButton2 = new JButton();
-
aButton2.setText("Press to clear message");
-
aButton2.setSize(200, 25);
-
aFrame.add(aButton2);
-
aButton2.setVisible(true);
-
-
inputArea = new JTextField(50);
-
inputArea.setVisible(true);
-
aFrame.add(inputArea);
-
}
-
public static void main(String[] args)
-
{
-
try
-
{
-
Main aMain = new Main();
-
socket = new Socket("192.168.0.6", 4242);
-
outStream = new PrintStream(socket.getOutputStream());
-
}
-
catch (Exception anException)
-
{
-
System.out.println("Error: " + anException);
-
}
-
}
-
public void actionPerformed(ActionEvent e)
-
{
-
String aString = Main.inputArea.getText();
-
if(aString.equals("exit"))
-
{
-
//socket.close();
-
System.exit(1);
-
}
-
else
-
{
-
Main.outStream.println(aString);
-
aString = Main.inputArea.getText();
-
}
-
}
-
-
}
-
-
-
Now to learn 2 way chat and multi threading!!
Thank you for your help Jos
Regards
Brendan
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Dave Rahardja |
last post by:
OK, so I've gotten into a philosophical disagreement with my colleague at
work. He is a proponent of the Template Method pattern, i.e.:
class foo
{
public:
void bar() { do_bar(); }
protected:
virtual void do_bar() {}
};
|
by: Chris |
last post by:
Hi
I have a scenario where I've created another AppDomain to dynamically load a DLL(s) into. In this newly loaded DLL I want to call a static method on a class. The problem arise is that I have the same class/static method definition statictly linked to my EXE and when I call InvokeMember(...), even though I got the Type from the new AppDomain, it calls the static method that I am staticly linked to and not the static method in the dynamicly...
|
by: daniel.w.gelder |
last post by:
I wrote a template class that takes a function prototype and lets you
store and call a C-level function, like this:
inline string SampleFunction(int, bool) {..}
functor<string (int, bool)> myFunctor = SampleFunction;
string result = myFunctor(7, true);
Works great thanks to the help from this group. Here's the guts so far
for two-arity:
|
by: greenflame |
last post by:
I am trying to make a matrix object. I have given it some properites. I
am trying to add a method. When I call the method by Test.showDims(...)
I want to only enter one input, that is the method by which to do it.
As you can see from the object definition that it corresponds to a
function that takes two inputs. When I try to run the script it does
nothing. So what is wrong and how do I fix it? Here is the script.
function...
|
by: Nick Flandry |
last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my
development environment (Win2K server running IIS 5) and a test environment (also Win2K server
running IIS 5), but fails on IIS 6 running on a Win2003 server.
The web uses Pages derived from a custom class I wrote (which itself derives from Page) to provide
some common functionality. The Page_Load handler the failing webpage starts out like this:
...
| |
by: JohnR |
last post by:
From reading the documentation, this should be a relatively easy thing. I
have an arraylist of custom class instances which I want to search with
an"indexof" where I'm passing an instance if the class where only the
"searched" property has a value. I expected to get the index into the
arraylist where I could then get the entire class instance. However, the
'indexof' is never calling my overloaded, overrides Equals method. Here is
the...
|
by: Mihai Osian |
last post by:
Hi everyone,
Given the code below, can anyone tell me:
a) Is this normal behaviour ?
b) If it is, what is the reason behind it ? I would expect the
A::method(int) to be inherited by B.
Compiler: gcc 4.1, Linux
Thanks,
|
by: Steve Richter |
last post by:
in a generic class, can I code the class so that I can call a static
method of the generic class T?
In the ConvertFrom method of the generic TypeConvert class I want to
write, I have a call to the static Parse method of the conversion
class.
if (InValue is string)
return T.Parse((string)InValue);
else
return base.ConvertFrom(context, culture, InValue);
|
by: allendowney |
last post by:
Hi All.
In a complex inheritance hierarchy, it is sometimes difficult to find
where a
method is defined. I thought it might be possible to get this info
from the
method object itself, but it looks like maybe not. Here is the test
case I tried:
class A(object):
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |