|
|
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.
|
|

07-05-2008, 10:04 AM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 20
|
|
|
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)
|
|

07-05-2008, 03:26 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: SW MO, USA
Posts: 1,464
|
|
|
Do you have code for this?
|
|

07-05-2008, 08:08 PM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 421
|
|
|
cross-posted in the Sun Java forums, and without posting code there as well.
|
|

07-05-2008, 08:24 PM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 20
|
|
|
Ok I am including the code
|
|

07-05-2008, 08:55 PM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 20
|
|
|
I am trying a alot to add the code but It is showing error
|
|

07-05-2008, 09:06 PM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 20
|
|
|
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
|
|

07-05-2008, 10:34 PM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,189
|
|
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);
}
}
|
|

07-06-2008, 08:53 AM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 20
|
|
|
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
|
|

07-06-2008, 08:58 AM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 20
|
|
|
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.
|
|

07-06-2008, 09:06 AM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,189
|
|
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.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|