VPL从java开始

VPL从java开始,java,swing,jpanel,awt,Java,Swing,Jpanel,Awt,我正在尝试创建一个通用节点,可以根据自己的意愿进行修改,但我遇到了一些麻烦,因为我在Swing等方面的技能不太好 问题1:我似乎无法在同一JFrame中获得两个Node/JPanel 较小的问题2:中风是从JPanel边界切断的(我是否应该建立一种出血,这样就不会发生这种情况?) 任何援助都是有帮助的:) 添加到JFrame的常规节点/面板类: public class NodePanel extends JPanel { private int x = 0, int y = 0;

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

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

较小的问题2:中风是从JPanel边界切断的(我是否应该建立一种出血,这样就不会发生这种情况?)

任何援助都是有帮助的:)

添加到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);
    }
}
public类PortPanel扩展了JPanel{
int x=0;
int y=0;
int-portWidth=14;
内部端口高度=14;
整数计数=0;
整数偏移=22;
图形2d-g2;
公共端口面板(int i){
this.count=i;
setPreferredSize(calculateDimensions());
}
公共组件(图形g){
g2=(图2d)g;
g2.设置颜色(颜色为黑色);
抽油孔();
}
专用void drawport(){
for(int i=0;i
  • 节点应该是逻辑类,而不是GUI组件类
  • 您应该有一个绘图JPanel,它可以绘制逻辑实体的所有可视化表示
  • 通过这种方式,模型可以保存多个逻辑实体,所有这些实体都可以由单个绘图JPanel绘制,而不必担心布局管理器
  • 别忘了在其覆盖方法中调用JPanel的
    super.paintComponent
    方法,以便JPanel可以进行内部绘制
  • 避免使用JPanels图形或Graphics2D字段,因为这样会增加代码抛出NPE的风险。使用指定给paintComponent方法的图形对象,如果需要在paintComponent调用的另一个方法中使用它,将其传递到该方法中

您能详细介绍一下super.paintComponent吗?其余的都明白了。@Josephus87:如果你不调用super的方法,JPanel不能进行家政绘画,包括在脏像素上绘画。@Josephus87看一看和,了解绘画在环境中如何工作的详细信息Swing@HovercraftFullOfEels因此,如果我双击我的all doing JPanel…它会创建一个节点并绘制它…但是我如何将图形对象传递给初学者呢?啊,忘了我了刚刚了解到JavaFX处理布局和样式要简单得多。所有的摇摆书都应该被烧掉,因为它们太过时了。
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);
    }
}