www.webdeveloper.com
Results 1 to 5 of 5

Thread: Resizing a window

  1. #1
    Join Date
    Oct 2004
    Posts
    8

    Resizing a window

    I'm doing an assignment for class it's a simple thing basically it's a window where you put in the RGB values to color the window and put two numbers (in the same first two fields) to set the window size. My problem is that my resize works when I put in values smaller than the default which I set to setSize(500,200). My second problem is that the window doesn't resize all the way. Only the area which is seen becomes smaller any ideas?

    Here's my code:

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

    public class Y extends JFrame implements ActionListener
    {
    JPanel p;
    JTextField n1, n2, n3;
    JButton b1,b2,b3;
    JLabel L1,L2,L3;
    int num1, num2, num3;


    public Y()
    {
    p = new JPanel();
    this.getContentPane().add(p);

    L1=new JLabel("Red"); p.add(L1);
    n1=new JTextField(10); p.add(n1);
    L2=new JLabel("Green"); p.add(L2);
    n2=new JTextField(10); p.add(n2);
    L3=new JLabel("Blue"); p.add(L3);
    n3=new JTextField(10); p.add(n3);
    b1=new JButton("colorMe"); p.add(b1);
    b2=new JButton("reSize"); p.add(b2);
    b3=new JButton("spawn"); p.add(b3);

    b1.addActionListener(this);
    b2.addActionListener(this);
    b3.addActionListener(this);
    }

    public void actionPerformed (ActionEvent bert)
    {
    if (bert.getSource()==b1)
    {
    num1 = Integer.parseInt(n1.getText());
    num2 = Integer.parseInt(n2.getText());
    num3 = Integer.parseInt(n3.getText());
    Color c = new Color (num1, num2, num3);
    p.setBackground(c);
    }



    if (bert.getSource()==b2)
    {
    num1 = Integer.parseInt(n1.getText());
    num2 = Integer.parseInt(n2.getText());
    p.setSize(num1,num2);
    //p.setSize(100, 200);
    }
    if (bert.getSource()==b3)
    {
    Y m = new Y();

    m.setSize(500,200);
    m.setVisible(true);
    MyInput.readString();
    m.pack();
    }
    }


    }

  2. #2
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,120
    this line of code
    p.setSize(num1,num2);
    needs rework.
    See you are changeing the size of JPanel where as JPanel is part of the container, which is part of a JFrame.
    The solution is to change the size of the JFrame....
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

  3. #3
    Join Date
    Oct 2004
    Posts
    8
    how do I refer to the JFrame should I declare it a variable?

  4. #4
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,120
    you can use "this" keyword in this case(since you are extending JFrame already...
    this.setSize() will work
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

  5. #5
    Join Date
    Oct 2004
    Posts
    8
    thanks that worked perfectly, I appreciate it. Another question, the third part of this application is supposed to create a new instance of the window hence the name of the button "spawn". I was thinking that I have to create a new instance of the class but it creates a new window that's blank and it kinda freezes the screen. here's the part of my code which is for the spawn button:

    if (bert.getSource()==b3)
    {
    Y m = new Y();
    m.setSize(500,200);
    m.setVisible(true);
    MyInput.readString();
    m.pack();
    }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles