jButton立即点击

时间:2016-07-17 10:29:02

标签: java swing button actionlistener

如前所述,我Main Frame包含JXTaskPainContainerjPanel1jButton2以及AddButton,其中包含动态btn。布局为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上的Main frame时,我想从jButton1致电Test panel并获取他们的actionCommand。点击addButton后,它会添加动态btn = new JButton("Test"),并引用Test panel。我想要做的是添加动态btn,例如,如果我们添加btn2, btn3, btn4,然后使用jButton2,我想doClick()所有jButton1 actionCommands addMenu()

当我在Test.jButton1.setActionCommand(Integer.toString(count)); Test.jButton1.doClick(); 中添加这行代码时:

addButton

然后,每次点击jButton1btn2, btn3, btn4 jButton1的所有jButton2都会被点击。但是,当我想使用Main frame中的private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { Test.jButton1.setActionCommand(Integer.toString(count)); Test.jButton1.doClick(); } 点击所有jButton1时:

btn4

但在这种情况下,只点击了上一个jButton1(即btn2, btn3, btn4)。 我想问一下,如何使用jButton2一次点击动态添加的$ImageName = '/IMAGES/testImage.jpg'; //Download the Image File $base_url = 'https://image.jimcdn.com/app/cms/image/transf/none/path/sa6549607c78f5c11/image/ia62ed3191fcc424f/version/1457278719/athens-european-best-destinations.jpg'; $image_size = getimagesize($base_url); $image_width = $image_size[0]; $image_height = $image_size[1]; $src_image = imagecreatefromjpeg($base_url); //Copy Image Object To File imagejpeg($src_image, $ImageName, 100); <pre> {}?}? 任何帮助都非常感谢。

1 个答案:

答案 0 :(得分:0)

使用getter而不是静态调用&#39; Test.jButton1&#39;

//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();