473,399 Members | 3,656 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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(new FlowLayout());
con.add(btnPush);
btnPush.addActionListener(this);
}

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

}

}
Jul 17 '05 #1
2 2849
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("randy", 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(new FlowLayout());
con.add(btnPush);
btnPush.addActionListener(this);
//added
setSize(100, 100);
//to here
}

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

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

JBlueGrey jApplet = new JBlueGrey();

jApplet.init();

jf.setContentPane(jApplet);
jf.pack();
jf.setVisible(true);
}
//to here
}

In article <aDHkb.30727$gi2.28720@fed1read01>, 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(new FlowLayout());
con.add(btnPush);
btnPush.addActionListener(this);
}

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

}

}

Jul 17 '05 #2
In article <20********************************@frobozz.se>, 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("randy", 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(new FlowLayout());
con.add(btnPush);
btnPush.addActionListener(this);
//added
setSize(100, 100);
//to here
}

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

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

JBlueGrey jApplet = new JBlueGrey();

jApplet.init();

jf.setContentPane(jApplet);
jf.pack();
jf.setVisible(true);
}
//to here
}

In article <aDHkb.30727$gi2.28720@fed1read01>, 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(new FlowLayout());
con.add(btnPush);
btnPush.addActionListener(this);
}

public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if (source == btnPush)
{
Graphics draw = getGraphics();
draw.setFont(firstFont);
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
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...
5
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...
1
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...
13
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
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....
3
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...
1
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...
2
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...
0
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
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,...
0
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...
0
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...
0
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,...
0
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...

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.