Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

adding controls to JFrame

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
This is my program the controls are not getting added please advise me how to add it.


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


public class testing extends JFrame implements Runnable
{
Thread t;
int i, j, x, y;
public volatile boolean run=true;

JButton b1, b2;

public static void main(String args[])
{
testing tst=new testing();
}

public testing()
{
b1=new JButton("stop");
b2=new JButton("start");
JPanel p=new JPanel();
p.add(b1);
p.add(b2);
pack();
setVisible(true);
setSize(800, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

t=new Thread(this);
t.start();
}

public void run()
{
for(i=0;i<210;i+=10)
{
repaint();
try
{
t.sleep(300);
}catch (InterruptedException es)
{}

}


}
public void paint(Graphics g)
{
g.setColor(Color.blue);
g.drawLine(50, 250, 50+i, 250);
g.drawLine(50, 50, 50, 50+i);
}


}
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
using JFrame: before add control : You have to: getContentPane() method and add control .
Ex myFrame.getContentPane().add(myButton);
 
Nirmal JeyaChandra
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
getContentPane().add(b1);
getContentPane().add(b2);

It is not working using the above code
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Please don't post the same question to more than one forum. Let's continue the discussion in the GUI forum, where an answer has already been given.
 
Then YOU must do the pig's work! Read this tiny ad. READ IT!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
    Bookmark Topic Watch Topic
  • New Topic