473,797 Members | 3,199 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sysntax error?

Hello all. I am new and I am having a tough time spotting my error here. The
idea is to produce an applet that will have a JButton which I can push to
display the string myName. Later I will make it so that it will chage the
font size and style, but for now I can't even get it to display the String.
It compiles fine but when I launch it in the browser or with the
appletviewer, it shows the button and nothing happens when I click it. Can
you see anything wrong with the way i set this up? Thanks in advance.

import javax.swing.*;
import java.awt.*;
import java.awt.event. *;

public class JBlueGrey extends JApplet implements ActionListener
{
String myName = new String("Randy") ;
JButton btnPush = new JButton("Push") ;
Font firstFont = new Font("Helvetica ", Font.ITALIC, 8);

public void init()
{
Container con = getContentPane( );
con.setLayout(n ew FlowLayout());
con.add(btnPush );
btnPush.addActi onListener(this );
}

public void actionPerformed (ActionEvent e)
{
Object source = e.getSource();
if (source == btnPush)
{
Graphics draw = getGraphics();
draw.setFont(fi rstFont);
draw.drawString (myName, 10, 100);
}

}

}
Jul 17 '05 #1
2 2864
The modified code works.
Added setSize in the JApplets init method.
Added a main method for test, and call init from that method.

Your approach for drawing could be better.
implement a method called
public void paint(Graphics g){
g.drawString("r andy", x, y);
}

and do all your drawing in that method (it is called from the
awt-thread, for all components that are to be drawn).

By implementing paint(..), we are telling awt to do the painting of the
component. paint is overridden to be handled correctly by the
swing-thread.

Don't mix awt and swing.
import javax.swing.*;
import java.awt.*;
import java.awt.event. *;

public class JBlueGrey extends JApplet implements ActionListener
{
String myName = new String("Randy") ;
JButton btnPush = new JButton("Push") ;
Font firstFont = new Font("Helvetica ", Font.ITALIC, 8);

public void init()
{
Container con = getContentPane( );
con.setLayout(n ew FlowLayout());
con.add(btnPush );
btnPush.addActi onListener(this );
//added
setSize(100, 100);
//to here
}

public void actionPerformed (ActionEvent e)
{
Object source = e.getSource();
if (source == btnPush)
{
Graphics draw = getGraphics();
draw.setFont(fi rstFont);
draw.drawString (myName, 10, 100);
}

}
//added
public static void main(String[] args){
JFrame jf = new JFrame("etst");

JBlueGrey jApplet = new JBlueGrey();

jApplet.init();

jf.setContentPa ne(jApplet);
jf.pack();
jf.setVisible(t rue);
}
//to here
}

In article <aDHkb.30727$gi 2.28720@fed1rea d01>, nu b
<yr******@yahoo .com> wrote:
import javax.swing.*;
import java.awt.*;
import java.awt.event. *;

public class JBlueGrey extends JApplet implements ActionListener
{
String myName = new String("Randy") ;
JButton btnPush = new JButton("Push") ;
Font firstFont = new Font("Helvetica ", Font.ITALIC, 8);

public void init()
{
Container con = getContentPane( );
con.setLayout(n ew FlowLayout());
con.add(btnPush );
btnPush.addActi onListener(this );
}

public void actionPerformed (ActionEvent e)
{
Object source = e.getSource();
if (source == btnPush)
{
Graphics draw = getGraphics();
draw.setFont(fi rstFont);
draw.drawString (myName, 10, 100);
}

}

}

Jul 17 '05 #2
In article <20************ *************** *****@frobozz.s e>, Folke
Bengtsson <Fo************ *@frobozz.se> wrote:
The modified code works.
Added setSize in the JApplets init method.
Added a main method for test, and call init from that method.

Your approach for drawing could be better.
implement a method called
public void paint(Graphics g){ hmm...sorry.. should probably be
public void paintComponent( Graphics g){..}

I think I'm mixing awt and swing.
g.drawString("r andy", x, y);
}

and do all your drawing in that method (it is called from the
awt-thread, for all components that are to be drawn).

By implementing paint(..), we are telling awt to do the painting of the
component. paint is overridden to be handled correctly by the
swing-thread.

Don't mix awt and swing.
import javax.swing.*;
import java.awt.*;
import java.awt.event. *;

