Tag: actionlistener

从另一个类向JButton添加actionListener

所以我有两个类testPanel和testFrame。 所有按钮都在testPanel类中。 我想将ActionListeners添加到testFrame类的Jbuttons中。 我该怎么做呢? パ: public class testPanel extends JPanel{ JLabel codeLbl = new JLabel(“Code”); JLabel titleLbl = new JLabel(“Title”); JLabel priceLbl = new JLabel(“Price”); JTextField codeTxt = new JTextField(20); JTextField titleTxt = new JTextField(20); JTextField priceTxt = new JTextField(20); JButton addBtn = new JButton(“Add”); JButton updateBtn = new JButton(“Update”); JButton delBtn = new JButton(“Delete”); […]

如何删除动作侦听器?

所以我正在制作一个国际象棋游戏,但只与骑士。 这是移动骑士的方法 public void caballo(final int row, final int column) { final JButton current = mesa[row][column]; current.setIcon(image); panel.repaint(); acciones(row, column, current); } public void acciones(final int row, final int column, final JButton current) { for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j = 0 && row = […]

双击JTree节点并获取其名称

如何双击JTree节点并获取其名称? 如果我调用evt.getSource() ,似乎返回的对象是一个JTree。 我无法将其强制转换为DefaultMutableTreeNode。

如何在java中动态添加项目时,如何避免触发JComboBox的actionlistener事件?

我需要你对以下任务的建议和指导。 我有一个框架有两个JComboBoxes,假设它们被命名为combo1和combo2,一个JTable和其他组件。 在使用上述组件可见框架的初始阶段。 combo1combobox中填充了一些值,但在初始阶段没有选择任何值,combo2combobox被禁用,表格为空。 我在combo1和combo2上添加了一个actionListener。 combo1中有两种类型的值,假设这些值是type1和type2。 条件1:当我们从Combo1中选择值type1时,将调用actionListener方法combo1,该方法调用combo2保持禁用的方法,并将一些行添加到与combo1中的选定值type1相关的表中。 条件2:当我们从combo1中选择值type2时,将调用actionListener方法combo1,该方法调用一个方法,该方法使combo2填充了与type2相关的一些值并启用但是没有从combo2中选择任何值,并且table也应保持为空,直到我们选择combo2中的任何值。 每次向combo2添加值时,表都会触发combo2的动作侦听器方法。 在combo2的actionListener方法中,它获取了combo2选择的值,但是这里没有选择的combo2值导致NullPointerException。 那么我应该怎么做才能在将值添加到combo2之后执行combo2的动作列表器方法。

奇怪的JFrame行为

我有以下程序,它运行时有一些非常奇怪和不需要的行为。 它应该有两个按钮,“开始”和“停止,但当我点击”开始“另一个按钮显示在”开始“的正下方。这是我正在谈论的打印屏幕: 我做错了什么,如何解决这个丑陋的问题? 这是代码: import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.Random; public class TwoButtonsTest { JFrame frame; Timer timer; boolean isClicked; public static void main(String[] args) { TwoButtonsTest test = new TwoButtonsTest(); test.go(); } public void go() { frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(500, 500); JButton startButton = new JButton(“Start”); startButton.addActionListener(new StartListener()); JButton stopButton […]

在按钮操作上向JPanel添加背景图像

调用JButton时,将背景图像添加到JPanel / JLabel的最佳方法是什么? 我知道如何获得JButton动作等。 当按下该按钮时,我无法弄清楚或找到一种方法来改变背景图像。

菜单上的java动作监听器,而不是菜单项

我需要启蒙。 如何添加动作actionListener事件绑定到菜单,而不绑定到菜单ITEM这里是演示代码,有效(在menuITEM上).. menuFileItem.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e) { System.out.println(“It works”); } } ); 但是当我尝试相同的,但只是在菜单本身它不起作用! menuFile.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e) { System.out.println(“Plz work… 🙁 “); } } ); 是否可以将监听器添加到菜单中? 我教过听众可以添加到一切。

Java:使用actionlistener在该类的对象上调用另一个类中的函数

基本上我想要做的是获得一个开始按钮来启动在另一个类中运行的方法并对另一个对象起作用。 我的听众代码: button1a.addActionListener(new ActionListener() { public void actionPerformed (ActionEvent event) { // Figure out how to make this work //sim.runCastleCrash(); } } ); 我的其他类的代码: public static void main(String[] args) { CastleCrash sim; sim = new CastleCrash(); } 和 public void runCastleCrash() { System.out.println(“Castle Crash is beginning…”); //Other method parts here to be added } 我觉得这不会太难,但我错过了一块。

JDialog的动作监听器,用于单击按钮

我有主要的应用程序在哪里是值与表。 然后,我点击“添加”按钮,新的CUSTOM(我自己制作)JDialog类型弹出窗口出现。 在那里我可以输入值,做一些滴答并点击“确认”。 所以我需要从对话框中读取该输入,因此我可以将此值添加到主应用程序中的表中。 当按下“确认”按钮时我该怎么听,所以我可以在那之后读取该值? addISDialog = new AddISDialog(); addISDialog.setVisible(true); addISDialog.setLocationRelativeTo(null); //somekind of listener… //after “Confirm” button in dialog was pressed, get value value = addISDialog.ISName;

嵌套类vs实现ActionListener

创建实现ActionListener的嵌套类是否有任何好处或缺点: public class Foo{ Foo(){ something.addActionListener(new ButtonListener()); } //… private class ButtonListener implements ActionListener{ public void actionPerformed(ActionEvent e){ //… } } } 与在主类本身中实现ActionListener相比: public class Foo implements ActionListener{ Foo(){ something.addActionListener(this); } //… public void actionPerformed(ActionEvent e){ //… } } 我经常看到这两个例子,只是想知道是否有“最佳实践”。