简体   繁体   English

将变量及其值添加到Java GUI

[英]Adding variables with their values to a Java GUI

I want to add to a Java JFrame a list of my class's fields names, along with their respective values. 我想在Java JFrame添加我的类的字段名称列表及其各自的值。 Those fields can of course change value during the program's execution and i want this change to be displayed dynamically in my GUI. 这些字段当然可以在程序执行期间改变值,我希望这个更改能够在我的GUI中动态显示。 If for example i have a field private int networkId; 例如,我有一个字段private int networkId; in a class BaseStation , how can i make it appear in the frame of a class BaseStationFrame ? 在类BaseStation ,如何让它出现在类BaseStationFrame的框架中?

我不是100%理解这个问题,但是如果通过JavaBeans, l2fprod PropertySheet会做你需要的吗?

A common approach would be to access the methods from the component that allow you to manipulate the component values. 一种常见的方法是从组件中访问允许您操作组件值的方法。

Here a simple example using a text area. 这是一个使用文本区域的简单示例。

private final static String newline = "\n";
private JTextArea textArea;


public void init() {
   //Make sure your components are not null;
   textArea = new JTextArea();  
}   

public void actionPerformed(ActionEvent evt) {
    String text = textArea.getText();
    textArea.append(textArea + newline);
}

//Get set methods

Depending on your needs and variable type, you might need to parse or use special methods. 根据您的需要和变量类型,您可能需要解析或使用特殊方法。 I would recomend you to read the documentation for the javax.swing package. 我建议你阅读javax.swing包文档。

Also in oracles site there is a great tutorial on javax.swing GUI 同样在oracles网站上有一个关于javax.swing GUI的很棒的教程

Well...you could store static final Strings that contain the names of your fields, and design some association between the String representation of the name of a field, and the value of that field. 嗯...您可以存储包含字段名称的静态最终字符串,并设计字段名称的字符串表示与该字段值之间的某种关联。

Or, you could use a Map for all of your fields, defining key-value pairs for them. 或者,您可以为所有字段使用Map,为它们定义键值对。 When you want to call the fields, you do a Map.get(), and when you want to print them, you could do something like 当你想调用字段时,你做一个Map.get(),当你想要打印它们时,你可以做类似的事情

Set s = map.keySet()
for(Object o : s) {
    print(o.toString() + " = " + map.get(o).toString())
}

This is, of course, Sudocode to hopefully get the ball rolling for you 当然,这就是Sudocode,希望能让你顺利举起

Good Luck :) 祝好运 :)

If you want your UI to have access to private fields of your class, you won't have much options 如果您希望您的UI可以访问您班级的私人字段,那么您将没有太多选择

  • Use reflection to access those fields (not recommended) 使用反射访问这些字段(不推荐)
  • Make those fields public (not recommended either) or package visible and place all code in one package (not recommended either) 使这些字段公开(不推荐)或包可见,并将所有代码放在一个包中(不推荐)
  • Add decent getters and setters and fire PropertyChangeEvent s when those fields change. 添加不错的getter和setter,并在这些字段更改时触发PropertyChangeEvent

Your UI can then use the getter to obtain the initial value, and use the events to update the UI when the value of a field has been changed. 然后,您的UI可以使用getter获取初始值,并在更改字段值时使用事件更新UI。 You could even write a fairly generic UI component for this by using reflection to obtain all the getters, but if it is only for a few classes this might be overkill 您甚至可以通过使用反射来获取所有getter来为此编写一个相当通用的UI组件,但如果它仅用于少数几个类,那么这可能是矫枉过正的

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

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