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

问题描述 投票:0回答:1

我正在尝试将ActionListener添加到JButton,它在另一个* .java文件中定义,但它不起作用。如果我从Main公共类中调用这个JButton它可以正常工作,我错过了什么?

我正在使用Java Swing构建一个简单的Paint应用程序。我已将代码分成几个* .java文件,以使其更具可读性。 JButton在SideBar.java文件中定义,我想添加ActionController.java文件,该文件将调用JButtons的所有actionListener。但是,当我添加.addActionListener()的代码(在ActionController.java文件中)时,按下按钮时没有任何反应。但是,当我在Main.java文件中添加相同的代码时,按下的按钮工作正常。有人能告诉我我错过了什么吗?

我还有另一个关于代码可读性的问题。我是Java的新手,所以我的问题是在这么多类中划分代码的逻辑是否良好?我创建了一个Main类,它将定义应用程序的框架,SideBar.java将包含侧边栏的所有布局,TopMenu.java,它将包含应用程序的Menu,DrawingArea.java,它将是空白的图形应用程序的论文,Draw.java,它将包含绘图的所有函数(调整大小铅笔,选择颜色),以及AcionController.java,它将所有函数(在Draw.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

action controller.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 {
}

top menu.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

drawing area.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

可以在ActionController.java文件中找到JButton(在SideBar.java中定义)动作侦听器的代码。

java class button actionlistener
1个回答
0
投票

好!我正在回答我自己的问题:)但我想出了答案,也许它会帮助别人。问题在于将SideBar类初始化两次,一次在Main.java文件中,另一次在ActionController.java文件中。我更改了以下代码:

在Main.java文件中:

actionController.clickOnButtons();

变成:

actionController.clickOnButtons(sideBar);

并在ActionController.java文件中:

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

public ActionController() {
}

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

}

变成:

ListenForButton listenForButton = new ListenForButton();

public ActionController() {
}

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

}

也许这将在未来给别人一个暗示。

© www.soinside.com 2019 - 2024. All rights reserved.