[java] JApplet and setting coordinates of components
Hi,
I have a JApplet with a JPanel inside it. On the JPanel there are 14 buttons, but I need to set the coordinates of these buttons and was wondering if there is an easy way. I've looked at setLocation, but that didn't seem to work when i tried it out.
Any ideas?
Thanks,
SD
If you really want the easy way, use eclipse and use the Visual Editor (VE) plugin. This will let you click and drag the buttons where you want them, and it also has nice tools for lining up components.
As for set location, the default layout for a JPanel is a FlowLayout. This layout will ignore the location of the object, and auto-position them for you. What you want to do is remove the Layout by passing in null. This should let the setLocation method work, but I still think the VE editor from eclipse would be the way to go.
P.S. I just typed in the code from memory, so don't trust the syntax to be 100%. If this does not work, let us know.
As for set location, the default layout for a JPanel is a FlowLayout. This layout will ignore the location of the object, and auto-position them for you. What you want to do is remove the Layout by passing in null. This should let the setLocation method work, but I still think the VE editor from eclipse would be the way to go.
JPanel p = new JPanel();p.setLayout( null );JButton b = new JButton( "test" );b.setLocation( 50, 50 );p.add( b );
P.S. I just typed in the code from memory, so don't trust the syntax to be 100%. If this does not work, let us know.
Plus, you have a few 'third party' alternatives for layout, if that's really a concern for you and you don't want to depend upon (usually messy) generated code. Just type a few keywords on google to find out.
Quote:Original post by Glass_Knife
If you really want the easy way, use eclipse and use the Visual Editor (VE) plugin. This will let you click and drag the buttons where you want them, and it also has nice tools for lining up components.
As for set location, the default layout for a JPanel is a FlowLayout. This layout will ignore the location of the object, and auto-position them for you. What you want to do is remove the Layout by passing in null. This should let the setLocation method work, but I still think the VE editor from eclipse would be the way to go.
*** Source Snippet Removed ***
P.S. I just typed in the code from memory, so don't trust the syntax to be 100%. If this does not work, let us know.
Well I tried the code but it doesn't display anything when I have the this.setLayout(null); code in the constructor of the JPanel.
Well here is the constructor, sorry for the late reply, been busy with other things
public class KalahGUI extends JPanel implements ActionListener, MouseListener { private Game game; // Graphical representations of the Kalahs private JButton kalah[]; // Graphical representations of the pits private JButton pits[][]; /** Creates a new instance of KalahGUI */ public KalahGUI(Game game) { KalahGameBoard startingBoard; this.setLayout(null); this.game = game; startingBoard = (KalahGameBoard)game.getState().getBoard(); kalah = new JButton[2]; kalah[0] = new JButton("" + startingBoard.getKalah(0)); kalah[1] = new JButton("" + startingBoard.getKalah(1)); kalah[0].setActionCommand("Kalah0"); kalah[1].setActionCommand("Kalah1"); kalah[0].addActionListener(this); kalah[1].addActionListener(this); kalah[0].setLocation(100,100); kalah[1].setLocation(1, 1); this.add(kalah[0]); this.add(kalah[1]); //kalah[0].setBounds(0, 200, 50, 50); //kalah[1].setLocation(200, 100); pits = new JButton[2][6]; for(int i = 0; i < 2; i++) for(int j = 0; j < 6; j++) { pits[j] = new JButton("" + startingBoard.getStones(i, j)); pits[j].setActionCommand("Pit" + ((6*i) + j)); pits[j].addActionListener(this); this.add(pits[j]); } }
Well, it looks like if you do not set the size of the button, it will not be visible. Try this:
That will not only make the button visible, but auto-size it based on
the button text.
kalah[0].setLocation(100,100);kalah[0].setSize(kalah[0].getPreferredSize());kalah[1].setLocation(1, 1);kalah[1].setSize(kalah[1].getPreferredSize());
That will not only make the button visible, but auto-size it based on
the button text.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement