简体   繁体   English

在Java NetBeans中创建自己的GUI构建器

[英]Make my own GUI Builder in Java NetBeans

I want to create a gui builder(like the one in NetBeans) in the following way : I have made a JSplitPane where in left side I have a JTextField where I write the name of a Swing Component and in the right side I want to appear that component. 我想通过以下方式创建一个gui生成器(如NetBeans中的一个):我制作了一个JSplitPane ,其中在左侧有一个JTextField ,在其中写入了Swing Component的名称,并在右侧显示了该组件。 Something like this : 像这样的东西:

http://i.stack.imgur.com/2KUYs.png

I want to make that using Reflections . 我想使用Reflections做到这一点。 This is first time using it, so please be kind with me and explain me how to make it. 这是第一次使用它,所以请对我好一点,并向我解释如何制作它。

Thank you! 谢谢!

in left side I have a JTextField where I write the name of a Swing Component 在左侧,我有一个JTextField,其中写入了Swing组件的名称

Well, you really shouldn't be use a JTextField. 好吧,您实际上不应该使用JTextField。 The user will not know what valid values are. 用户将不知道什么是有效值。 Instead you should probably be using a JCombobox. 相反,您可能应该使用JCombobox。 Then you just get the component name from the selected item 然后,您只需从所选项目中获取组件名称

I want to make that using Reflections. 我想使用反射来做到这一点。

The basic code for creating a component given a String value can be something like: 给定String值来创建组件的基本代码可以是:

try
{
    String componentName = "Button";
    String classname = "javax.swing.J" + componentName;
    JComponent component = (JComponent)Class.forName(classname).newInstance();
    // add component to your panel

}
catch(Exception e) { System.out.println(e); }

However, if you use the above code the component is created without any parameters, so when you add it to the panel it will be very small. 但是,如果使用上面的代码,则将创建没有任何参数的组件,因此,将其添加到面板中时,它会很小。 So you will need extra code to set some of the properties of the component. 因此,您将需要额外的代码来设置组件的某些属性。

Not this code is not really reflection since you are not examining the class for possible different constructors to use. 由于您没有检查该类是否可能使用其他构造函数,因此该代码并不是真正的反映。 It is just a simple way to create an Object given a String name. 这只是创建给定字符串名称的对象的简单方法。

For a more flexible approach that does use reflection you can start to specify parameters. 对于使用反射的更灵活方法,您可以开始指定参数。 Here is an example that will pass a String in the creation of a JButton: 这是一个在创建JButton时传递String的示例:

String componentName = "Button";
String classname = "javax.swing.J" + componentName;
Class<?> type = Class.forName(classname);
Class[] argTypes = new Class[]{String.class};
Constructor constructor = type.getConstructor(argTypes);
Object[] parameters = new Object[]{"Button Text"};
JComponent component = (JComponent)constructor.newInstance(parameters);
add( component );

Here is a more general implementation that will allow you to create components with specific parameters as you create the component: 这是一个更通用的实现,可让您在创建组件时使用特定的参数创建组件:

import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.lang.reflect.*;

public class SwingComponentInfo
{
    private String name;
    private ArrayList<Class> types = new ArrayList<Class>();
    private ArrayList<Object> values = new ArrayList<Object>();

    public SwingComponentInfo(String name)
    {
        this.name = name;
    }

    public SwingComponentInfo(String name, Class type, Object value)
    {
        this.name = name;
        addParameter(type, value);
    }

    public void addParameter(Class type, Object value)
    {
        types.add(type);
        values.add(value);
    }

    public String getName()
    {
        return name;
    }

    public JComponent createComponent()
    {
        try
        {
            String classname = "javax.swing.J" + name;
            Class<?> type = Class.forName(classname);
            Constructor constructor = type.getConstructor( types.toArray( new Class[types.size()] ) );
            JComponent component = (JComponent)constructor.newInstance( values.toArray() );

            return component;
        }
        catch(Exception e) { e.printStackTrace(); }

        return null;
    }

    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame("SwingComponent");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setLayout( new FlowLayout() );
        SwingComponentInfo button = new SwingComponentInfo("Button", String.class, "New Button");
        frame.add(button.createComponent());

        SwingComponentInfo textField = new SwingComponentInfo("TextField", int.class, 10);
        frame.add(textField.createComponent());

        SwingComponentInfo textArea = new SwingComponentInfo("TextArea", int.class, 5);
        textArea.addParameter(int.class, 30);
        frame.add(textArea.createComponent());

        frame.setLocationByPlatform( true );
        frame.pack();
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater( () -> createAndShowGUI() );
/*
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
*/
    }
}

So now you can add any number of SwingComponentInfo Objects to a combo box. 因此,现在您可以将任意数量的SwingComponentInfo对象添加到组合框。 Then in your ActionListener for the combo box you get the Item selected and create the Swing component and then add it to your panel. 然后在组合框的ActionListener中,选择Item并创建Swing组件,然后将其添加到面板中。

You will need to create a custom renderer when you add the SwingComponentInfo object to the combo box to that the component name is displayed. SwingComponentInfo对象添加到显示组件名称的组合框中时,需要创建一个自定义渲染器。 Check out Combo Box With Custom Renderer for an easy way to do this. 请查看带有自定义渲染器的组合框,以轻松实现此目的。

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

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