VPL从java开始
我正在尝试创建一个通用节点,可以根据自己的意愿进行修改,但我遇到了一些麻烦,因为我在Swing等方面的技能不太好 问题1:我似乎无法在同一JFrame中获得两个Node/JPanel 较小的问题2:中风是从JPanel边界切断的(我是否应该建立一种出血,这样就不会发生这种情况?) 任何援助都是有帮助的:) 添加到JFrame的常规节点/面板类: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;
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的
方法,以便JPanel可以进行内部绘制super.paintComponent
- 避免使用JPanels图形或Graphics2D字段,因为这样会增加代码抛出NPE的风险。使用指定给paintComponent方法的图形对象,如果需要在paintComponent调用的另一个方法中使用它,将其传递到该方法中
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);
}
}