我是 Java 和编程新手。我想通过选择表中的一行来在 JDialog
类中生成 int
并将该 int
发送到创建它的主类,或者,处理在主类中执行的按钮操作。主类在这里创建子类:
private void launchtable() {
DlgBeds dcl = new DlgBeds();
dcl.setLocationRelativeTo(this);
dcl.setVisible(true);
// I want in this place to put something like this:
txtCode.setText(subclassgeneratedint );
}
但首先我需要从子类中获取该 int。,我尝试在那里创建一个方法,我可以在 main 中使用该方法来返回 int
,但它改为使用第一行设置文本selected(默认选择),并且它无法返回更多行选择来不断更改 txtCode;该方法就像死在那里一样。
main 还可以为子类中的按钮执行操作
,但我猜参数需要是子类中的一个。
protected void actionPerformed[button in the subclass](ActionEvent e) {
//actions
}
我真的迷失了,我知道我需要更多的学习来解决这样的情况,但我需要尽快得到答案。
最佳答案
添加ListSelectionListener
到您的餐 table 。可以得到选中的行如图here并使用 PropertyChangeListener
将其传递到主窗口,如图 here和 here .
关于java - 在主类中处理按钮事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38194795/