Results 1 to 2 of 2

Thread: Need a runnable Java Application

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Need a runnable Java Application

    I'm in need of a small, short and sweet Java application that will show a window form with a button and when I click on the button it will paint a message on the window. I think it should be something like class MyForm extends Frame implements Runnable but I'm not sure but it needs to run in a Thread.

    Does anyone know if there is something similar to this around here or can someone just give me a simple example.

    Thanks in advance.

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Need a runnable Java Application

    I hope this helps:
    Code:
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    
    public class MyFrame extends JFrame {
    
        private JButton btnPaint;
    
        public MyFrame() {
    	btnPaint = new JButton("Paint");
    	btnPaint.addActionListener(new ActionListener() {
    
    	    public void actionPerformed(ActionEvent e) {
    		Thread th = new Thread(new Runnable() {
    		    public void run() {
    			getGraphics().drawString("My String", 50, 100);
    		    }
    		});
    		th.start();
    	    }
    	});
    	this.add(btnPaint, BorderLayout.NORTH);
    	this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	this.setSize(200, 200);
    	this.setVisible(true);
        }
    
        public static void main(String[] args) {
    	new MyFrame();
        }
    }
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width