简体   繁体   English

在Netbeans GUI Builder中开发现有的GUI代码

[英]Developing existing GUI code in Netbeans GUI Builder

I'm doing project in eclipse, I did the swing part. 我正在做日食项目,做了秋千部分。 But now, i wish to use Netbeans to do the Swing part of the project as its easier to do and i'll be able to do it more neatly as I am not experienced much with swing and its layout Managers. 但是现在,我希望使用Netbeans来完成该项目的Swing部分,因为它更容易完成,而且由于我对swing和它的布局管理器没有太多的经验,所以我可以更加整齐地完成它。 Here is an example i found on stackoverflow which consists of some swing components. 这是我在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();
            }
        });
    }
}

The question is: with some of the swing components already designed programatically, can i open this project in netbeans and use its GUI Builder to further build on the GUI part? 问题是:有了一些已经通过编程设计的摆动组件,我可以在netbeans中打开该项目并使用其GUI Builder进一步在GUI部件上构建吗?

No, but yes. 不,但是可以。

The problem has to do with how NetBeans does its projects and UI builder. 问题与NetBeans如何完成其​​项目和UI构建器有关。

Netbeans puts a folder in the root of the project folder called "nbproject" which contains stuff that's unique to Netbeans that doesn't happen in Eclipse (I would explain, but it's beyond the scope of your question). Netbeans在项目文件夹的根目录中放置一个名为“ nbproject”的文件夹,其中包含Netbeans特有的,Eclipse中不会发生的东西(我会解释,但这超出了您的问题范围)。 You can, however, use File -> Import Project -> Eclipse Project... and it should work. 但是,您可以使用File -> Import Project -> Eclipse Project... ,它应该可以工作。

The trickier part comes with the Swing GUI. 棘手的部分是Swing GUI。 You can absolutely import the Swing code into Netbeans, but Netbeans has its own library that isn't exactly pure Swing. 您可以将Swing代码绝对导入到Netbeans中,但是Netbeans有其自己的库,它并不完全是纯Swing。 It's called org.jdesktop.application and it includes SingleFrameApplication , FrameView , and a few other tailor-made things. 这就是所谓的org.jdesktop.application它包括SingleFrameApplicationFrameView ,以及其他一些量身定做的东西。 When you build an application with the Netbeans framework, it creates it preferring these classes over the "actual" Swing. 当您使用Netbeans框架构建应用程序时,它会比“实际” Swing优先使用这些类。 Can you use the drag and drop editor? 可以使用拖放编辑器吗? I've never tried, but I'll bet the answer is no. 我从未尝试过,但我敢打赌答案是否定的。 On the other hand, I do have one project I'm working on now that I abandoned the Netbeans Swing for the pure Swing, and if you're ok with not having the drag and drop features, it's actually kind of nice. 另一方面,我确实有一个项目正在处理,现在我放弃了Netbeans Swing来使用纯Swing,如果您可以不用拖放功能,那实际上还不错。 It's way more flexible and capable because the generated code is not edit-blocked. 因为生成的代码没有被编辑阻止,所以它更加灵活和功能强大。

What you propose is not supported, but you can add any number of new JPanel Form instances to your existing frame. 建议的内容不受支持,但是您可以向现有框架中添加任意数量的新JPanel Form实例。 SouthPanel was created by choosing File > New File > Swing GUI Forms > JPanel Form , adding a JLabel and specifying FlowLayout . 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);
    ...
}

MoveIcon图片

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM