Jump to content

Swing: Putting a image at the NORTH CENTER

- - - - -

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

#1
gammaman

gammaman

    Learning Programmer

  • Members
  • PipPipPip
  • 71 posts
hello, I am taking my first go at Swing and I am trying to learn the basics.
I want to put an Image at the NORTH CENTER. As of now it is at the north, but I cannot seem to center it. Can someone please show me how to center the north image.


/*

 * To change this template, choose Tools | Templates

 * and open the template in the editor.

 */




/**

 *

 * @author Matt

 */


import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class mario {


    /**

     * @param args the command line arguments

     */

    public static void main(String[] args) {

        // TODO code application logic here

        EventQueue.invokeLater(new Runnable()

        {

            public void run()

            {

                QuizFrame frame = new QuizFrame();

                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                frame.setVisible(true);

            }        

        });

    }


}


class QuizFrame extends JFrame

{

    public QuizFrame()

    {

        //size the frame

        setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);

        //create Icon object and set its location

        ImageIcon icon = new ImageIcon("C:\\users\\Matt\\Documents\\mario2.gif");

        //create a label

        JLabel label    =  new JLabel();

        //add the icon to the label

        label.setIcon(icon);

        JButton button1 = new JButton("one");  //create button

        JButton button2 = new JButton("two");  //create button

        JButton button3 = new JButton("three");//create a button

        JButton button4 = new JButton("four"); //create a button

        

        //create a panel

        QuizPanel = new JPanel();

        //add the label to the north border of the panel

       [COLOR="Red"] add(label,BorderLayout.NORTH);  //I want this to be NORTH CENTER[/COLOR]

        

        QuizPanel.add(button1);

        QuizPanel.add(button2);

        QuizPanel.add(button3);

        QuizPanel.add(button4);

        add(QuizPanel,BorderLayout.SOUTH);

        

        QuizLabel = new JLabel("This is a test",JLabel.CENTER); 

        add(QuizLabel,BorderLayout.CENTER);

        

        

    } 

    private JPanel QuizPanel;

    private JLabel QuizLabel;

    private static final int DEFAULT_WIDTH = 300;

    private static final int DEFAULT_HEIGHT = 200;

}        



#2
Turk4n

Turk4n

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,847 posts
BorderLayout (Java 2 Platform SE v1.4.2)
You would need to change your layout to something else, I at most times use no layout to manage the position of my components where I want to...
So, read here and try get a understanding.
Cheers !
Posted Image