2

我在eclipse中做项目,我做了swing部分。但是现在,我希望使用 Netbeans 来完成项目的 Swing 部分,因为它更容易做,而且我能够更整洁地完成它,因为我对 Swing 及其布局管理器没有太多经验。这是我在 stackoverflow 上找到的一个示例,其中包含一些摆动组件。

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.*;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
import javax.swing.*;

public class MoveIcon extends JPanel {

    private static final long serialVersionUID = 1L;
    private static final String IMAGE_PATH = "http://duke.kenai.com/misc/Bullfight.jpg";
    private static final String IMAGE_PATH_PLAYER = "http://duke.kenai.com/iconSized/duke4.gif";
    public static final int STEP = 3;
    private static final int TIMER_DELAY = STEP * 8;
    private BufferedImage bkgrndImage = null;
    private BufferedImage playerImage = null;
    private Map<Direction, Boolean> directionMap = new HashMap<Direction, Boolean>();
    private int playerX = 0;
    private int playerY = 0;

    enum Direction {

        UP(KeyEvent.VK_UP, 0, -1), DOWN(KeyEvent.VK_DOWN, 0, 1),
        LEFT(KeyEvent.VK_LEFT, -1, 0), RIGHT(KeyEvent.VK_RIGHT, 1, 0);
        private int keyCode;
        private int xDirection;
        private int yDirection;

        private Direction(int keyCode, int xDirection, int yDirection) {
            this.keyCode = keyCode;
            this.xDirection = xDirection;
            this.yDirection = yDirection;
        }

        public int getKeyCode() {
            return keyCode;
        }

        public int getXDirection() {
            return xDirection;
        }

        public int getYDirection() {
            return yDirection;
        }
    }

    public MoveIcon() {
        try {
            URL bkgrdImageURL = new URL(IMAGE_PATH);
            URL playerImageURL = new URL(IMAGE_PATH_PLAYER);
            bkgrndImage = ImageIO.read(bkgrdImageURL);
            playerImage = ImageIO.read(playerImageURL);
            setPreferredSize(new Dimension(bkgrndImage.getWidth(), bkgrndImage.getHeight()));
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        for (Direction direction : Direction.values()) {
            directionMap.put(direction, false);
        }
        setKeyBindings();
        Timer timer = new Timer(TIMER_DELAY, new TimerListener());
        timer.start();
    }

    private void setKeyBindings() {
        InputMap inMap = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
        ActionMap actMap = getActionMap();
        for (final Direction direction : Direction.values()) {
            KeyStroke pressed = KeyStroke.getKeyStroke(direction.getKeyCode(), 0, false);
            KeyStroke released = KeyStroke.getKeyStroke(direction.getKeyCode(), 0, true);
            inMap.put(pressed, direction.toString() + "pressed");
            inMap.put(released, direction.toString() + "released");
            actMap.put(direction.toString() + "pressed", new AbstractAction() {

                private static final long serialVersionUID = 1L;

                @Override
                public void actionPerformed(ActionEvent e) {
                    directionMap.put(direction, true);
                }
            });
            actMap.put(direction.toString() + "released", new AbstractAction() {

                private static final long serialVersionUID = 1L;

                @Override
                public void actionPerformed(ActionEvent e) {
                    directionMap.put(direction, false);
                }
            });
        }

    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (bkgrndImage != null) {
            g.drawImage(bkgrndImage, 0, 0, null);
        }
        if (playerImage != null) {
            g.drawImage(playerImage, playerX, playerY, null);
        }
    }

    private class TimerListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            boolean moved = false;
            for (Direction direction : Direction.values()) {
                if (directionMap.get(direction)) {
                    playerX += STEP * direction.getXDirection();
                    playerY += STEP * direction.getYDirection();
                    moved = true;
                }
            }
            if (moved) {
                int x = playerX - 2 * STEP;
                int y = playerY - 2 * STEP;
                int w = playerImage.getWidth() + 4 * STEP;
                int h = playerImage.getHeight() + 4 * STEP;
                MoveIcon.this.repaint(x, y, w, h); // !! repaint just the player
            }
        }
    }

    private static void createAndShowUI() {
        JFrame frame = new JFrame("MoveIcon");
        frame.getContentPane().add(new MoveIcon());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                createAndShowUI();
            }
        });
    }
}

问题是:由于已经以编程方式设计了一些 Swing 组件,我可以在 netbeans 中打开这个项目并使用它的 GUI Builder 来进一步构建 GUI 部分吗?

4

2 回答 2

2

不支持您提出的建议,但您可以JPanel Form向现有框架添加任意数量的新实例。SouthPanel是通过选择File > New File > Swing GUI Forms > JPanel Form、添加JLabel和指定来创建的FlowLayout

private static void createAndShowUI() {
    JFrame frame = new JFrame("MoveIcon");
    frame.add(new MoveIcon(), BorderLayout.CENTER);
    frame.add(new SouthPanel(), BorderLayout.SOUTH);
    ...
}

移动图标图像

于 2012-06-14T05:22:31.293 回答
2

不,但是是的。

问题与 NetBeans 如何处理其项目和 UI 构建器有关。

Netbeans 在项目文件夹的根目录中放置了一个名为“nbproject”的文件夹,其中包含在 Eclipse 中不会发生的 Netbeans 独有的内容(我会解释,但这超出了您的问题范围)。但是,您可以使用File -> Import Project -> Eclipse Project...它并且它应该可以工作。

Swing GUI 附带了更棘手的部分。您绝对可以将 Swing 代码导入 Netbeans,但 Netbeans 有自己的库,它不完全是纯 Swing。它被称为org.jdesktop.application,它包括SingleFrameApplication,FrameView和一些其他定制的东西。当您使用 Netbeans 框架构建应用程序时,它会优先创建这些类而不是“实际的”Swing。你可以使用拖放编辑器吗?我从未尝试过,但我敢打赌答案是否定的。另一方面,我现在确实有一个正在处理的项目,我放弃了 Netbeans Swing 转而使用纯 Swing,如果您对没有拖放功能感到满意,那实际上还不错。它更加灵活和强大,因为生成的代码没有被编辑阻止。

于 2012-06-14T05:15:29.360 回答