trouble getting buttons to show up

Discussion in 'Java' started by Bob, Aug 21, 2003.

  1. Bob

    Bob Guest

    why don't the two buttons show up using the code below?

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

    public class Ball extends JApplet implements ActionListener
    {
    public int x = 0, y = 50, dx = 1, dy = 2;
    private int diameter = 50, width = 0, height = 0;
    private JButton up, down;
    private Container c;

    public void init()
    {
    width = getSize().width;
    height = getSize().height;
    up = new JButton("up");
    down = new JButton("down");
    up.addActionListener(this);
    down.addActionListener(this);
    c = this.getContentPane();
    c.setLayout(new FlowLayout());
    c.add(up);
    c.add(down);

    }
    public void paint(Graphics g)
    {
    while (true)
    {

    try{Thread.sleep(15);}
    catch (Exception e){}

    x += dx;
    if (x < 0) dx = 1;
    if (x > width-diameter) dx = -1;
    y += dy;
    if (y < 50) dy = 2;
    if (y > height-diameter) dy = -2;
    g.setColor(this.getBackground());
    g.fillRect(0,45,width,height);
    g.setColor(Color.red);
    g.fillOval(x,y,diameter,diameter);
    }
    }
    public void actionPerformed(ActionEvent e)
    {
    String cmd = e.getActionCommand();
    if (cmd.equals("up"))
    {
    dx++;
    dy++;
    }

    if (cmd.equals("down"))
    {
    dx--;
    dy--;
    }

    }


    Thank You

    }
     
    Bob, Aug 21, 2003
    #1
    1. Advertising

  2. "Bob" <> writes:
    > public void paint(Graphics g)
    > {
    > while (true)
    > {


    You block the event dispatching thread. The list below (didn't I post
    the list already once today?), contains a pointer to the TSC articles.
    There is one explaining the Swing architecture and Swing's painting.
    You might want to read them.

    /Thomas

    Sun's Swing Quick-Start Tutorial:

    http://java.sun.com/docs/books/tutorial/uiswing/mini/index.html

    Sun's Swing Tutorial:

    http://java.sun.com/docs/books/tutorial/uiswing/index.html

    Sun's Swing Connection (TSC) (Swing developer's site):

    http://java.sun.com/products/jfc/tsc/index.html

    The TSC article index (Swing architecture, Swing and threads, painting
    architecture, etc.):

    http://java.sun.com/products/jfc/tsc/articles/

    Sun's well hidden set of Icons for Swing' Metal LnF:

    http://developer.java.sun.com/developer/techDocs/hi/repository/

    More icons:

    http://sourceforge.net/projects/icon-collection/
    (the link to javalobby.org is broken)

    A collection of examples for doing nice things with Swing components
    (some are a little bit outdated):

    http://www2.gol.com/users/tame/swing/examples/SwingExamples.html

    Christian Kaufhold's Java and Swing Know-How:

    http://www.chka.de/

    Marco Schmidt's Java resource page (Java imaging information)

    http://www.geocities.com/marcoschmidt.geo/java.html

    Jalice (Linda Radecke's site):

    - no longer there

    Karsten Lentsch's company web page:

    http://www.jgoodies.com/

    This group's archive at Google:

    http://groups.google.com/groups?group=comp.lang.java.gui

    Java Look and Feel Design Guidelines:

    http://java.sun.com/products/jlf/ed2/book/index.html

    Java Look and Feel Design Guidelines: Advanced Topics:

    http://java.sun.com/products/jlf/at/book/index.html

    Java 2D API:

    The Programmer's Guide
    http://java.sun.com/j2se/1.4.2/docs/guide/2d/spec/j2d-bookTOC.html

    Java 3D API:

    There is a separate newsgroup. see

    news:comp.lang.java.3d
    http://groups.google.com/groups?group=comp.lang.java.3d

    GUI Information in the SDK documentation (commonly
    overlooked):

    See your local SDK installation, or

    Abstract Window Toolkit (AWT)
    http://java.sun.com/j2se/1.4.2/docs/guide/awt/index.html

    Swing
    http://java.sun.com/j2se/1.4.2/docs/guide/swing/index.html

    2D Graphics and Imaging
    http://java.sun.com/j2se/1.4.2/docs/guide/2d/index.html

    Image I/O
    http://java.sun.com/j2se/1.4.2/docs/guide/imageio/index.html

    Print Service
    http://java.sun.com/j2se/1.4.2/docs/guide/jps/index.html

    Input Method Framework
    http://java.sun.com/j2se/1.4.2/docs/guide/imf/index.html

    Accessibility
    http://java.sun.com/j2se/1.4.2/docs/guide/access/index.html

    Drag-and-Drop data
    http://java.sun.com/j2se/1.4.2/docs/guide/dragndrop/index.html

    Swing Examples in the SDK:

    See the directory demo/jfc in your Java SDK installation.

    General

    The Java Tutorial:

    http://java.sun.com/docs/books/tutorial/

    All kinds of tutorials:
    http://developer.java.sun.com/developer/onlineTraining/

    Roedy's Java Glossary:

    http://www.mindprod.com/jgloss/jgloss.html

    The (more and more outdated) Java Programmer's FAQ:
    http://www.afu.com/intro.html

    Making the most out of a newsgroup (ignore the hacker slang):
    http://www.catb.org/~esr/faqs/smart-questions.html

    /Thomas
     
    Thomas Weidenfeller, Aug 21, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roger
    Replies:
    1
    Views:
    649
    GreggTB
    May 20, 2005
  2. =?Utf-8?B?QmVu?=

    Image Buttons/Buttons not responding

    =?Utf-8?B?QmVu?=, Jul 6, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    461
    Teemu Keiski
    Jul 6, 2005
  3. Jeremy Jones

    Datagrid Nav buttons and numeric buttons

    Jeremy Jones, Mar 21, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    633
    Masudur
    Mar 22, 2007
  4. Jonathan N. Little
    Replies:
    3
    Views:
    538
    Andy Dingley
    Apr 2, 2007
  5. Dr. Leff
    Replies:
    3
    Views:
    315
    David Mark
    Oct 15, 2007
Loading...

Share This Page