gpt4 book ai didi

java - 将绝对布局合并为标准布局

转载 作者:行者123 更新时间:2023-12-02 02:38:12 26 4
gpt4 key购买 nike

我正在尝试创建一个简单的 Java GUI 应用程序,我可以在其中放置端口、开关和天线。然后我想映射端口的输出通过多个开关传导到何处,这些开关将在 GUI 中更改。

下图是自定义创建的对象(端口、电缆、开关和天线),它覆盖使用绝对坐标在 JFrame 上绘制的 JPanel 容器。

在 Netbeans 中,我创建了一个表单,我想将绝对框架和表单组合在一起以形成一个完整的表单。

我已经做了一些搜索,这一切都是从不使用绝对布局开始的。我的问题是我应该使用什么布局?我预计将使用 20 多个交换机、10 多个端口,甚至更多的电缆来连接所有将交叉所有其他面板(非 Planer)的电缆。

我可以使用绝对坐标创建面板,然后使用不同的布局管理器将其嵌入到表单中吗?

package SystemDiagram;

import java.awt.Point;
import java.util.ArrayList;
import java.util.HashMap;
import javax.swing.JFrame;
import javax.swing.JPanel;



public class SystemDiagram {

Callbox callBox; //Callbox
Switch swi; //Switches
HashMap<String,Cable> cables = new HashMap<>(); //Cables
Antenna downlinkAntenna;
Form control;
JFrame f;
JPanel diagram;

public static void main(String args[]) {

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new SystemDiagram();

}
});
}

public SystemDiagram() {
intializeContainers();
createElements();
}

private void intializeContainers(){
control = new Form(); //unused until I can combine it with f JFrame as a JPanel
diagram = control.getDiagramPanel();//unused was trying to draw callbox, switches, and antenna's on this JPanel of the Netbeans form
f = new JFrame(); //needs to turn into a JPanel and be added to control Form object
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(1000, 800);
}


private void createElements()
{
//output
callBox = new Callbox("OUT", new Point (0,100));

cables.put("c1", new Cable("c1"));
cables.put("c2", new Cable("c2"));
cables.put("c3", new Cable("c3"));

callBox.setOutputCable(cables.get("c1"));

//CA Combiner
swi = new Switch("1 to 3 Switch", 3, new Point(345, 140),true);// Name, # ouf output ports, top left point of Jpanel, reversed
//Output side
swi.setOutputCable(1, cables.get("c2"));
swi.setOutputCable(2, cables.get("c3"));
//Input side
swi.setInputCable(cables.get("c1"));
//Activate Output
swi.setActiveOutput(2);

//DL Antenna
downlinkAntenna = new Antenna("link", new Point(500,320), "spiral", true);
downlinkAntenna.setOutputCable(cables.get("c3"));



}


private void packComponents()
{
//Callboxes/Ports
f.add(callBox.getGraphic());// add callbox graphic (JPanel) to Frame
f.pack();
f.add( swi.getGraphic());// add switch graphic (JPanel) to Frame
f.pack();
//Cables
ArrayList<String> cableKeys = new ArrayList<>(cables.keySet());
for(String key: cableKeys){
if(cables.get(key).getConnections() == 2)// if a cable is connected on both ends draw its' graphic
{
Cable temp = cables.get(key);
temp.getGraphic().setPoint(0,temp.getFirstConnection().getPortCoordinate((Component)temp));// Sets first point coordinate of cable
temp.getGraphic().setPoint(1,temp.getSecondConnection().getPortCoordinate((Component)temp));// Sets 2nd point of cable
f.add(cables.get(key).getGraphic());
f.pack();
}
}
//Antenna pack
f.add(downlinkAntenna.getGraphic());// add antenna JPanel to Frame
f.pack();
f.setVisible(true);
}
}

最佳答案

您可以一起撰写 View 。例如,使用 BorderLayout 创建 JPanel ,使用 BorderLayer.CENTER 添加具有绝对布局的面板,并将 Netbeans 生成的表单添加为 BorderLayout.LINE_END。 或者,您可以使用 splitter view

关于java - 将绝对布局合并为标准布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45928041/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com