簡體   English   中英

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

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

我正在嘗試將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

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

可以在ActionController.java文件中找到JButton(在SideBar.java中定義)動作偵聽器的代碼。

好! 我正在回答我自己的問題:)但我想出了答案,也許它會幫助別人。 問題在於將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);

}

也許這將在未來給別人一個暗示。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM