Swing: What is a good way to implement fully-scalable components?

我怕爱的太早我们不能终老 提交于 2019-12-24 08:25:00

问题


I am creating an application which has a scrollable and scalable (zoomable) view. Inside this view I want to place other components (most of them customized JPanels and JInternalFrames).

Things such as fonts and borders, and sub-elements such as buttons don't need to be scalable. Only dimensions and position of container components should be.

What do you think, what is a good way to implement scalable components?

EDIT: I'm talking about resizing the entire layout including all components. Please think of something such as a visual UML editor with zoom functionality.

My alternatives are:

  • Create a custom layout manager;
  • Create custom resizeable sub-components;
  • Create a custom container which would take care of resizing its sub-components;
  • Do something else?

Possible problems:

  • Boilerplate code;
  • Necessity to provide access to additional custom properties of components;
  • Not straightforward (inconsistent) representation of components in code.
  • Something else?

回答1:


This is why layout managers exist: they tell contained components where and how large they should be.

Since you're talking about a UML editor, are you using contained Swing components to represent the various objects in the diagram (eg, a component for a class)? If yes, then you've got a lot of work ahead of you (although it's not necessarily a bad approach). I'd recommend creating a constraints object that identifies the object's location on a "unit space," then multiplying by the current size.




回答2:


I've done that by creating a custom layout manager. Every component (or rather component class) on the frame has a marker whether or not it shall be resized when the container is resized (e.g. tables are resized, buttons are not). Those which are not resized are moved when the container is resized.

This is used to make resizeable forms without any manual setup, i.e. forms are defined by just specifying x/y/length/width for each component (no further alignment info).




回答3:


If I look at this problem as visual UML editor then I had to think about single "canvas" component drawing each element as graphical object with base aspect ration and zooming in/out. I can't see reason for list of components aligned within parent container.




回答4:


I started a similar solution that works fine without touching the original layout.

It's as easy as this:

// Install scalable layout and CTRL+/CTRL- keys for scaling operations
ScalableLayoutUtils.installScalableLayoutAndKeys(new DefaultScalableLayoutRegistry(),  frame, 0.1);

It's still under work but It will be soon available as part of the next "utils4swing" version.



来源:https://stackoverflow.com/questions/1031795/swing-what-is-a-good-way-to-implement-fully-scalable-components

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!