gpt4 book ai didi

java - 动态设置 JRadioButton 上的文本

转载 作者:行者123 更新时间:2023-11-30 07:04:44 25 4
gpt4 key购买 nike

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 Varile 名称类似于 BT1、BT2、BT3...所以我尝试了

最佳答案

由于数据是动态的,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());
}

关于java - 动态设置 JRadioButton 上的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40336755/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com