Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-05-2008, 10:04 AM
Member
 
Join Date: Jul 2008
Posts: 20
SANDY_INDIA is on a distinguished road
How To Add A Jbutton Inside A Rectangle
Can anybody tell me How I can draw a Rectangle and Add a button(inside it) to a same Frame(without Panel). Actually I tried It alot but the problem is , Its only show a rectangle and when I Click on the Frame twice then only button is displaying.

Same Problem occurs in case of other Figures(like line, circle etc)
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-05-2008, 03:26 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SW MO, USA
Posts: 1,464
Norm is on a distinguished road
Do you have code for this?
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-05-2008, 08:08 PM
Senior Member
 
Join Date: Jun 2008
Posts: 421
Fubarable is on a distinguished road
cross-posted in the Sun Java forums, and without posting code there as well.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-05-2008, 08:24 PM
Member
 
Join Date: Jul 2008
Posts: 20
SANDY_INDIA is on a distinguished road
Ok I am including the code
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-05-2008, 08:55 PM
Member
 
Join Date: Jul 2008
Posts: 20
SANDY_INDIA is on a distinguished road
I am trying a alot to add the code but It is showing error
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 07-05-2008, 09:06 PM
Member
 
Join Date: Jul 2008
Posts: 20
SANDY_INDIA is on a distinguished road
Still It showing some error in attachments. Can you send me the desired code???? Or give your email id so that I can mail my program
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 07-05-2008, 10:34 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
Code:
import java.awt.*; import javax.swing.*; public class FramedButton extends JPanel { Rectangle r = new Rectangle(50,40,125,75); public FramedButton() { // Use Absolute positioning: setLayout(null); // Adding components to a "null" layout requires // two things for success: // 1 - add to container with an add method // 2 - their bounds must set. JButton button = new JButton("Button"); add(button); int pad = 5; // Make a "defensive copy" of "r" so we // don't alter it. Rectangle bounds = new Rectangle(r); bounds.grow(-pad, -pad); button.setBounds(bounds); } protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.draw(r); } public static void main(String[] args) { JPanel contentPane = new FramedButton(); // Content panes must be opaque. contentPane.setOpaque(true); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setContentPane(contentPane); f.setSize(300,200); f.setLocation(200,200); f.setVisible(true); } }
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 07-06-2008, 08:53 AM
Member
 
Join Date: Jul 2008
Posts: 20
SANDY_INDIA is on a distinguished road
Thanks for this code. But my problem is different. Actually I am designing a Form and it contains no. of Text Fields, Labels, button. And To distinguish between different areas(Like personal information, Interest etc) I need to draw no. of rectangles and lines so that one area(containing no. of swing components) can be separated from other.

Secondly can you tell me how you have posted your code so that I can Show mine????????
I am facing problem in it.

Reply
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 07-06-2008, 08:58 AM
Member
 
Join Date: Jul 2008
Posts: 20
SANDY_INDIA is on a distinguished road
package JAVAPROJECT;
import javax.imageio.*;
import javax.swing.*;
import java.io.*;
import java.awt.image.*;
import java.awt.*;
import java.awt.event.*;

public class FORM0 extends JFrame implements ActionListener
{

JLabel j1;
JButton b1;
Container con= getContentPane();
Font f;
JTextField tf1;
JPasswordField tf2;
public FORM1 f1;
static FORM0 cls1=null;

public FORM0(String s)
{
super(s);
con.setLayout(null);


j1=new JLabel(new ImageIcon(this.getImage()));
j1.setLocation(0,0);
j1.setSize(1300, 100);
con.add(j1);




j1=new JLabel("USER NAME ");
f=new Font("Helvetica",Font.BOLD,12);
j1.setFont(f);
j1.setForeground(Color.BLACK);
j1.setLocation(610,300);
j1.setSize(200,80);
con.add(j1);


j1=new JLabel("PASSWORD ");
f=new Font("Helvetica",Font.BOLD,12);
j1.setFont(f);
j1.setForeground(Color.BLACK);
j1.setLocation(610,340);
j1.setSize(200,80);
con.add(j1);

//USERNAME TEXTFIELD
tf1=new JTextField();
tf1.setLocation(600,350);
tf1.setSize(100,20);
con.add(tf1);

//PASSWORD TEXTFIELD
tf2=new JPasswordField();
tf2.setLocation(600,390);
tf2.setSize(100,20);
con.add(tf2);

//SUBMIT BUTTON
b1=new JButton("SUBMIT");
b1.setLocation(605,430);
b1.setSize(90,20);
con.add(b1);
b1.addActionListener(this);

//IMAGE ICON
j1=new JLabel(new ImageIcon(this.getImage()));
j1.setLocation(500,350);
j1.setSize(100, 80);
con.add(j1);
setSize(1300,770);
setVisible(true);
}

public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
{
if(tf1.getText().equals("ADMIN") && tf2.getText().equals("ADMIN"))
{
JOptionPane.showMessageDialog(cls1,"Login Successful");
f1=new FORM1("ADMIN");
cls1.setVisible(false);
// JOptionPane.showMessageDialog(cls1,"Login Successful");
}
else if(tf1.getText().equals("") && tf2.getText().equals(""))
{
JOptionPane.showMessageDialog(cls1,"Please Enter Username & Password");
}
else
{
JOptionPane.showMessageDialog(cls1,"Login Failed");
tf1.setText("");
tf2.setText("");
}
}
}


private Image getImage(){
try{
BufferedImage buffImg = ImageIO.read(new File("C:\\Users\\Sandeep\\Documents\\NetBeansProje cts\\CARROT.jpg"));
Image image = buffImg.getScaledInstance(1300,100,Image.SCALE_SMO OTH);
return image;
}catch(IOException ioe){
ioe.printStackTrace();
return null;
}
}

/*public static void main(String ar[])
{
cls1=new FORM0("ADMIN ");
cls1.setResizable(false);
cls1.setSize(1300,770);
cls1.setVisible(true);
}*/
}



Here is my code. I want to draw a three rectangles to separate (image), (Username label,Password Label and Text fields) and (SUBMIT Button). It should look Like a boundry.

Last edited by SANDY_INDIA : 07-06-2008 at 09:02 AM.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 07-06-2008, 09:06 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
1 - see How to Use Borders to get some ideas.
2 - scroll down to the Quick Reply header. You may have to open the division with the button on the right side of the header to expose the Message textArea. Below this are two buttons. Select the one on the right "Go Advanced".
A new, larger textArea will appear/reload under the header "Reply to Thread". Paste your code in this textArea in between these [code] [/code] tags.
Press the left button "Preview Post". When you like what you see in the preview press the left button "Submit Reply".

Last edited by hardwired : 07-06-2008 at 09:09 AM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Fill a Rectangle in Java Java Tip java.awt 0 06-23-2008 12:10 AM
How to Draw a Rectangle in Java Java Tip java.awt 0 06-23-2008 12:09 AM
Rectangle Intersection Gwindow Java 2D 1 04-24-2008 04:53 PM
help with rectangle class darkgt New To Java 7 11-14-2007 07:19 PM
Rectangle with rounded edges?? orchid Java 2D 1 05-10-2007 03:31 AM


All times are GMT +3. The time now is 04:17 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org