Jump to content

setLocation()

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
random guy

random guy

    Learning Programmer

  • Members
  • PipPipPip
  • 48 posts
hey i am trying to build the card game klondike (or at least as much of it i can get done as possible before thursday due date)

i want to place cards in certin places in the window and i think the set location command can do that but it doesnt seem to work right for me.

i am adding 2 pictures of cards to the window and i want them to overalp a little just to test if it is working but they just get added side by side. cards show up but in wrong places.

any way to fix this? thanks.


   import javax.swing.*; // windows

   import java.awt.*; // events and stuff

   import java.awt.event.*; // event

   import java.awt.Component.*;

   import javax.swing.JTextPane;


// our class which is a subclass of JFrame and uses actionlistener

    public class Klondike extends JFrame implements ActionListener

   {

      private static final int WIDTH = 700; // width of the window

      private static final int HEIGHT = 500; // height of the window

   

   //component declarations

      private JPanel graphics; // panel for everything

   

   // the default constructor

       public Klondike() 

      {

         graphics = new JPanel();// make the full panel

         

         Icon one = new ImageIcon("cards/2c.gif");

         JLabel display = new JLabel(one);

         graphics.add(display);

         display.setLocation(0,0);

      	

         Icon two = new ImageIcon("cards/3c.gif");

         display = new JLabel(two);

         graphics.add(display);

			display.setLocation(0,20);

        

         setContentPane(graphics);// give our window the graphics panel

      }

     

   // program entry point

       public static void main(String args[])

      {

         Klondike myGUI = new Klondike(); // make a new GUI class

         myGUI.setBackground(Color.green); // set its background to green

         myGUI.setSize(WIDTH, HEIGHT); // set its window size

         myGUI.setVisible(true); // make it visible

         myGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // close the program when hitting the x

      }

   

   // actionlistner for the buttons

       public void actionPerformed(ActionEvent ev)

      {

      }

   }



Hey like something i said? Helped you out? Or you just like supporting the Random Guy?
add to my rep. its quick and easy and definitely wont steal your girlfriend.

#2
kmhosny

kmhosny

    Programmer

  • Members
  • PipPipPipPip
  • 133 posts
Hi Random Guy,
from your code i guess the error is that you didn't set the layout of your GUI and sometimes you must specify the size of the component you are going to add.
so try
setLayout(null);//this method is one of the parent's method so you don't need to write //JFrame.setlayout(null)
display.setSize(20,20);//your free to give the dimension, i m given you an example

the null layout lets you manage the place and the size of your component there is other layouts you can use like "FlowLayout: setLayout(new FlowLayout()); " which puts your components in flow each element added is added behind the previous one.
i hope this solved your problem.
Best Regards,
Karim

#3
gszauer

gszauer

    Programmer

  • Members
  • PipPipPipPip
  • 113 posts
Here is the code you need, i commented it:
(It's also attached)
import javax.swing.*; // Swing Components

import java.awt.*; // Legacy AWT Components

import java.awt.event.*; // Events

import java.awt.Component.*; // Not Shure What you need this for...


public class Klondike extends JFrame implements ActionListener {

  private static final int WIDTH = 700;

  private static final int HEIGHT = 500;

  

  public Klondike() {

    Container pane = this.getContentPane(); // Use a content pane

    pane.setLayout(null); // Set your layout manager to null

    

    ImageIcon one = new ImageIcon("cards/2c.gif"); // Datatype is Image Icon, not Icon

    JLabel display = new JLabel(one);

    display.setLocation(0,0); // Set Location first

    display.setSize(20, 20); // You need to set the size

    pane.add(display); // AFTER the component is set up, add it to the content pane

    

    ImageIcon two = new ImageIcon("cards/3c.gif");

    JLabel display2 = new JLabel(two); // Do not reuse the display variable

    display2.setLocation(0, 20);

    display2.setSize(20, 20);

    pane.add(display2);

    

    // No need to explicitly set the content pane, because you are using

    // the JFrame's getContentPane method

    

    // Set up the window inside the objects constructor

    this.setBackground(Color.green);

    this.setSize(WIDTH, HEIGHT);

    this.setVisible(true);

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  }  

  public static void main(String args[]) {

    Klondike myGUI = new Klondike(); // Create a Klondike Object

      // See end of constructor for window actions

  } 

  public void actionPerformed(ActionEvent ae) {

    // Do funn stuff

  }

}

Attached Files


Edited by gszauer, 25 March 2008 - 12:12 PM.

~Aristotle said:

It is the mark of an educated mind to entertain a tought without accepting it
If my post was helpful, please help me build some rep Posted Image

#4
gszauer

gszauer

    Programmer

  • Members
  • PipPipPipPip
  • 113 posts

kmhosny said:

Hi Random Guy,
from your code i guess the error is that you didn't set the layout of your GUI and sometimes you must specify the size of the component you are going to add.
so try
setLayout(null);//this method is one of the parent's method so you don't need to write //JFrame.setlayout(null)
display.setSize(20,20);//your free to give the dimension, i m given you an example

the null layout lets you manage the place and the size of your component there is other layouts you can use like "FlowLayout: setLayout(new FlowLayout()); " which puts your components in flow each element added is added behind the previous one.
i hope this solved your problem.
Best Regards,
Karim
Actually, if you don't explicitly define a layout manager the compiler will implicitly use FlowLayout
the setLayout() method is optional.

~Aristotle said:

It is the mark of an educated mind to entertain a tought without accepting it
If my post was helpful, please help me build some rep Posted Image

#5
kmhosny

kmhosny

    Programmer

  • Members
  • PipPipPipPip
  • 133 posts
actually i didn't try it without the setLayout() but anyway thanks for the tip gszaur

#6
random guy

random guy

    Learning Programmer

  • Members
  • PipPipPip
  • 48 posts
thanks for the help guys. i did it that way which made cards show up on the screen which was good but when i played the game (it is played by putting in commands in the command line and have stuff show up on the screen.) it wouldnt redraw the changes. only way i can think of doing that is to use paintcomponent, but no cards show up this way now sigh. advice on using one way or the other? etc...

code is attached

EDIT: oh ya sorry the code is a mess, i am trying to get it done as fast as i can.
EDIT: i changed the code back, like i said it doesnt show updates.

Attached Files


Edited by random guy, 27 March 2008 - 06:27 PM.

Hey like something i said? Helped you out? Or you just like supporting the Random Guy?
add to my rep. its quick and easy and definitely wont steal your girlfriend.