gpt4 book ai didi

java - jButton 立即点击

转载 作者:太空宇宙 更新时间:2023-11-04 12:25:40 25 4
gpt4 key购买 nike

正如我之前所描述的,我有Main Frame,其中包含JXTaskPainContainerjPanel1jButton2以及添加动态btnAddButton。布局为 here .

主类:

public class Main extends javax.swing.JFrame{
public static JXTaskPaneContainer taskPaneContainer;
public static int count = 2;

public Main() {
initComponents();

taskPaneContainer = new JXTaskPaneContainer();
jPanel1.setLayout(new GridLayout(1,1));
jPanel1.add(taskPaneContainer, BorderLayout.EAST);
}

private void addMenu(String Title) {
final JXTaskPane actionPane = new JXTaskPane();
actionPane.setSpecial(true);
JPanel opaquePanel = new JPanel();
opaquePanel.setOpaque(true);
JButton btn = new JButton("Test");
btn.setActionCommand(Integer.toString(count));
cardLayoutPanel.add(new Test(count), "Test"+Integer.toString(count));

opaquePanel.add(btn);
actionPane.add(opaquePanel);
taskPaneContainer.add(actionPane);
}

private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {
count++;
addMenu("Menu " + count);
}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
Test.jButton1.doClick(); //call from Test panel
}
}

这里测试面板:

public class Test extends javax.swing.JPanel{

/**
* Creates new form Test
* @param val
*/

public Test(int val){
initComponents();
jButton1.setActionCommand(Integer.toString(val));
}

public Test(){
initComponents();
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt){
System.out.println("test"+evt.getActionCommand());
}
}

现在,当我单击主框架上的jButton2时,我想从测试面板调用jButton1并获取其actionCommand。当我们点击addButton时,它会添加动态btn = new JButton("Test"),并且它引用测试面板。我想做的是添加动态btn,例如,如果我们添加btn2、btn3、btn4,,然后使用jButton2,我想对所有jButton1actionCommands进行doClick()

当我在 addMenu() 中添加这行代码时:

Test.jButton1.setActionCommand(Integer.toString(count));
Test.jButton1.doClick();

然后,每次点击 addButton 时,btn2、btn3、btn4 的所有 jButton1 都会立即被点击。但是当我想在主框架中使用jButton2单击所有jButton1时:

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
Test.jButton1.setActionCommand(Integer.toString(count));
Test.jButton1.doClick();
}

但在本例中,仅单击了最后一个 jButton1(即 btn4)。我想问一下,如何使用jButton2一次性点击所有动态添加的jButton1btn2、btn3、btn4?非常感谢任何帮助。

最佳答案

使用 getter 而不是静态调用“Test.jButton1”

//include imports to make it an MCVE
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;
public class Test extends javax.swing.JPanel{

JButton jButton1;

public Test(int val){
initComponents();
jButton1.setActionCommand(Integer.toString(val));
}

public Test(){
initComponents();
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt){
System.out.println("test"+evt.getActionCommand());
}

/**
* Add a getter, so you can get jButton1
*/
public JButton getjButton1() {
return jButton1;
}
}

如果 jPanel1 是 Test 的实例,您可以通过以下方式获取 jButton1:

    JButton jButton1 = jPanel1.getButton1();

或者点击它

jPanel1.getButton1().doClick();

关于java - jButton 立即点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38420263/

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