Results 1 to 4 of 4
  1. #1
    beginnerprog is offline Member
    Join Date
    Mar 2013
    Posts
    22
    Rep Power
    0

    Default changing position of Jbuttons

    Ok heres my code im trying to get two buttons on each line on the Jpanel but im soo confused.



    import java.awt.*;
    import javax.swing.JFrame;
    import javax.swing.*;
    import javax.swing.border.*;

    public class TravelAgentSystemGUI{

    public static void main (String args[]) {
    JFrame myJFrame = new JFrame();
    myJFrame.setSize(500,300); //500 wide, 300 tall
    myJFrame.setLocation(150,250); //150 across, 250 down
    myJFrame.setTitle("TravelAgentSystem");
    myJFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    myJFrame.setResizable(true); //allows user to resize the frame
    myJFrame.setVisible(true);

    myJFrame.setLayout(new BorderLayout());
    JPanel testPanel = new JPanel();
    JButton moneyButton, timeButton, hotelButton, exitButton;
    moneyButton = new JButton("Money");
    timeButton = new JButton("Time");
    hotelButton = new JButton("Hotel");
    exitButton = new JButton("Exit");
    testPanel.add(timeButton);
    testPanel.add(moneyButton);
    testPanel.add(hotelButton);
    testPanel.add(exitButton);
    testPanel.setBorder( new TitledBorder( new EtchedBorder(), "Main Menu"));
    testPanel.setBackground(Color.RED);
    myJFrame.add(testPanel, BorderLayout.WEST);
    timeButton.setLocation(0,1);
    moneyButton.setLocation(0,2);
    hotelButton.setLocation(1,3);





    }
    }

  2. #2
    Lets Assume is offline Assuming
    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0

    Default Re: changing position of Jbuttons

    You need to set a gridLayout for your testPanel.

    You should easily be able to achieve this by adding

    Java Code:
    testPanel.setLayout(new GridLayout(2,0));
    after assigning testPanel as a new JPanel.

  3. #3
    beginnerprog is offline Member
    Join Date
    Mar 2013
    Posts
    22
    Rep Power
    0

    Default Re: changing position of Jbuttons

    Quote Originally Posted by Lets Assume View Post
    You need to set a gridLayout for your testPanel.

    You should easily be able to achieve this by adding

    Java Code:
    testPanel.setLayout(new GridLayout(2,0));
    after assigning testPanel as a new JPanel.
    That works but now my button size has changed ive tryed to use timeButton.setPreferredSize(new Dimension(25, 25));

    but doesn't affect my program, how do i do this?

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

    Default Re: changing position of Jbuttons

    Consider creating a JPanel that uses BorderLayout that holds your buttons, and then placing that JPanel into another one that holds your GUI. You'll be basically nesting JPanels.

    Also on an unrelated note, Also, when posting your code, please use code tags so that your code will retain its formatting and be readable. To do this, you will need to paste already formatted code into the forum, highlight this code, and then press the "code" button at the top of the forum Message editor prior to posting the message. You may want to click on the Preview tab to make sure that your code is formatted correctly. Another way is to place the tag [code] at the top of your block of code and the tag [/code] at the bottom.

Similar Threads

  1. Replies: 4
    Last Post: 12-31-2010, 12:15 PM
  2. Changing JScrollPane default viewing position
    By peterhabe in forum AWT / Swing
    Replies: 7
    Last Post: 10-07-2010, 08:44 PM
  3. Changing position of JMenuBar
    By LianaN in forum New To Java
    Replies: 8
    Last Post: 09-17-2010, 12:41 PM
  4. Replies: 0
    Last Post: 08-14-2010, 10:09 AM
  5. get position in string from caret position
    By helloworld111 in forum AWT / Swing
    Replies: 5
    Last Post: 02-19-2009, 01:36 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
  •