Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

Button action listener should implement abstract method

Wed May 30, 2012 7:43 am

Code:
//Import packages
    
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
//main class
public class FirstGUI {
    
//Declare variables
            
static JFrame frame1;
            static 
Container pane;
            static 
JButton btnConnectbtnDisconnect;
            static 
JLabel lblServerlblUsernamelblPasswordlblPort;
            static 
JTextField txtServertxtUsernametxtPasswordtxtPort;
            static 
Insets insets;}
    public static 
void main(String[] args) {
         
//Create the frame
                    
frame1 = new JFrame ("Sample GUI Application");
                
//Set its size to 800x200 pixels
                    
frame1.setSize (800,200);
                    
//Prepare panel
                    
pane frame1.getContentPane();
                    
insets pane.getInsets();
                    
//Apply the null layout
                    
pane.setLayout (null);
    
btnConnect = new JButton ("Connect");
        
btnDisconnect = new JButton ("Disconnect");
        
lblServer = new JLabel ("Remote host:");
        
lblUsername = new JLabel ("Username:");
        
lblPassword = new JLabel ("Password:");
        
lblPort = new JLabel ("Port #:");
        
txtServer = new JTextField (10);
        
txtUsername = new JTextField  (10);
        
txtPassword = new JTextField  (10);
            
txtPort = new JTextField  (5);
    
pane.add (lblServer); //Add component to panel
        
lblServer.setBounds (insets.left 5insets.top 5lblServer.getPreferredSize().widthlblServer.getPreferredSize().height);
        
//Add all components to panel
            
pane.add (lblServer);
            
pane.add (lblUsername);
            
pane.add (lblPassword);
            
pane.add (lblPort);
            
pane.add (txtServer);
            
pane.add (txtUsername);
            
pane.add (txtPassword);
            
pane.add (txtPort);
            
pane.add (btnConnect);
            
pane.add (btnDisconnect);
             
            
//Place all components
            
lblServer.setBounds (insets.left 5insets.top 5lblServer.getPreferredSize().widthlblServer.getPreferredSize().height);
            
txtServer.setBounds (lblServer.getX() + lblServer.getWidth() + 5insets.top 5txtServer.getPreferredSize().widthtxtServer.getPreferredSize().height);
             
            
lblUsername.setBounds (txtServer.getX() + txtServer.getWidth() + 5insets.top 5lblUsername.getPreferredSize().widthlblUsername.getPreferredSize().height);
            
txtUsername.setBounds (lblUsername.getX() + lblUsername.getWidth() + 5insets.top 5txtUsername.getPreferredSize().widthtxtUsername.getPreferredSize().height);
             
            
lblPassword.setBounds (txtUsername.getX() + txtUsername.getWidth() + 5insets.top 5lblPassword.getPreferredSize().widthlblPassword.getPreferredSize().height);
            
txtPassword.setBounds (lblPassword.getX() + lblPassword.getWidth() + 5insets.top 5txtPassword.getPreferredSize().widthtxtPassword.getPreferredSize().height);
             
            
lblPort.setBounds (txtPassword.getX() + txtPassword.getWidth() + 5insets.top 5lblPort.getPreferredSize().widthlblPort.getPreferredSize().height);
            
txtPort.setBounds (lblPort.getX() + lblPort.getWidth() + 5insets.top 5txtPort.getPreferredSize().widthtxtPort.getPreferredSize().height);
         
            
btnConnect.setBounds (txtPort.getX() + txtPort.getWidth() + 5insets.top 5btnConnect.getPreferredSize().widthbtnConnect.getPreferredSize().height);
         
            
//Place disconnect button (start a new line!)
            
btnDisconnect.setBounds (insets.left 15lblServer.getY() + lblServer.getHeight() + 5btnDisconnect.getPreferredSize().widthbtnDisconnect.getPreferredSize().height);
            
frame1.setVisible (true);
            
//Set Look and Feel
            
try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}
                catch (
ClassNotFoundException e) {}
                catch (
InstantiationException e) {}
                catch (
IllegalAccessException e) {}
                catch (
UnsupportedLookAndFeelException e) {}
            
btnConnect.addActionListener(new btnConnectAction()); //Register action
            
public static class btnConnectAction implements ActionListener{
                public static class 
btnConnectAction implements ActionListener{
                            public 
void actionPerformed (ActionEvent e){
                            
System.out.println("You entered "+txtUsername.getText());
            }
    }

I did not understand whats the problem with this code every time it says"The type btnConnectAction must implement the inherited abstract method ActionListener.actionPerformed(ActionEvent)"or"The nested type btnConnectAction cannot hide an enclosing type"



Post a reply
  Related Posts  to : Button action listener should implement abstract method
 What is an abstract method     -  
 Text Listener for events TextListener-on TextArea-TextField     -  
 Convert link and button to custom button with good style     -  
 php extends in action     -  
 is_numeric in action     -  
 php SAX parser in action     -  
 JButtons implementing an Action     -  
 relationship an event-listener interface & event handler     -  
 how to implement an interface     -  
 How to implement HttpSessionListener in JSP     -