0

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

4

3 回答 3

5

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!

于 2010-05-27T05:57:01.050 回答
3

learn fest swing test and miglayout! fest swing test enables you to run your gui screnario. and Miglayout,it is my opinion, also is easy to use layout lib.

Fest: http://fest.easytesting.org/swing/wiki/pmwiki.php
MigLayout: http://www.miglayout.com/
于 2010-05-27T12:02:49.857 回答
0

If you are not trying to learn the ins & outs of Java Swing and aren't trying to create some fancy GUI then a GUI designer like the one you are using should be fine.

What you are not able to do seems to be a limitation of your particular IDE though, and therefore it might be helpful to give Netbeans a try. You can always take the generated GUI code (ugly as it may be) and then plug it back into your project in your other IDE.

于 2010-05-27T07:13:45.473 回答