【问题标题】:setText on JRadioButton dynamicallyJRadioButton 上的 setText 动态
【发布时间】:2017-03-13 05:10:58
【问题描述】:

I have one JInternalframe in which there are many JRadioButton and JLabel. I want to Retrive Data from Database and display on this JRadioButton and JLabel
由于数据库中可以有任意数量的行将被获取。 我已成功从数据库中获取数据,但无法在 JRadioButton 和 JLabel 上设置它

void setData(String s1, String s2, String s3, int cnt) {
       //Setting JRadioButton and JLabel Coding Part Here
  }

在变量 s1,s2,s3 中的上述函数中存在我想要显示的数据,并且在变量 cnt 中有计数器的值在 resultset.next 上递增( )

谁能给我一些关于如何在 JRadiobutton 和 JLabel 上显示数据的提示
JRadioButton Variale 名称类似于 BT1,BT2,BT3... 所以我尝试了

【问题讨论】:

  • 正如你在问题标题中所说的那样。假设您的JRadioButton 的引用是btn,只需调用btn.setText(s1)JLabel 确实有一个公共的setText 方法,JRadioButtonAbstractButton 继承它。
  • 感谢您的建议,但如果提取的记录超过 1 条,我想将其显示在单独的 JRadioButton 和 JLabel 中,因此每次变量名称都会不同。例如,如果提取了 2 条记录所以数据应该显示在 BT1 和 BT2 等请参阅附件截图了解更多详情

标签: java variables dynamic jlabel jradiobutton


【解决方案1】:

由于数据是动态的,所以 GUI 也应该是动态的。

考虑以下示例:

import javax.swing.JLabel;
import javax.swing.JRadioButton;

public class GuiRow {

  /** jRadioButton */
  private JRadioButton jRadioButton;

  /** studentIdLabel */
  private JLabel studentIdLabel;

  /** nameLabel */
  private JLabel nameLabel;

  /** courseLabel */
  private JLabel courseLabel;

  /**
   * Constructs a new instance.
   * 
   * @param studentIdtext
   * @param nameText
   * @param courseText
   */
  public GuiRow(String studentIdtext, String nameText, String courseText) {
    this.setjRadioButton(new JRadioButton(""));
    // TODO configure radio button

    this.setStudentIdLabel(new JLabel(studentIdtext));
    this.setNameLabel(new JLabel(nameText));
    this.setCourseLabel(new JLabel(nameText));
  }

  /**
   * Get jRadioButton.
   * 
   * @return jRadioButton
   */
  public JRadioButton getjRadioButton() {
    return this.jRadioButton;
  }

  /**
   * Set jRadioButton.
   * 
   * @param jRadioButton
   */
  public void setjRadioButton(JRadioButton jRadioButton) {
    this.jRadioButton = jRadioButton;
  }

  /**
   * Get studentIdLabel.
   * 
   * @return studentIdLabel
   */
  public JLabel getStudentIdLabel() {
    return this.studentIdLabel;
  }

  /**
   * Set studentIdLabel.
   * 
   * @param studentIdLabel
   */
  public void setStudentIdLabel(JLabel studentIdLabel) {
    this.studentIdLabel = studentIdLabel;
  }

  /**
   * Get nameLabel.
   * 
   * @return nameLabel
   */
  public JLabel getNameLabel() {
    return this.nameLabel;
  }

  /**
   * Set nameLabel.
   * 
   * @param nameLabel
   */
  public void setNameLabel(JLabel nameLabel) {
    this.nameLabel = nameLabel;
  }

  /**
   * Get courseLabel.
   * 
   * @return courseLabel
   */
  public JLabel getCourseLabel() {
    return this.courseLabel;
  }

  /**
   * Set courseLabel.
   * 
   * @param courseLabel
   */
  public void setCourseLabel(JLabel courseLabel) {
    this.courseLabel = courseLabel;
  }
}

这是一个简单的对象,用于在容器中创建和组织一行(可能是 JPanel,对吧?)

当您解析结果集时,您需要构建此类对象的列表并在其中添加数据。

这是一个关于如何解析和显示数据的简单示例,您可以将其拆分为 2 个或多个方法并将它们放置在适当的实体中:

int resultsetSize = resultset.getFetchSize();

//init the list 
List<GuiRow> guiRowList = new ArrayList<GuiRow>(resultsetSize);
while (resultset.next()) {
  //get the column values
  String id = resultset.getString("studentId");
  String name = resultset.getString("studentName");
  String course = resultset.getString("course");
  //create the the entry 
  GuiRow guiRow = new GuiRow(id, name, course);
  //add it to the list
  guiRowList.add(guiRow);
}

//now that you have a nice list of rows add them one by one to the container
JPanel container = new JPanel(new GridLayout(resultsetSize, 4));
//further container configuration could be done here ...

for (GuiRow row : guiRowList) {
  container.add(row.getjRadioButton());
  container.add(row.getStudentIdLabel());
  container.add(row.getNameLabel());
  container.add(row.getCourseLabel());
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-03
    • 1970-01-01
    • 2010-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多