why can't run!

H

hua song

i just learn java
write a applet..
but it can't show what i want...
can anyone help me?
import java.awt.*;
import javax.swing.*;
import java.awt.Event.*;
public class Jj extends JApplet{
public static int a;
public static int b;
public void init()
{
a=Integer.parseInt(getParameter("Value1"));
b=Integer.parseInt(getParameter("Value2"));
Container cp=getContentPane();
Buttonlistener bl=new Buttonlistener();
cp.add(bl);
show();
}
public class FillPanel2 extends JPanel{

private int aa;
private int bb;
FillPanel2(int aa,int bb)
{
this.aa=aa;
this.bb=bb;
}
public void paintComponment(Graphics g)
{
super.paintComponent(g);
g.drawOval(aa,bb,80,30);
}

}
public class FillPanel1 extends JPanel{

private int aa;
private int bb;

FillPanel1(int aa,int bb)
{
this.aa=aa;
this.bb=bb;
}
public void paintComponment(Graphics g)
{
super.paint(g);
g.drawRect(aa,bb,80,30);
}

}
public class Buttonlistener extends JPanel implements ActionListener{
public void ButtonListener(){//构造函数
Button b1=new Button("rect");
Button b2=new Button("oval");
Button b3=new Button("round");
add(b1);
add(b2);
add(b3);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {

String s=((Button)e.getSource()).getLabel() ;
if(s.equals("rect"))
{
FillPanel1 fp =new FillPanel1(Jj.a,Jj.b);
add(fp);
}
if(s.equals("oval"))
{
FillPanel2 fp=new FillPanel2(Jj.a,Jj.b );
add(fp);
}
if(s.equals("round"))
{
FillPanel3 fp=new FillPanel3(Jj.a,Jj.b );
add(fp);
}
}

}
public class FillPanel3 extends JPanel{

private int aa;
private int bb;
public FillPanel3(int a, int b) {
aa=a;
bb=b;
}
public void paintComponent(Graphics g)
{
super.paint(g);
g.drawOval( aa,bb,50,50);
}
}
}
 
A

Andrew Thompson

hua said:
i just learn java
write a applet..

That is a bad combination. Applets are not for beginners.
They are hard to debug, and hard to deploy (get to user).
but it can't show what i want...

What do you want?
can anyone help me?

Try this code instead, look carefully at the comments
as well..

<sscce>
import java.awt.*;
import java.awt.event.*; // required for compiling
import javax.swing.*;
import java.awt.Event.*;

/** Class names should be meaningful, and
'DrawApplet' means nothing to me. */
public class DrawApplet extends JApplet{

public static int a;
public static int b;

public void init() {
a=Integer.parseInt(getParameter("Value1"));
b=Integer.parseInt(getParameter("Value2"));
Container cp=getContentPane();
ButtonListener bl=new ButtonListener();
cp.add(bl);
validate();
// this is an applet, it does not require 'show'.
//show();
}
}

/** The common way to write java class names is
EachWordUpperCase - for methods and attributes
it should usually be firstWordLowerCase() ...

This class/method used the reverse of that convention.
Very confusing. */
class ButtonListener
extends JPanel
implements ActionListener {

public ButtonListener() {//????
setLayout(new GridLayout(0,1));
Button b1=new Button("rect");
Button b2=new Button("oval");
Button b3=new Button("round");
add(b1);
add(b2);
add(b3);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
}

public void actionPerformed(ActionEvent e) {

String s=((Button)e.getSource()).getLabel() ;
if(s.equals("rect"))
{
RectanglePanel p = new
RectanglePanel(DrawApplet.a,DrawApplet.b);
add(p);
}
else if(s.equals("oval"))
{
OvalPanel p = new
OvalPanel(DrawApplet.a,DrawApplet.b );
add(p);
}
else if(s.equals("round"))
{
RoundPanel p = new
RoundPanel(DrawApplet.a,DrawApplet.b );
add(p);
}
validate();
}
}

class RectanglePanel extends JPanel{

private int aa;
private int bb;

public RectanglePanel(int a, int b) {
aa=a;
bb=b;
}

public void paintComponent(Graphics g)
{
g.setColor(Color.green);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(Color.gray);
g.drawRect( aa,bb,50,50);
}
}

class OvalPanel extends JPanel {

private int aa;
private int bb;

OvalPanel(int aa,int bb) {
this.aa=aa;
this.bb=bb;
}

// spelling is important!
//public void paintComponment(Graphics g)
public void paintComponent(Graphics g) {
g.setColor(Color.yellow);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(Color.blue);
g.drawOval(aa,bb,80,30);
}
}

class RoundPanel extends JPanel{

private int aa;
private int bb;

public RoundPanel(int a, int b) {
aa=a;
bb=b;
}

public void paintComponent(Graphics g) {
g.setColor(Color.red);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(Color.black);
g.drawOval( aa,bb,20,10);
}
}
</sscce>

<the HTML...>

<html>
<head>
<title>DrawApplet - Test</title>
</head>
<body>
<applet
code='DrawApplet'
width='400px'
height='300px'>
<param name='Value1' value='10'>
<param name='Value2' value='10'>
</applet>

</body>
</html>

</the HTML...>

This shows on screen, but I rewlised once I saw it,
that I have no idea what you are trying to do..

In any case, an electrical strom is on the way and
that regularly kills my power (and PC) as I better post
this and shut-down without too many more comments..

HTH

Andrew T.
 
H

hua song

thank u ...........thank u !!!
i just want to draw a graphics as the button 's label//
i just change some codes.
but i can only see three buttons.
and no actions as i pressed it..
help me!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top