简体   繁体   English

创建一个方法来处理所有类型的Swing组件

[英]Creating a method to handle all Types of Swing Components

In order to save some time when setting the size of Swing Components (JPanel, JLabel, etc.), I am trying to write a method that when called, will set that component's size using 3 of the standard size-defining functions. 为了在设置Swing组件(JPanel,JLabel等)的大小时节省一些时间,我试图编写一个方法,在调用时,将使用3个标准大小定义函数设置该组件的大小。

I would like to be able to do this: 我希望能够做到这一点:

import CustomComponents;
...
 JPanel xPanel = new JPanel();
 CSize.setDefiniteSize(xPanel, 400, 300);
...

Or (and preferably) 或者(最好)

...
 JPanel xPanel = new JPanel();
 xPanel.CSize.setDefiniteSize(400, 300);
...

Here's the code I have so far. 这是我到目前为止的代码。 The problem is, I want the method to be functional for all types of Swing components, and it seems to only work for the one I specify in the heading. 问题是,我希望该方法适用于所有类型的Swing组件,它似乎只适用于我在标题中指定的方法。

package CustomComponents;
import javax.swing.JComponent;
import java.awt.Dimension;

public class CSize
{
public static void setDefiniteSize(JComponent c, int h, int w)
{
    Dimension k = new Dimension (h, w);
    c.setMaximumSize(k);
    c.setMinimumSize(k);
    c.setPreferredSize(k);
}
}

As you can see, here I tried using JComponent, but it won't work, as whatever I use it with - say a JPanel - is a JPanel, not a JComponent (even though it's a subclass? I still don't get how this works) 正如你所看到的,这里我尝试使用JComponent,但是它不起作用,因为我使用它 - 比如一个JPanel - 是一个JPanel,而不是一个JComponent(即使它是一个子类?我仍然不知道如何这个工作)

Though what you're trying to do, you may think is crafty, but you may find it later to be discouraging trying to size everything 虽然你正在尝试做什么,但你可能认为是狡猾的,但你可能会发现它后来试图调整一切

See this thread Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? 看到这个线程我应该避免在Java Swing中使用set(Preferred | Maximum | Minimum)Size方法吗?

Instead you should make use of LayoutManagers. 相反,你应该使用LayoutManagers。 and .pack() the frame. .pack()框架。 Swing was made to be used with LayoutManager. 使Swing与LayoutManager一起使用。 You need to take into account that your program may look good on your development screen, but may look different on others. 您需要考虑到您的程序在开发屏幕上看起来不错,但在其他程序上可能看起来不同。 You will also find out that many LayoutManegers will not even respect your preferred size, so you need to learn which ones do and which ones don't. 您还会发现许多LayoutManegers甚至不会尊重您的首选大小,因此您需要了解哪些做了哪些,哪些不做。 Here's a simple example 这是一个简单的例子

With layout managers, you let the layouts do all the sizing for you. 使用布局管理器,您可以让布局为您完成所有大小调整。 Some layouts will stretch your components, some center them in open spaces. 一些布局会拉伸您的组件,一些布局将它们放在开放空间中。 Your program will be a lot more fluid if you use them. 如果您使用它们,您的程序将更加流畅。

See Laying out Components Within a Container for more details on how you can use them. 有关如何使用它们的更多详细信息,请参阅在容器中布置组件

You have to use java.awt.Container , because JFrame is not a direct subclass of JComponent ! 你必须使用java.awt.Container ,因为JFrame不是JComponent的直接子类!

So it should work with the following code: 所以它应该使用以下代码:

package CustomComponents;

import java.awt.Container;
import java.awt.Dimension;

public class CSize {
    public static void setDefiniteSize(Container c, int h, int w) {
        Dimension k = new Dimension (h, w);
        c.setMaximumSize(k);
        c.setMinimumSize(k);
        c.setPreferredSize(k);
    }
}

Look at the hierarchy of the components: 查看组件的层次结构:

java.awt.Component
  |--java.awt.Container
       |--java.awt.Window
       |    |--java.awt.Frame
       |         |--javax.swing.JFrame
       |--javax.swing.JComponent
            |--javax.swing.JLabel

Even better would be using java.awt.Component , since this class is defining all methods you want to use ( setMinimumSize , setMaximumSize and setPreferredSize ). 更好的是使用java.awt.Component ,因为这个类定义了你想要使用的所有方法( setMinimumSizesetMaximumSizesetPreferredSize )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM