简体   繁体   中英

Adding JButton ActionListener (in one *.java file) to a Button in a different *.java file doesn't work

I'm trying to add ActionListener to a JButton, which is defined in another *.java file, but it doesn't work. If I call this JButton from the Main public class it works just fine, what am I missing?

I am building a simple Paint Application using Java Swing. I have divided the code in several *.java files to make it more readable. JButtons are defined in a SideBar.java file and I wanted to add ActionController.java file that would call up all of the actionListeners for JButtons. BUT when I add the code for .addActionListener() (in the ActionController.java file) nothing happens when I press the button. But, when I add the same code in the Main.java file the pressed button works just fine. Can somebody tell me what am I missing?

I have also another question regarding readability of the code. I am new to Java, so my question is whether the logics for dividing the code in so many classes is good? I created a Main class that would define the frame of the application, SideBar.java that would contain all the layout of the sidebar, TopMenu.java, which would contain Menu for the application, DrawingArea.java which would be the graphics for the blank paper of the application, Draw.java which would contain all of the functions for the drawing (resizing pencil, choosing a colour), and AcionController.java which would assign all of the functions (defined in Draw.java file) to Buttons, sliders etc. Is it a good way of creating an app or would you suggest dividing it in other way?

Below you can find the code for my so far written app:

Main.java

package sample;

import sample.applicationLayout.ActionController;
import sample.applicationLayout.DrawingArea;
import sample.applicationLayout.TopMenu;
import sample.applicationLayout.SideBar;
import javax.swing.*;
import java.awt.*;

public class Main {

    Main() {

        //creating Frame for the application
        JFrame frame = new JFrame("Paint Application");

        //creating menu
        TopMenu menu = new TopMenu();
        frame.setJMenuBar(menu);
        //END OF MENU


        SideBar sideBar = new SideBar();
        DrawingArea drawingArea = new DrawingArea();

        ActionController actionController = new ActionController();
        actionController.clickOnButtons();

        frame.add(sideBar, BorderLayout.WEST);
        frame.add(drawingArea, BorderLayout.CENTER);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(1200, 800);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

    }//end of Main()

    public static void main(String[] args) {
        new Main();
    }//end of public static void main(String[] args)

}//end of Main class

ActionController.java

package sample.applicationLayout;

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

public class ActionController {

        SideBar sideBar = new SideBar();
        ListenForButton listenForButton = new ListenForButton();

        public ActionController() {
        }

        public void clickOnButtons() {
                sideBar.getButton_pencil().addActionListener(listenForButton);

        }

        //listener for the buttons
        public class ListenForButton implements ActionListener {
                public void actionPerformed(ActionEvent e) {

                        System.out.println("button 1");

                }//end of public void actionPerformed
        }//end of public class ListenForButton

}//end of ActionController class

Draw.java

package sample;

public class Draw {
}

TopMenu.java

package sample.applicationLayout;

import javax.swing.*;

public class TopMenu extends JMenuBar {

    public TopMenu() {

        JMenu fileMenu = new JMenu("File");
        JMenu infoMenu = new JMenu("Info");

        JMenuItem newMenuItem = new JMenuItem("New");
        JMenuItem openMenuItem = new JMenuItem("Open");
        JMenuItem saveMenuItem = new JMenuItem("Save");
        JMenuItem clearMenuItem = new JMenuItem("Clear");
        JMenuItem exitMenuItem = new JMenuItem("Exit");
        JMenuItem aboutmeMenuItem = new JMenuItem("About");

        this.add(fileMenu);
        this.add(infoMenu);

        fileMenu.add(newMenuItem);
        fileMenu.add(openMenuItem);
        fileMenu.add(saveMenuItem);
        fileMenu.add(clearMenuItem);
        fileMenu.add(exitMenuItem);
        infoMenu.add(aboutmeMenuItem);

    }//end of TopMenu()

}//end of TopMenu extends JMenuBar

DrawingArea.java

package sample.applicationLayout;

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

public class DrawingArea extends JPanel {

    public DrawingArea() {

        this.setPreferredSize(new Dimension(1100, 800));

        this.setBackground(Color.WHITE);
    }//end of DrawingArea()

}//end of DrawingArea extends JPanel

SideBar.java

package sample.applicationLayout;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;

public class SideBar extends JPanel {


    private JButton button_pencil, button_brush, button_line, button_oval, button_rectangle,
            button_filled_oval, button_filled_rectangle, button_text, button_eraser, button_bucket;



    //===========================================

    public JButton getButton_pencil() {
        return button_pencil;
    }




    //===========================================


    public SideBar() {



        this.setPreferredSize(new Dimension(120, 800));
        BoxLayout boxLayout = new BoxLayout(this, BoxLayout.Y_AXIS);

        //fist JPanel with buttons
        JPanel panelA = new JPanel();
 //       panelA.setBackground(Color.WHITE);
        panelA.setPreferredSize(new Dimension(120, 250));
        Border panelAborder = BorderFactory.createTitledBorder("Paint:");
        panelA.setBorder(panelAborder);

        panelA.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();

        //icons for buttons
        ImageIcon icon_pencil = new ImageIcon("graphics/icon_pencil.png");
        ImageIcon icon_brush = new ImageIcon("graphics/icon_brush.png");
        ImageIcon icon_line = new ImageIcon("graphics/icon_line.png");
        ImageIcon icon_oval = new ImageIcon("graphics/icon_oval.png");
        ImageIcon icon_rectangle = new ImageIcon("graphics/icon_rectangle.png");
        ImageIcon icon_filled_oval = new ImageIcon("graphics/icon_filled_oval.png");
        ImageIcon icon_filled_rectangle = new ImageIcon("graphics/icon_filled_rectangle.png");
        ImageIcon icon_text = new ImageIcon("graphics/icon_text.png");
        ImageIcon icon_eraser = new ImageIcon("graphics/icon_eraser.png");
        ImageIcon icon_bucket = new ImageIcon("graphics/icon_bucket.png");


        //creating JButtons for basic paint drawing functions
        button_pencil = new JButton();
        button_brush = new JButton();
        button_line = new JButton();
        button_oval = new JButton();
        button_rectangle = new JButton();
        button_filled_oval = new JButton();
        button_filled_rectangle = new JButton();
        button_text = new JButton();
        button_eraser = new JButton();
        button_bucket = new JButton();

        //set size of the buttons
        button_pencil.setPreferredSize(new Dimension(35,35));
        button_brush.setPreferredSize(new Dimension(35,35));
        button_line.setPreferredSize(new Dimension(35,35));
        button_oval.setPreferredSize(new Dimension(35,35));
        button_rectangle.setPreferredSize(new Dimension(35,35));
        button_filled_oval.setPreferredSize(new Dimension(35,35));
        button_filled_rectangle.setPreferredSize(new Dimension(35,35));
        button_text.setPreferredSize(new Dimension(35,35));
        button_eraser.setPreferredSize(new Dimension(35,35));
        button_bucket.setPreferredSize(new Dimension(35,35));

        //color picker should be in an individual JPane

        //setting icons to buttons
        button_pencil.setIcon(icon_pencil);
        button_brush.setIcon(icon_brush);
        button_line.setIcon(icon_line);
        button_oval.setIcon(icon_oval);
        button_rectangle.setIcon(icon_rectangle);
        button_filled_oval.setIcon(icon_filled_oval);
        button_filled_rectangle.setIcon(icon_filled_rectangle);
        button_text.setIcon(icon_text);
        button_eraser.setIcon(icon_eraser);
        button_bucket.setIcon(icon_bucket);


        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        panelA.add(button_pencil, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        panelA.add(button_brush, gbc);

        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelA.add(button_line, gbc);

        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelA.add(button_oval, gbc);

        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        panelA.add(button_rectangle, gbc);

        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        panelA.add(button_filled_oval, gbc);

        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        panelA.add(button_filled_rectangle, gbc);


        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        panelA.add(button_text, gbc);

        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.insets = new Insets(5,5,5,5);
        panelA.add(button_eraser, gbc);

        gbc.gridx = 1;
        gbc.gridy = 4;
        gbc.insets = new Insets(5,5,5,5);
        panelA.add(button_bucket, gbc);




        //second JPanel with sliders
        JPanel panelB = new JPanel();
 //     panelB.setBackground(Color.red);
        panelB.setPreferredSize(new Dimension(120, 200));
        Border panelBborder = BorderFactory.createTitledBorder("Paint:");
        panelB.setBorder(panelBborder);

        panelB.add(new JButton("button1"));
        panelB.add(new JButton("button2"));


        //third JPanel with color picker
        JPanel panelC = new JPanel();
//      panelC.setBackground(Color.blue);
        panelC.setPreferredSize(new Dimension(120, 200));
        Border panelCborder = BorderFactory.createTitledBorder("Paint:");
        panelC.setBorder(panelCborder);

        panelC.add(new JButton("button1"));
        panelC.add(new JButton("button2"));


        //adding JPanels to main JPanel
        this.add(panelA, BorderLayout.NORTH);
        this.add(panelB, BorderLayout.CENTER);
        this.add(panelC, BorderLayout.SOUTH);

    }//end of public SideBar()


}//end of public class SideBar extends JPanel

The code for JButton (which is defined in SideBar.java) action listener can be found in ActionController.java file.

Ok! I'm answering my own question again :) But I figured out the answer, maybe it will help someone else. The problem was in initialising the SideBar class twice, once in the Main.java file and secondly in the ActionController.java file. I changed the following code:

In Main.java file:

actionController.clickOnButtons();

changed to:

actionController.clickOnButtons(sideBar);

And in ActionController.java file:

SideBar sideBar = new SideBar();
ListenForButton listenForButton = new ListenForButton();

public ActionController() {
}

public void clickOnButtons() {
    sideBar.getButton_pencil().addActionListener(listenForButton);

}

changed to:

ListenForButton listenForButton = new ListenForButton();

public ActionController() {
}

public void clickOnButtons(SideBar sideBar) {
    sideBar.getButton_pencil().addActionListener(listenForButton);

}

Maybe this will give a hint to someone else in the future.

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