javaswinggui-designer

Need some help with GUI in java


i'm working on GUI in java and got stuck with move the object.

Please visit this youtube video i made a short demo for you guys to see what i was trying to do. I'm so new to the GUI thing as i've never been taught of doing GUI.

Here is the link: http://www.youtube.com/watch?v=up1LV5r-NSg


Solution

  • I see you're using a GUI designer. I highly recommend building your GUI "by hand" instead in which case your code is IMO much clearer (I'm not saying all GUI designers produce bad code, but it is almost always harder to read, and editing it will be hard without using the exact same GUI designer). Once you're comfortable with GUI designing by hand, then try a GUI designer and see what makes you more comfortable.

    See: http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html

    In your case, you might create a BorderLayout, and in the "south" of your panel/frame you can place a panel with a FlowLayout aligning it's components to the left. Then add your button to the panel with the FlowLayout.

    A little demo:

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Date;
    
    public class LayoutDemo extends JFrame {
    
        LayoutDemo() {
            super("LayoutDemo");
            super.setSize(400, 200);
            super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            createGUI();
            super.setVisible(true);
        }
    
        private void createGUI() {
            // set the layout of this frame
            super.setLayout(new BorderLayout());
    
            // create a panel to put the button on
            final JPanel bottomPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
    
            // create a text area to put in the center
            final JTextArea textArea = new JTextArea();
    
            // create the search button
            final JButton searchButton = new JButton("search");
    
            // add a listener to the button that add some text to the text area
            searchButton.addActionListener(new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent e) {
                    textArea.setText(textArea.getText() + "pressed search on " + (new Date()) + "\n");
                }
            });
    
            // add the button to the bottom panel
            bottomPanel.add(searchButton);
    
            // wrap a scroll-pane around the text area and place it on the center of this frame
            super.add(new JScrollPane(textArea), BorderLayout.CENTER);
    
            // put the bottom panel (containing the button) on the 'south' of this frame
            super.add(bottomPanel, BorderLayout.SOUTH);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    new LayoutDemo();
                }
            });
        }
    }
    

    produces:

    alt text http://img689.imageshack.us/img689/5874/guiq.png


    EDIT

    And to move the button a bit more up, use the constructor new FlowLayout(FlowLayout.LEFT, int hgap, int vgap)

    where hgap is the gap (in pixels) between the left and right components and vgap is the gap (in pixels) between the upper and lower components.

    Try:

    final JPanel bottomPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 10));
    

    Note that the space between the button and text area also increases slightly!