Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: setAlignment have no effect

  1. #1
    Junior Member
    Join Date
    Feb 2022
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default setAlignment have no effect

    I'm trying to make a game with a menu and therefor I'm using the setAlignment which seem to have no effect.

    (I'm not sure how I attach images so it might not run.



     
    package fold;
    import javax.swing.*;
    //Pakke der hjælper med at lave vinduet
    import javax.swing.JFrame;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.JLabel;
     
    import javax.swing.ImageIcon;
     
    public class GameFrame extends JFrame {
        // CENTER regions størrelse
        static final int SCREEN_WIDTH = 2296;
        static final int SCREEN_HEIGHT = 1280;
     
        // De forskellige JPanels
        JLabel topMenu;
        JLabel mineCounter1, mineCounter2, mineCounter3;
        JLabel timer1, timer2, timer3;
        public static GameFrame frame;
        JButton newGameButton;
     
        GameFrame() {
     
            setLayout(new BorderLayout());
     
            // adding top menu
            //add(new GamePanel(), BorderLayout.CENTER);
     
            addTopMenu();
     
            setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
            setVisible(true);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setResizable(false);
     
        }
     
     
        public static void main(String[] args) {
     
            frame = new GameFrame();
        }
     
        // Indeling af kode. Den del af koden som laver top menuen.
        void addTopMenu() {
            topMenu = new JLabel();
            topMenu.setIcon(new ImageIcon("Images/menuBackground.png"));
            topMenu.setLayout(new BoxLayout(topMenu, BoxLayout.Y_AXIS));
     
            JButton optionButton = new JButton();
     
     
            optionButton.setAlignmentX(LEFT_ALIGNMENT);
            String iconfilePath = this.getClass().getClassLoader().getResource("Images/gear.png").getFile();
            optionButton.setIcon(new ImageIcon(iconfilePath));
            optionButton.setBorder(BorderFactory.createEmptyBorder());
            optionButton.setContentAreaFilled(false);
            optionButton.setFocusable(false);
            topMenu.add(optionButton);
     
     
     
     
            JPanel iconMenu = new JPanel();
            iconMenu.setLayout(new BoxLayout(iconMenu, BoxLayout.X_AXIS));
     
     
     
            // topMenu.setLayout(new GridLayout(1, 3, 0, 300));
     
     
     
            // topMenu.add(Box.createRigidArea(new Dimension(0, 0)));
            // topMenu.add(Box.createRigidArea(new Dimension(0, 0)));
     
            mineCounter3 = new JLabel();
            mineCounter3.setIcon(new ImageIcon("Images/Numbers/1.png"));
            mineCounter3.setAlignmentX(LEFT_ALIGNMENT);
            iconMenu.add(mineCounter3);
     
            mineCounter2 = new JLabel();
            mineCounter2.setIcon(new ImageIcon("Images/Numbers/9.png"));
            mineCounter2.setAlignmentX(LEFT_ALIGNMENT);
            iconMenu.add(mineCounter2);
     
            mineCounter1 = new JLabel();
            mineCounter1.setIcon(new ImageIcon("Images/Numbers/9.png"));
            mineCounter1.setAlignmentX(LEFT_ALIGNMENT);
            iconMenu.add(mineCounter1);
     
            // topMenu.add(Box.createRigidArea(new Dimension(0, 0)));
            // topMenu.add(Box.createRigidArea(new Dimension(830, 0)));
     
            newGameButton = new JButton();
            newGameButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    System.out.println("new game");
                }
            });
     
            newGameButton.setIcon(new ImageIcon("Images/Defualt.png"));
            newGameButton.setBorder(BorderFactory.createEmptyBorder());
            newGameButton.setContentAreaFilled(false);
            newGameButton.setFocusable(false);
            newGameButton.setAlignmentX(CENTER_ALIGNMENT);
            iconMenu.add(newGameButton);
     
            // .add(Box.createRigidArea(new Dimension(830, 0)));
     
            timer3 = new JLabel();
            timer3.setIcon(new ImageIcon("Images/Numbers/0.png"));
            timer3.setAlignmentX(RIGHT_ALIGNMENT);
            iconMenu.add(timer3);
     
            timer2 = new JLabel();
            timer2.setIcon(new ImageIcon("Images/Numbers/0.png"));
            timer2.setAlignmentX(RIGHT_ALIGNMENT);
            iconMenu.add(timer2);
     
            timer1 = new JLabel();
            timer1.setIcon(new ImageIcon("Images/Numbers/0.png"));
            timer1.setAlignmentX(RIGHT_ALIGNMENT);
            iconMenu.add(timer1);
     
     
            iconMenu.setAlignmentY(LEFT_ALIGNMENT);
     
            topMenu.add(iconMenu);
     
     
     
            add(topMenu, BorderLayout.NORTH);
     
        }
     
        public void changeIcon(String imagePath) {
            String iconfilePath = this.getClass().getClassLoader().getResource(imagePath).getFile();
            newGameButton.setIcon(new ImageIcon(iconfilePath));
        }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: setAlignment have no effect

    Can you describe what happens when the code is executed?
    Then describe what you want to be different.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2022
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: setAlignment have no effect

    What I want is a small JButton with a image in the very top left corner. Then I want 3 JLabel with image in the left side, 1 JButton with image in the middle and 3 JLabel with image in the right side. All of this is in borderlayout section NORTH. Right now everthing is in the middle.

    How I have tried achieve this is by adding a JLabel with a background image to section NORTH: topMenu. Layout(new BoxLayout(topMenu, BoxLayout.Y_AXIS));. The reason I do this is becuase I want my small top left JButton to be further up then the other elements: optionButton.setAlignmentX(LEFT_ALIGNMENT);

    Now I make a seperate JPanel to my 7 other elements: iconMenu.setLayout(new BoxLayout(iconMenu, BoxLayout.X_AXIS));. I use the timer3.setAlignmentX(RIGHT_ALIGNMENT); on 3 of my elements, newGameButton.setAlignmentX(CENTER_ALIGNMENT); on 1 and mineCounter1.setAlignmentX(LEFT_ALIGNMENT); on yet another 3.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: setAlignment have no effect

    Check the calls to the setAllgnment methods.

    The code appears to put contents on two rows.


    Try posting on this site: https://coderanch.com/forums
    There are more people there that know layouts
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2022
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: setAlignment have no effect

    Quote Originally Posted by Norm View Post
    Check the calls to the setAllgnment methods.

    The code appears to put contents on two rows.


    Try posting on this site: https://coderanch.com/forums
    There are more people there that know layouts

    The guys from the coderanch helped me and now everything works. Using glue was the trick. Thanks for the help

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: setAlignment have no effect

    Posted here: https://coderanch.com/t/753741/java/setAlignment-effect

    Can you post the working code here?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Effect of Perm gen memory on CPU Utilization
    By abhi_gkp in forum Java Theory & Questions
    Replies: 7
    Last Post: July 3rd, 2014, 08:14 AM
  2. Effect of Perm gen memory on CPU Utilization
    By abhi_gkp in forum Member Introductions
    Replies: 4
    Last Post: July 3rd, 2014, 04:59 AM
  3. Make A Text Blinking Effect
    By Nemesis89 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 3rd, 2014, 12:09 PM
  4. query to developed for 3 view effect
    By nishantapte in forum What's Wrong With My Code?
    Replies: 0
    Last Post: June 22nd, 2013, 03:16 AM