如何在Java中使用一个按钮使另一个按钮重复按下多次?

 我来答
daay1986
2011-03-03 · TA获得超过6018个赞
知道大有可为答主
回答量:2208
采纳率:0%
帮助的人:1466万
展开全部
public class NewJFrame2 extends javax.swing.JFrame {

/** Creates new form NewJFrame2 */
public NewJFrame2() {
initComponents();
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
java.awt.GridBagConstraints gridBagConstraints;

jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setName("Form"); // NOI18N
getContentPane().setLayout(new javax.swing.BoxLayout(getContentPane(), javax.swing.BoxLayout.LINE_AXIS));

jButton1.setText(org.openide.util.NbBundle.getMessage(NewJFrame2.class, "NewJFrame2.jButton1.text")); // NOI18N
jButton1.setName("jButton1"); // NOI18N
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
getContentPane().add(jButton1);

jButton2.setText(org.openide.util.NbBundle.getMessage(NewJFrame2.class, "NewJFrame2.jButton2.text")); // NOI18N
jButton2.setName("jButton2"); // NOI18N
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
getContentPane().add(jButton2);

pack();
}// </editor-fold>

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
System.out.println("乌龟呼叫王八,收到请回答!");
this.jButton2.doClick();
this.jButton2.doClick();
this.jButton2.doClick();
this.jButton2.doClick();
}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
System.out.println("我是王八,已收到!");
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {
new NewJFrame2().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
// End of variables declaration
}
追问
org.openide晕 我的eclipse没有哇~~~编译不了啊
追答
org.openide.util.NbBundle.getMessage(NewJFrame2.class, "NewJFrame2.jButton2.text")  吧这句话改成字符串就可以了,我是netbeans自动生成的代码,你自己改一下就可以了。
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式