我想用以下方法創建一個gui生成器(如NetBeans中的一個):我已經制作了一個JSplitPane
,在左側我有一個JTextField
,其中我寫了一個名稱Swing Component
並在右側我想要顯示該組件。事情是這樣的:在Java中製作我自己的GUI生成器NetBeans
我想打,使用Reflections
。這是第一次使用它,所以請善待我並解釋我如何做到這一點。
謝謝!
我想用以下方法創建一個gui生成器(如NetBeans中的一個):我已經制作了一個JSplitPane
,在左側我有一個JTextField
,其中我寫了一個名稱Swing Component
並在右側我想要顯示該組件。事情是這樣的:在Java中製作我自己的GUI生成器NetBeans
我想打,使用Reflections
。這是第一次使用它,所以請善待我並解釋我如何做到這一點。
謝謝!
在左側我有一個JTextField,我寫Swing組件
井的名字,你真的不應該用一個JTextField。用戶不知道什麼是有效值。相反,你應該使用JCombobox。然後,您只需從所選項目中獲取組件名稱
我想使用Reflections進行設置。
創建組件給定一個字符串值可以是這樣的基本代碼:
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); }
但是,如果使用上面的代碼創建的組件不帶任何參數,所以當你添加它到面板它將會非常小。所以你需要額外的代碼來設置組件的一些屬性。
不是這個代碼不是真正的反射,因爲你沒有檢查可能使用的不同構造函數的類。這只是一個簡單的方法來創建一個給定字符串名稱的對象。
對於使用反射的更靈活的方法,您可以開始指定參數。這是將在創建一個JButton的傳遞的字符串,例如:
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);
這裏是一個更通用的實現作爲創建組件,讓您創建具有特定參數的組件:
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();
}
});
*/
}
}
因此,現在您可以將任意數量的SwingComponentInfo
對象添加到組合框。然後在組合框的ActionListener中,選擇Item並創建Swing組件,然後將其添加到面板。
當您將SwingComponentInfo
對象添加到組合框以顯示組件名稱時,您將需要創建自定義渲染器。查看組合框與自定義渲染器的一個簡單的方法來做到這一點。
你的問題似乎過於寬泛,因爲它涉及到Java的許多子部分。我建議您嘗試將問題分解爲其組成步驟,然後嘗試單獨解決每一步。那麼如果你遇到困難,你總是可以回來問一個更具體和可回答的問題,並展示你的相關代碼。投票結束。 –