2

Ok so I'm trying to get familier with Java, and I've made a simple thing where if you click a button then some text appears. How can I make it so the button and label are created in one class file, and put the code for when the button is clicked in another? Sorry if it sounds like a silly question.

Pastebin code:

package com.nate.derp;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class Derp {

    private JFrame frmHello;


    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Derp window = new Derp();
                    window.frmHello.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }


    public Derp() {
        initialize();
    }


    public void initialize() {
        frmHello = new JFrame();

        frmHello.setTitle("Hello");
        frmHello.setBounds(100, 100, 225, 160);
        frmHello.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmHello.getContentPane().setLayout(null);

        final JLabel helloLabel = new JLabel("Hello World!");
        helloLabel.setVisible(false);
        helloLabel.setBounds(40, 89, 145, 16);
        frmHello.getContentPane().add(helloLabel);

        final JButton btnClickMe = new JButton("Click Me!");
        btnClickMe.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                helloLabel.setVisible(true);
            }
        });


        btnClickMe.setBounds(54, 29, 117, 29);
        frmHello.getContentPane().add(btnClickMe);


    }
}
4
  • 3
    Post the code you have done so far, we'll help you re-organize it Commented Mar 28, 2012 at 22:07
  • A question about OOP design does not really make sense without the context of what each component/class is doing and how they are related. There are multiple ways to accomplish this (i.e., static class, component pattern, event handler pattern) and they are all relevant in certain situations. Commented Mar 28, 2012 at 22:07
  • I kept getting formatting errors when I tried to post it here so I hope that if I put it on pastebin that's fine (pastebin.com/K5Ad5XH6) Commented Mar 28, 2012 at 22:37
  • @Natatos I added your pastebin code to your question Commented Mar 29, 2012 at 6:00

1 Answer 1

1

You can do this by creating a JButton and adding an ActionListener, which can be implemented by another class.

So you first create the JButton:

Jbutton button = new JButton("hello");

Then add the Actionlistener:

button.addActionListener(new MyListener());

Where MyListener is your implementation class

class MyListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
       ...
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.