+ 1

Why btnRefresh can't response to the button click after first time?

package javaapplication14; import javax.swing.JFrame; /** * * @author xuji28 */ public class JavaApplication14 { /** * @param args the command line arguments */ public static void main(String[] args) { new JMainFrame(); } } package javaapplication14; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; /** * * @author xuji28 */ public class JMainFrame { JFrame mainFrm=null; JButton btn=null; int i=0; JPanelTest panel=null; public JMainFrame() { mainFrm=new JFrame("Test"); mainFrm.setLayout(new GridLayout(2,1)); btn=new JButton("OK"); btn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { btn.setText("counts: "+String.valueOf(i++)); } }); mainFrm.add(btn); panel=new JPanelTest(); mainFrm.add(panel.getPanel()); mainFrm.setSize(100, 50); mainFrm.pack(); mainFrm.setVisible(true); } } package javaapplication14; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JPanel; /** * * @author xuji28 */ public class JPanelTest{ JPanel panel=null;//new JPanel(); JButton btnRefresh=null; int i=0; public JPanel getPanel() { btnRefresh=new JButton("Refresh"); btnRefresh.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent agr0) { btnRefresh.setText("clicked:"+String.valueOf(i));//???Why btnRefresh can't //response to the button click? } }); panel=new JPanel(); panel.add(btnRefr

4th Jan 2017, 8:15 AM
Job
1 Odpowiedź
0
if i recall correctly, if you are changing name of button you should call frame#validate and frame#repaint try these.
6th Jan 2017, 5:49 PM
T. Harsh Vardhan
T. Harsh Vardhan - avatar