2016-03-30 13 views
0

Also habe ich ein bisschen Java auf dem College gemacht und schaue voraus, da wir noch nicht dabei sind. Ich habe versucht, mir Event Handling und Gui's bei zukünftigen Prüfungsfragen etc. beizulesen gui ist ziemlich handlich, aber Event-Handling nicht so viel ... Ich bin schon eine Weile hier gewesen und ich kann mich einfach nicht mehr um sie kümmern, ich versuche es zu bekommen, wenn der Knopf gedrückt wird Gibt die Länge() des JTextField in JLabel zurück. Jede Hilfe wird sehr geschätzt. DankLernereignis Handhabung

import java.awt.*; 
    import java.awt.event.*; 
    import javax.swing.*; 
    import javax.swing.event.*; 

public class guiWithCatchBlock extends JFrame implements ActionListener, MouseListener { 


guiWithCatchBlock() { 

    super("Attempting Event Handling"); 

    Container c = getContentPane(); 
    JButton stringLengthButton = new JButton("Get String Length"); 
    JTextField inputField = new JTextField(); 
    JLabel outputLabel = new JLabel("String Length = "); 
    stringLengthButton.addActionListener(this); 
    inputField.addActionListener(this); 
    outputLabel.addMouseListener(this); 
    c.add(stringLengthButton,BorderLayout.NORTH); 
    c.add(inputField,BorderLayout.CENTER); 
    c.add(outputLabel,BorderLayout.SOUTH); 
    setSize(400, 300); 
    show(); 

} 

public static void main(String args[]) { 

    guiWithCatchBlock testAction = new guiWithCatchBlock(); 

} 

public void actionPerformed(ActionEvent e) { 

    System.out.print(paramString()); 

} 

@Override 
public void mouseClicked(MouseEvent arg0) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void mouseEntered(MouseEvent arg0) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void mouseExited(MouseEvent arg0) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void mousePressed(MouseEvent arg0) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void mouseReleased(MouseEvent arg0) { 
    // TODO Auto-generated method stub 

} 

}

Antwort

0

Sie müssen die Action auf die Schaltfläche

stringLengthButton.addActionListener(this); 

Sie diese, wie Sie die Action Klasse implementieren können passieren. Jetzt das Ereignis, das wird Feuer ist:

public void actionPerformed(ActionEvent e) { 
    // Do GUI manipulations 
    System.out.print(paramString()); 
} 
0

Wenn Sie versucht haben, Ihre inputfield und output in Ihrem Konstruktor zu deklarieren, wird es schwer sein, später zu beziehen, besser als Klassenmitglied zu erklären.
In Ihrem actionPerform, versucht, die Textlänge und wieder Label wie diese

@Override 
public void actionPerformed(ActionEvent e) { 
    outputLabel.setText("String Length = " + inputField.getText().length()); 
} 

Completed-Code als

public class guiWithCatchBlock extends JFrame implements ActionListener, MouseListener { 
    JTextField inputField; 
    JLabel outputLabel; 
    guiWithCatchBlock() { 
     super("Attempting Event Handling"); 
     Container c = getContentPane(); 
     JButton stringLengthButton = new JButton("Get String Length"); 
     inputField = new JTextField(); 
     outputLabel = new JLabel("String Length = "); 
     stringLengthButton.addActionListener(this); 
     inputField.addActionListener(this); 
     outputLabel.addMouseListener(this); 
     c.add(stringLengthButton, BorderLayout.NORTH); 
     c.add(inputField, BorderLayout.CENTER); 
     c.add(outputLabel, BorderLayout.SOUTH); 
     setSize(400, 300); 
     show(); 

    } 

    public static void main(String args[]) { 
     guiWithCatchBlock testAction = new guiWithCatchBlock(); 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     outputLabel.setText("String Length = " + inputField.getText().length()); 
    } 

    @Override 
    public void mouseClicked(MouseEvent arg0) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void mouseEntered(MouseEvent arg0) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void mouseExited(MouseEvent arg0) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void mousePressed(MouseEvent arg0) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void mouseReleased(MouseEvent arg0) { 
     // TODO Auto-generated method stub 

    } 
} 
zu erhalten