Results 1 to 10 of 10
  1. #1
    SANDY_INDIA is offline Member
    Join Date
    Jul 2008
    Posts
    20
    Rep Power
    0

    Default 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)

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,576
    Rep Power
    23

    Default

    Do you have code for this?

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    cross-posted in the Sun Java forums, and without posting code there as well.

  4. #4
    SANDY_INDIA is offline Member
    Join Date
    Jul 2008
    Posts
    20
    Rep Power
    0

    Default

    Ok I am including the code

  5. #5
    SANDY_INDIA is offline Member
    Join Date
    Jul 2008
    Posts
    20
    Rep Power
    0

    Default

    I am trying a alot to add the code but It is showing error

  6. #6
    SANDY_INDIA is offline Member
    Join Date
    Jul 2008
    Posts
    20
    Rep Power
    0

    Default

    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

  7. #7
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java 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 [i]add[/i] 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);
        }
    }

  8. #8
    SANDY_INDIA is offline Member
    Join Date
    Jul 2008
    Posts
    20
    Rep Power
    0

    Default

    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

  9. #9
    SANDY_INDIA is offline Member
    Join Date
    Jul 2008
    Posts
    20
    Rep Power
    0

    Default

    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 08:02 AM.

  10. #10
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    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 08:09 AM.

Similar Threads

  1. How to Fill a Rectangle in Java
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:10 PM
  2. How to Draw a Rectangle in Java
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:09 PM
  3. Rectangle Intersection
    By Gwindow in forum Java 2D
    Replies: 1
    Last Post: 04-24-2008, 03:53 PM
  4. help with rectangle class
    By darkgt in forum New To Java
    Replies: 7
    Last Post: 11-14-2007, 06:19 PM
  5. Rectangle with rounded edges??
    By orchid in forum Java 2D
    Replies: 1
    Last Post: 05-10-2007, 02:31 AM

Posting Permissions

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