I have the following code:
package testOpdracht1; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.Date; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.io.InputStream; public class MainMenu extends JFrame implements KeyListener { public MainMenu() { initUI(); } public final void initUI() { JLabel label1 = new JLabel("text1"); add(label1); addKeyListener(this); setTitle("Bla"); setPreferredSize(new Dimension(400,250)); setMinimumSize(getPreferredSize()); setResizable(true); setLocationRelativeTo(null); setDefaultCloseOperation(EXIT_ON_CLOSE); } public void keyPressed(KeyEvent e) { } public void keyReleased(KeyEvent e) { } public void keyTyped(KeyEvent e) { } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { MainMenu ex = new MainMenu(); ex.setVisible(true); } }); } }
I want to change the text in the label when any button is pressed. How can i do this? I know that I can call methods from the JFrame class, since my MainMenu class extends it, but I cannot find a way to refer to the label element to change the value.
Yours faithfully,
Luxo
source share