【问题标题】:VPL start with javaVPL从java开始
【发布时间】:2016-02-14 09:54:08
【问题描述】:

我正在尝试创建一个可以修改的通用节点,但我遇到了一些麻烦,因为我在 Swing 等方面的技能不太好。

问题 1:我似乎无法在同一个 JFrame 中获得两个 Node/Jpanel。

小问题2:笔画从JPanel边框被切断(我应该建立一种出血,这样就不会发生了吗?)

任何帮助都有帮助:)

public class SimpleGui {

    public SimpleGui() {
        JFrame frame = new JFrame();

        NodePanel panel = new NodePanel(3, 2);
        panel.setLayout(new javax.swing.SpringLayout());
        frame.add(panel);

        NodePanel panel2 = new NodePanel(3, 2);
        panel2.setLayout(new javax.swing.SpringLayout());
        frame.add(panel2);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 300);
        frame.setResizable(true);
        frame.setVisible(true);

        panel.setLocation(50, 50);
        panel2.setLocation(150, 150);
    }
}

添加到 JFrame 的通用节点/面板类:

public class NodePanel extends JPanel {

    private int x = 0, int y = 0;    
    private PortPanel inPortPanel;
    private PortPanel outPortPanel;    
    int iH;
    int oH;    
    private int width = 120;
    private int height = 30;    
    Graphics2D g2;

    public NodePanel(int input, int output) {

        inPortPanel = new PortPanel(input);
        this.add(inPortPanel);

        outPortPanel = new PortPanel(output);
        this.add(outPortPanel);

        setPreferredSize(calculateDimension());
    }

    public void paintComponent(Graphics g) {
        g2 = (Graphics2D) g;

        g2.setColor(Color.black);
        g2.setStroke(new BasicStroke(4));

        inPortPanel.setLocation(x, y + (height - iH) / 2);
        outPortPanel.setLocation(x + width, y + (height - oH) / 2);

        drawNode();
    }

    private void drawNode() {
        g2.drawRoundRect(x, y, width, height, 5, 5);
    }

    private Dimension calculateDimension() {

        iH = inPortPanel.getPreferredSize().height;
        int iW = inPortPanel.getPreferredSize().width;
        oH = outPortPanel.getPreferredSize().height;
        height = iH > oH ? iH + iW : oH + iW;

        return new Dimension(width, height);
    }
}

作为节点面板一部分的端口面板

public class PortPanel extends JPanel {

    int x = 0;
    int y = 0;
    int portWidth = 14;
    int portHeight = 14;
    int count = 0;
    int offset = 22;
    Graphics2D g2;

    public PortPanel(int i) {
        this.count = i;
        setPreferredSize(calculateDimensions());        
    }

    public void paintComponent(Graphics g) {
        g2 = (Graphics2D) g;
        g2.setColor(Color.black);
        drawPorts();
    }

    private void drawPorts() {
        for (int i = 0; i < count; i++) {
            g2.fillOval(x, y + (i * offset), portWidth, portHeight);
        }
    }

    private Dimension calculateDimensions(){
        int overallHeight = (offset * (count-1)) + portHeight;
        return new Dimension(portWidth,overallHeight);
    }
}

【问题讨论】:

    标签: java swing jpanel awt


    【解决方案1】:
    • 节点应该是一个逻辑类,而不是一个 GUI 组件类。
    • 您应该有一个可以绘制逻辑实体的所有可视化表示的单个绘图 JPanel。
    • 这样,模型可以包含多个逻辑实体,所有这些都可以由单个绘图 JPanel 绘制,而不必担心布局管理器。
    • 不要忘记在其覆盖方法中调用您的 JPanel 的 super.paintComponent 方法,以便您的 JPanel 可以进行内务处理。
    • 避免提供 JPanels Graphics 或 Graphics2D 字段,因为这会增加代码引发 NPE 的风险。使用为您的paintComponent 方法提供的Graphics 对象,如果您需要在paintComponent 调用的另一个方法中使用它,请将其传递 到该方法中。

    【讨论】:

    • 你能详细说明一下 super.paintComponent 吗?休息是可以理解的。
    • @Josephus87:如果你不调用 super 的方法,JPanel 不能做家务绘画,包括在脏像素上绘画。
    • @Josephus87 请查看Painting in AWT and SwingPerforming Custom Painting,详细了解如何在 Swing 中进行绘画
    • @HovercraftFullOfEels 所以如果我例如双击我所有的 JPanel……它会创建一个节点并绘制它……但是我如何传入 Graphics 对象以供初学者使用?
    • 啊忘记了,我刚刚了解到 JavaFX 处理布局和样式要简单得多。所有的 Swing 书籍都应该被烧掉,因为它太老套了。
    猜你喜欢
    • 1970-01-01
    • 2013-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-31
    • 2011-03-21
    • 2011-09-01
    • 1970-01-01
    相关资源
    最近更新 更多