我制作了jbutton并向其添加了ActionListner,但public void actionPerformed(ActionEvent e){}中的代码执行了两次。它打印两次test,而不是只打印一次
JButton testbut=new JButton("Test");
ListenForButton2 l=new ListenForButton2();
testbut.addActionListener(l);
private class ListenForButton2 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == testbut){System.out.println("tEST");}
}
}
这里是完整的代码,以防这是没有意义的。看起来问题是添加两个动作监听器到我的按钮。“if(testbut.GetActionListeners().length<1))testbut.AddActionListener(l);”修复了它。但是你能找到我在哪里添加了两个动作监听器到测试按钮吗
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Arc2D;
import java.awt.geom.CubicCurve2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.QuadCurve2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Game extends JFrame {
JButton but1 = new JButton("MakeMoney");
JButton button1 = new JButton("Current money");
JButton testbut=new JButton("Make money");
int money = 0;
double currentMoney;
String moneyString = "";
String box1txt="Text for box one";
String[] boxtext={"Newspaper Delivery","Car wash","Pizza Delivery","Donut shop","Shrimp boat","Hockey team","Movie Studio","Bank","Oil Company"};
String[] prices={"60.00","720.00","8,640.00","103,680.00","1,244,160.00","14,929,920.00","179,159,040.00","2.149 billion","25.798 billion"};
public static void main(String[] args) {
new Game();
}
public Game() {
try {
this.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("C:/Users/TPC/workspace/ProjectMoney/Resources/backgroundForApp.png")))));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.setLayout(new BorderLayout());
button1.setContentAreaFilled(false);
ListenForButton lforButton = new ListenForButton();
but1.addActionListener(lforButton);
JPanel thePanel = new JPanel();
thePanel.add(button1);
thePanel.add(but1);
//thePanel.add(testbut);
this.setSize(1042, 617);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Project Money");
this.add(thePanel, BorderLayout.NORTH);
this.add(new DrawStuff(), BorderLayout.CENTER);
this.setResizable(true);
this.setVisible(true);
Sound sound1 = new Sound();
String sound = "file:C:/Users/TPC/Downloads/sound.wav";
sound1.playMusic(sound);
}
private class ListenForButton implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == but1) {
money += 10;
moneyString = Integer.toString(money);
button1.setText("$" + moneyString);
}
}
}
private class ListenForButton2 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == testbut){System.out.println("tEST");}
}
}
private class DrawStuff extends JComponent {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D graph2 = (Graphics2D) g;
graph2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Shape drawRoundRec = new RoundRectangle2D.Double(170,40,250,66,45, 45);
//first num xpos,secound num ypost,third num sirina,fourht num duzina
int x1=170; int y1=40; int x2=250; int y2=66; int def=45;
Shape drawRoundRec2=new RoundRectangle2D.Double(x1,(y1+80),x2,y2,def,def);
Shape drawRoundRec3=new RoundRectangle2D.Double(x1,(y1+80+80),x2,y2,def,def);
Shape drawRoundRec4=new RoundRectangle2D.Double(x1,(y1+80+80+80),x2,y2,def,def);
Shape drawRoundRec5=new RoundRectangle2D.Double(x1,(y1+80+80+80+80),x2,y2,def,def);
Shape drawRoundRect6=new RoundRectangle2D.Double(x1+330,40,250,66,def, def);
Shape drawRoundRect7=new RoundRectangle2D.Double(x1+330,(y1+80),250,66,def, def);
Shape drawRoundRect8=new RoundRectangle2D.Double(x1+330,(y1+80+80),250,66,def, def);
Shape drawRoundRect9=new RoundRectangle2D.Double(x1+330,(y1+80+80+80),250,66,def, def);
Shape drawRoundRect10=new RoundRectangle2D.Double(x1+330,(y1+80+80+80+80),250,66,def, def);
graph2.setPaint(Color.BLACK);
graph2.setColor(Color.LIGHT_GRAY);
graph2.fill(drawRoundRec); graph2.fill(drawRoundRec2);
graph2.fill(drawRoundRec3); graph2.fill(drawRoundRec4); graph2.fill(drawRoundRec5); graph2.fill(drawRoundRect6);
graph2.fill(drawRoundRect7); graph2.fill(drawRoundRect8); graph2.fill(drawRoundRect9); graph2.fill(drawRoundRect10);
graph2.setPaint(Color.BLACK);
graph2.draw(drawRoundRec2); graph2.draw(drawRoundRec3); graph2.draw(drawRoundRec4);
graph2.draw(drawRoundRec5); graph2.draw(drawRoundRect6); graph2.draw(drawRoundRect7); graph2.draw(drawRoundRect8);
graph2.draw(drawRoundRect9); graph2.draw(drawRoundRect10);
Font font=new Font("Serif",Font.PLAIN,30);
g.setFont(font); g.drawString(box1txt,190,80); g.drawString(boxtext[0],190,150); g.drawString(boxtext[1],190,150+80); g.drawString(boxtext[2],190,150+90+70);
g.drawString(boxtext[3],190,150+90+70+80);
g.drawString(boxtext[4],520,80); g.drawString(boxtext[5],520,150); g.drawString(boxtext[6],520,150+80); g.drawString(boxtext[7],520,150+90+70);
g.drawString(boxtext[8],520,150+90+70+80);
g.drawString(prices[0],190,150+30); g.drawString(prices[1],190,150+80+30); g.drawString(prices[2],190,150+90+70+30);
g.drawString(prices[3],190,150+90+70+80+30);
g.drawString(prices[4],520,80+25); g.drawString(prices[5],520,150+30); g.drawString(prices[6],520,150+80+30); g.drawString(prices[7],520,150+90+70+30);
g.drawString(prices[8],520,150+90+70+80+30);
testbut.setLocation(180,70);
Dimension d = new Dimension(100,40);
testbut.setSize(d);
ListenForButton2 l=new ListenForButton2();
if (testbut.getActionListeners().length<1) testbut.addActionListener(l);
this.add(testbut);
}
}
}
确保只添加一次操作侦听器。
您可以通过testbut.getActionListeners().length
获得侦听器计数。
在扩展JComponent的DrawStuff类的paintComponent中,只添加一次侦听器。与第一个监听器相同。
我在MariaDb服务器中有一个触发器,其代码如下: 我正在尝试在我拥有MySQL(5.7+版本)的生产服务器上创建相同的触发器。我会得到这样的错误: 误差静态分析: 分析中发现错误6处。 语句开头意外。(在位置0的“创建触发器”附近) 语句开头意外。(靠近位置15处的“”) 语句开头意外。(靠近位置39处的“插入之前”) 语句开头意外。(第57位“”附近) 语句开头意外。(靠近位置74处的“FO
本文向大家介绍JS代码触发事件代码实例,包括了JS代码触发事件代码实例的使用技巧和注意事项,需要的朋友参考一下 这篇文章主要介绍了js代码触发事件代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教程。
本文向大家介绍Oracle触发器实例代码,包括了Oracle触发器实例代码的使用技巧和注意事项,需要的朋友参考一下 Oracle触发器,用于选单后修改选单的表的触发动作。 以上所述是小编给大家介绍的Oracle触发器实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对呐喊教程网站的支持!
我觉得有一个简单的解决办法,但由于某些原因,我不能把我的头围绕它。 我有一个情况,一个按钮必须先点击,才能点击另一个。用户为这2个JButtons选择的选项将决定程序的下一步。这意味着我必须调用actionListener两次,对吗?我如何在一个actionPerformed方法中做到这一点? 如果actionPerformed方法中的(e.getSource()==Square[1][4]&&e
问题内容: 出于某种原因,每当我在循环使用时按一下该键,此后的代码将立即执行两次。如果不在循环中,它将执行一次!为什么是这样? 这是一个测试它的代码片段: 问题答案: 发生这种情况的原因是,当您按Enter键时,System.in.read()将注册两个字符。应该将其用作扫描仪的更复杂版本,而不是使用扫描仪来执行Scanner.nextLine()或Scanner.nextInt()来返回字符串或
问题内容: 我有以下代码,但我的问题是该事件被触发了两次。我不知道是什么原因造成的。我怀疑是由供应商前缀引起的,但并非如此。即使我只离开,它仍然会开火两次。 CSS JS 问题答案: 对于您和,每个已转换的属性都会触发。 您可以在访问与事件关联的属性。 没有“ transition s end”事件,因此您可能需要一些技巧,例如仅过滤其中一个过渡属性的回调处理。例如: ps。没有浏览器会触发该事件