简体   繁体   English

将JButton ActionListener(在一个* .java文件中)添加到另一个* .java文件中的Button不起作用

[英]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. 我正在尝试将ActionListener添加到JButton,它在另一个* .java文件中定义,但它不起作用。 If I call this JButton from the Main public class it works just fine, what am I missing? 如果我从Main公共类中调用这个JButton它可以正常工作,我错过了什么?

I am building a simple Paint Application using Java Swing. 我正在使用Java Swing构建一个简单的Paint应用程序。 I have divided the code in several *.java files to make it more readable. 我已将代码分成几个* .java文件,以使其更具可读性。 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. JButton在SideBar.java文件中定义,我想添加ActionController.java文件,该文件将调用JButtons的所有actionListener。 BUT when I add the code for .addActionListener() (in the ActionController.java file) nothing happens when I press the button. 但是,当我添加.addActionListener()的代码(在ActionController.java文件中)时,按下按钮时没有任何反应。 But, when I add the same code in the Main.java file the pressed button works just fine. 但是,当我在Main.java文件中添加相同的代码时,按下的按钮工作正常。 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? 我是Java的新手,所以我的问题是在这么多类中划分代码的逻辑是否良好? 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? 我创建了一个Main类,它将定义应用程序的框架,SideBar.java将包含侧边栏的所有布局,TopMenu.java,它将包含应用程序的Menu,DrawingArea.java,它将是空白的图形应用程序的论文,Draw.java,它将包含绘图的所有函数(调整大小铅笔,选择颜色),以及AcionController.java,它将所有函数(在Draw.java文件中定义)分配给按钮,滑块这是创建应用程序的好方法还是建议以其他方式划分?

Below you can find the code for my so far written app: 下面你可以找到我迄今为止编写的应用程序的代码:

Main.java 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 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 Draw.java

package sample;

public class Draw {
}

TopMenu.java 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 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 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. 可以在ActionController.java文件中找到JButton(在SideBar.java中定义)动作侦听器的代码。

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. 问题在于将SideBar类初始化两次,一次在Main.java文件中,另一次在ActionController.java文件中。 I changed the following code: 我更改了以下代码:

In Main.java file: 在Main.java文件中:

actionController.clickOnButtons();

changed to: 变成:

actionController.clickOnButtons(sideBar);

And in ActionController.java file: 并在ActionController.java文件中:

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. 也许这将在未来给别人一个暗示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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