【问题标题】:Combining an absolute layout into a standard layout将绝对布局组合成标准布局
【发布时间】:2017-08-28 22:12:18
【问题描述】:

我正在尝试创建一个简单的 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);
}
}

【问题讨论】:

  • 另见JMCAD
  • 该图像(左侧)看起来不像是组件放置在面板上,而更像是自定义绘制的面板。因此,最好的方法可能是使用自定义绘画,并覆盖首选尺寸方法以返回合理的尺寸。可以通过将包含的所有形状加在一起,然后得到最终形状的边界来计算该大小。 然后返回适当的大小将使自定义绘制的组件能够与布局管理器一起使用。
  • 如需尽快获得更好的帮助,请发帖minimal reproducible exampleShort, Self Contained, Correct Example
  • “我做了一些搜索,一切都是从不使用绝对布局开始的。我的问题是我应该使用什么布局?”当然,也有'自定义布局'要考虑.. 使用这种方法,程序员将布局逻辑封装在自定义布局中,自定义布局根据组件的大小和位置计算出合理的首选大小,布局管理器使用它分配容器布置足够的空间。

标签: java swing layout layout-manager


【解决方案1】:

您可以将视图组合在一起。例如,使用BorderLayout 创建JPanel,使用BorderLayer.CENTER 将具有绝对布局的面板添加到其中,并将netbeans 生成的表单添加为BorderLayout.LINE_END。 或者,您可以使用splitter view

【讨论】:

  • ".. 使用BorderLayer.CENTER" ..然后pack() 顶级容器将绝对布局添加到它的面板,以查看绝对布局缩小到 0 大小.
  • 您不需要使用pack()。您可以设置JFrame 的大小。或者,在您的null 布局面板上使用setSize()setMinimumSize()setPreferredSize(),以防止pack() 将其缩小为空。
猜你喜欢
  • 2018-01-22
  • 2012-07-03
  • 2011-09-18
  • 2017-01-03
  • 2013-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-07
相关资源
最近更新 更多