public class JBlueGrey extends JApplet implements ActionListener
{
String myName = new String("Randy") ;
JButton btnPush = new JButton("Push") ;
Font firstFont = new Font("Helvetica ", Font.ITALIC, 8);

public void init()
{
Container con = getContentPane( );
con.setLayout(n ew FlowLayout());
con.add(btnPush );
btnPush.addActi onListener(this );
//added
setSize(100, 100);
//to here
}

public void actionPerformed (ActionEvent e)
{
Object source = e.getSource();
if (source == btnPush)
{
Graphics draw = getGraphics();
draw.setFont(fi rstFont);
draw.drawString (myName, 10, 100);
}

}
//added
public static void main(String[] args){
JFrame jf = new JFrame("etst");

JBlueGrey jApplet = new JBlueGrey();

jApplet.init();

jf.setContentPa ne(jApplet);
jf.pack();
jf.setVisible(t rue);
}
//to here
}

In article <aDHkb.30727$gi 2.28720@fed1rea d01>, nu b
<yr******@yahoo .com> wrote:
import javax.swing.*;
import java.awt.*;
import java.awt.event. *;

public class JBlueGrey extends JApplet implements ActionListener
{
String myName = new String("Randy") ;
JButton btnPush = new JButton("Push") ;
Font firstFont = new Font("Helvetica ", Font.ITALIC, 8);

public void init()
{
Container con = getContentPane( );
con.setLayout(n ew FlowLayout());
con.add(btnPush );
btnPush.addActi onListener(this );
}

public void actionPerformed (ActionEvent e)
{
Object source = e.getSource();
if (source == btnPush)
{
Graphics draw = getGraphics();
draw.setFont(fi rstFont);
draw.drawString (myName, 10, 100);
}

}

}

Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4382
by: AIM | last post by:
Error in msvc in building inheritance.obj to build hello.pyd Hello, I am trying to build the boost 1.31.0 sample extension hello.cpp. I can not compile the file inheritance.cpp because the two files containing some templates: adjacency_list.hpp and mem_fn.hpp can not compile. Does anyone have any solutions?
5
16261
by: Tony Wright | last post by:
Hi, I am having a problem installing an msi for a web site. The error message I am getting is: "The specified path 'http://mipdev05/features/Fas2' is unavailable. The Internet Information Server might not be running or the path exists and is redirected to another machine. Please check the status of this virtual directory in the Internet Services Manager."
1
8419
by: Aravind | last post by:
we have two files: 1. rc4.c (defines one function "create_pin()") 2. MyImpl.c(calling the function "create_pin()"),This implements JNI method. 1.When I am trying to create .dll file with one file rc4.obj(rc4.c),it is creating the .dll file without any error. Command : ILINK32 rc4.obj 2.But,when we are trying to create .dll file with two .obj files with following errors.
13
6621
by: deko | last post by:
I use this convention frequently: Exit_Here: Exit Sub HandleErr: Select Case Err.Number Case 3163 Resume Next Case 3376 Resume Next
7
5032
by: p | last post by:
WE had a Crystal 8 WebApp using vs 2002 which we upgraded to VS2003. I also have Crystal 9 pro on my development machine. The web app runs fine on my dev machine but am having problems deploying. I created the websetup and built the MSI, have the bundled version. Copied to webserver and ran Websetup.msi. Said I had to remove old version, which I did, then reran WebSetup.msi and keeps giving me this error. "The installer was interrupted...
3
1095
by: Rudy | last post by:
Hello All! I'm coding my UPDATE button on my data grid. Getting a syntax error, can't seen to find it. Hope you can help Thanks!!! Rudy Public Sub grdProducts_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
1
1532
by: nkosinathi | last post by:
Hi Guys im trying to write an import class where i can import information from .CSV file into my database. I am getting the following error Errors: You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 PHP Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in...
2
19496
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I will be writing this article intended for those who are in the same level, or maybe lower, of my technical knowledge. I would be using layman's words, or maybe, my own words as how I understand them, hoping, you will understand it the same way that...
0
2897
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I will be writing this article intended for those who are in the same level, or maybe lower, of my technical knowledge. I would be using layman's words, or maybe, my own words as how I understand them, hoping, you will understand it the same way that...
0
9537
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,...
0
10469
Oralloy
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...
0
10246
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10209
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,...
0
9066
agi2029
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...
1
7560
isladogs
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...
1
4135
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3750
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2934
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.