Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
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
Forum:
Swing / AWT / SWT
adding a button
budsy remo
Ranch Hand
Posts: 103
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
i've made a very small code and now i want to add a button on top of it but i don't know how . Can somebody help ?
Maybe i am missing an important concept regarding what to add where . So please point me to the right direction
import javax.swing.*; import java.awt.*; import java.awt.geom.*; public class Draw { public static void main(String... args) { EventQueue.invokeLater(new Runnable() { public void run() { CFrame r1=new CFrame(); r1.setDefaultCloseOperation(r1.EXIT_ON_CLOSE); r1.setVisible(true); } }); } } class CFrame extends JFrame { CFrame() { setTitle("DOODOO"); setSize(400,500); JButton b1=new JButton("hellllllllllllllllllllllllloo"); add(b1); DaDrw d1=new DaDrw(); add(d1); } } class DaDrw extends JComponent { public void paintComponent(Graphics g) { Graphics2D g2=(Graphics2D)g; Rectangle2D r2=new Rectangle2D.Double(0,200,200,100); g2.draw(r2); System.out.println("hello"); Line2D p=new Line2D.Double(0,225,400,200); g2.draw(p); Ellipse2D e2=new Ellipse2D.Double(); e2.setFrame(r2); g2.draw(e2); } }
Darryl Burke
Bartender
Posts: 5167
11
I like...
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
A JFrame has a default BorderLayout, and a BorderLayout has 6 regions, to each of which only one component can be added.
http://download.oracle.com/javase/tutorial/uiswing/layout/border.html
If you want to add the button on top of the DaDrw, first give it an appropriate layout and then add the button to it.
http://download.oracle.com/javase/tutorial/uiswing/layout/index.html
If you want to add the button and the DaDrw to the frame, use the default BorderLayout with appropriate constraints, or set a layout of your choice.
luck, db
There are no new questions, but there may be new answers.
Consider Paul's
rocket mass heater
.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
how to highlight(change color) of the button and its neighbours when it is clicked
how to draw line between two buttons by clicking
Sorry, one more thing with AWT
Not to show the ellipse?
How to group these JLabels?
More...