简体   繁体   English

Java Swing:如何在另一个类中使用未命名的按钮?

[英]Java Swing: how do I use an unnamed button in another class?

I am currently trying to add functionality to some buttons as part of an assignment.我目前正在尝试为某些按钮添加功能作为作业的一部分。 the starter code for the assignment uses unnamed buttons and when trying to give these buttons a name results in an error.分配的起始代码使用未命名的按钮,当尝试为这些按钮命名时会导致错误。

Normally that wouldn't be too difficult to do as I understand how to use an unnamed button provided its in the same class.通常这不会太难,因为我了解如何使用在同一类中提供的未命名按钮。

However this is the tricky part - I am not allowed to use the same class to add functionality.然而,这是棘手的部分 - 我不允许使用相同的类来添加功能。 Trying to add functionality to the buttons when they are unnamed and in a different class is what I am struggling to figure out.当按钮未命名且位于不同的类中时,尝试为它们添加功能是我正在努力弄清楚的。 Its divided into two seperate classes - View for the design itself (This includes the button) and controller (This is were I want the button handling to go)它分为两个单独的类 - 设计本身的视图(包括按钮)和控制器(这是我想要的按钮处理去)

Here is the code for the view class:这是视图类的代码:

package clock;

import java.awt.*;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.util.Observer;
import java.util.Observable;

public class View implements Observer {

    ClockPanel panel;
    ActionListener listener;

    public View(Model model) {
        JFrame frame = new JFrame();
        panel = new ClockPanel(model);
        //frame.setContentPane(panel);
        frame.setTitle("Java Clock");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Start of border layout code

        Container pane = frame.getContentPane();

        JButton button = new JButton("Button 1");
        pane.add(button, BorderLayout.PAGE_START);

        panel.setPreferredSize(new Dimension(200, 200));
        pane.add(panel, BorderLayout.CENTER);

        //This is the button I want to use
        button = new JButton("Create Alarm");
        pane.add(button, BorderLayout.LINE_START);


        button = new JButton("View Alarms");
        pane.add(button, BorderLayout.PAGE_END);

        button = new JButton("5 (LINE_END)");
        pane.add(button, BorderLayout.LINE_END);

        // End of borderlayout code

        frame.pack();
        frame.setVisible(true);
    }

    public void update(Observable o, Object arg) {
        panel.repaint();
    }
}

And here is the code for the controller class:这是控制器类的代码:

package clock;

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

public class Controller {

    ActionListener listener;
    Timer timer;

    AddAlarm AddAlarm;
    Model model;
    View view;

    public Controller(Model m, View v, AddAlarm a) {
        model = m;
        view = v;
        AddAlarm = a;

        listener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                model.update();

            }
            // and this is what I want the button to do.
//        button.addActionListener(listener);    
//        private void ButtonActionPerformed(java.awt.event.ActionEvent evt) {
//        new AddAlarm().setVisible(true);
//        }
      };


        timer = new Timer(100, listener);
        timer.start();
    }
}

I'm really new to Swing myself, so if someone could show me how to accomplish this, it would be most appreciated.我自己对 Swing 真的很陌生,所以如果有人能告诉我如何做到这一点,我将不胜感激。

Pass a reference of Continer pane to your controller or a getter method in view to return the pane -> iterate trough all the component and get all the buttons-> down cast them back to Jbuttons and attach a event listener -> something like this...将 Continer 窗格的引用传递给您的控制器或 getter 方法以返回窗格 -> 遍历所有组件并获取所有按钮 -> 将它们向下投射回 Jbuttons 并附加一个事件侦听器 -> 类似的东西。 ..

//get a container reference from view
Container pane = view.getPane();

Component[] components = pane.getComponents();
ActionListener actionListener = new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        //do something with the button click;
        String txt = ((Button)e.getSource()).getText();
        if(txt.equals("bla"){ 
            //do bla
        }
    }
};

for (Component component : components) {
    if (component instanceof JButton) {
        ((JButton) component).addActionListener(actionListener);
    }
}

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

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