简体   繁体   中英

How to make a class a component in Java Swing?

I am a beginner in Java programming so I do not know if I may be using the correct terms here. Basically, I have an assignment to program a little applet that changes the color of the background to whatever of the 4 color buttons is pressed. I was given a sample code with ActionListener and was told to use MouseListener to implement it.

I was able to successfully program it to work and then the requirements changed. Below is my current code that works (before the requirements changed).

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

class ButtonPanel extends JPanel implements MouseListener{
    private JButton abutton, bbutton, cbutton, dbutton;

    public ButtonPanel(){
        abutton = new JButton("Cyan");
        bbutton = new JButton("Orange");
        cbutton = new JButton("Magenta");
        dbutton = new JButton("Yellow");

        add(abutton);
        add(bbutton);
        add(cbutton);
        add(dbutton);

    /* register the specific event handler into each button */
        abutton.addMouseListener(this);
        bbutton.addMouseListener(this);
        cbutton.addMouseListener(this);
        dbutton.addMouseListener(this);
    }

/* implementation for the Mouse Event  */

    public void mouseClicked(MouseEvent evt){
        Object source = evt.getSource();
        if (source == abutton) setBackground(Color.cyan);
        else if (source == bbutton) setBackground(Color.orange);
        else if (source == cbutton) setBackground(Color.magenta);
        else if (source == dbutton) setBackground(Color.yellow);
        repaint();
    }

    public void mousePressed(MouseEvent evt){

    }

    public void mouseEntered(MouseEvent evt){

    }

    public void mouseReleased(MouseEvent evt){

    }

    public void mouseExited(MouseEvent evt){

    }
}

class ButtonFrame extends JFrame{
    public ButtonFrame(){
        setTitle("Low-level Mouse Event to Set Color");
        setSize(50, 50);
        addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){ System.exit(0);}
        });
        Container contentPane = getContentPane();
        contentPane.add(new ButtonPanel());
    }
}

public class ME_SetColor {
    public static void main(String[] args) {
        JFrame frame = new ButtonFrame();
        frame.pack();
        frame.setSize(400, 250);
        frame.setVisible(true);
    }
}

The new requirement is to exclude extends JPanel and any other extensions for class ButtonPanel . So the modified class has to be class ButtonPanel implements MouseListener{ private JButton abutton, bbutton, cbutton, dbutton;

Without JPanel, the ButtonPanel class would not be a component and therefore it can not be added to contentPane . Is there another way to make this ButtonPanel a component so it can be added to contentPane ? Or is there any other ways to implement this program?

Without JPanel, the ButtonPanel class would not be a component

You could extend JComponent. JComponent is the base class for Swing components. JPanel itself is a simple extension of JComponent (with one minor difference: a JPanel's opaque property is true by default, whereas it's false by default for JComponent).

But if your requirement is to exclude any extensions for ButtonPanel, you're right, you can't actually make it a component that can be added to a container.

However, you could include a component as a field of ButtonPanel:

class ButtonPanel implements ... {
    private JPanel panel;
    private JButton abutton, bbutton, cbutton, dbutton;

    ...

    public JPanel getPanel() { return panel; }
}

Then in ButtonFrame:

add(new ButtonPanel().getPanel());

You don't need to call getContentPane() and contentPane.add by the way, as a frame's own add methods automatically do this .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM