如何从不同的类定义多个 JButton 操作 [英] How to define multiple JButton actions from a different class

查看:20
本文介绍了如何从不同的类定义多个 JButton 操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序,我需要根据单击的按钮为单独的类执行不同的操作.

公共类NewJFrame{公共静态 JButton b1;公共静态 JButton b2;公共静态 JButton b3;}公共类插槽{整数值;JButton 按钮;插槽(整数值,JButton 按钮){this.value=值;this.button=button;}}公开课游戏{插槽[] 插槽=新插槽[3];游戏(){插槽[0]=新插槽(1,NewJFrame.b1);插槽[1]=新插槽(2,NewJFrame.b2);插槽[2]=新插槽(3,NewJFrame.b3);}public void actionPerformed(ActionEvent e) {for(int i=0;i<3;i++){if(e.getSource()==slots[i].button)slot[i].button.setText(String.valueOf(value));}}}

类似的东西.请注意,我在 GUI 设计方面完全是新手.

解决方案

使用

import java.awt.BorderLayout;导入 java.awt.Color;导入 java.awt.Dimension;导入 java.awt.EventQueue;导入 java.awt.Graphics;导入 java.awt.Graphics2D;导入 java.awt.RenderingHints;导入 java.awt.event.ActionEvent;导入 java.awt.event.ActionListener;导入 java.awt.geom.Ellipse2D;导入 javax.swing.AbstractAction;导入 javax.swing.Action;导入 javax.swing.Timer;导入 javax.swing.JButton;导入 javax.swing.JFrame;导入 javax.swing.JPanel;/** @see http://stackoverflow.com/a/37063037/230513 */公共类信标{私有静态类 BeaconPanel 扩展 JPanel {私有静态最终 int N = 16;私人最终 Ellipse2D.Double ball = new Ellipse2D.Double();私人最终计时器;私人最终颜色;私人最终颜色关闭;private final AbstractAction flashAction = new AbstractAction("Flash") {@覆盖public void actionPerformed(ActionEvent e) {timer.restart();}};private final AbstractAction onAction = new AbstractAction("On") {@覆盖public void actionPerformed(ActionEvent e) {停止(开);}};private final AbstractAction offAction = new AbstractAction("Off") {@覆盖public void actionPerformed(ActionEvent e) {停下);}};私有颜色 currentColor;公共信标面板(颜色打开,颜色关闭){this.on = on;this.off = 关;this.currentColor = on;计时器 = 新计时器(500,新的 ActionListener(){@覆盖public void actionPerformed(ActionEvent e) {更改颜色();}});}公共无效开始(){定时器开始();}@覆盖公共无效paintComponent(图形g){super.paintComponent(g);Graphics2D g2 = (Graphics2D) g;g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);int x = getX() + N;int y = getY() + N;int w = getWidth() - 2 * N;int h = getHeight() - 2 * N;ball.setFrame(x, y, w, h);g2.setColor(currentColor);g2.fill(球);g2.setColor(Color.black);g2.draw(球);}私有无效 changeColors() {currentColor = currentColor == on ?关:开;重绘();}私人无效停止(颜色颜色){定时器停止();当前颜色 = 颜色;重绘();}公共动作 getFlashAction() {返回 flashAction;}公共动作 getOnAction() {返回操作;}公共动作 getOffAction() {return offAction;}@覆盖公共维度 getPreferredSize() {返回新维度(N * N,N * N);}}公共静态无效显示(){JFrame f = new JFrame("Beacon");f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);final BeaconPanel beaconPanel = new BeaconPanel(Color.orange, Color.orange.darker());f.添加(信标面板);JPanel 控件 = new JPanel();control.add(new JButton(beaconPanel.getFlashAction()));control.add(new JButton(beaconPanel.getOnAction()));control.add(new JButton(beaconPanel.getOffAction()));f.add(controls, BorderLayout.SOUTH);f.pack();f.setLocationRelativeTo(null);f.setVisible(true);信标面板.start();}公共静态无效主(字符串 [] args){EventQueue.invokeLater(new Runnable() {@覆盖公共无效运行(){信标显示();}});}}

I am writing a program where I need to do different actions for a separate class depending on which button is clicked.

public class NewJFrame{
    public static JButton b1;
    public static JButton b2;
    public static JButton b3;
}

public class Slot{

    int value;
    JButton button;

    Slot(int value, JButton button)
    {
        this.value=value;
        this.button=button;
    }
}

public class Game{
    Slot[] slots=new Slot[3];
    Game(){
        slots[0]=new Slot(1,NewJFrame.b1);
        slots[1]=new Slot(2,NewJFrame.b2);
        slots[2]=new Slot(3,NewJFrame.b3);
    }
    public void actionPerformed(ActionEvent e) {
        for(int i=0;i<3;i++){
            if(e.getSource()==slots[i].button)
                slots[i].button.setText(String.valueOf(value));
        }
    }
}

Something like this. Note that, I'm completely novice at GUI designing.

解决方案

Use Action to encapsulate functionality for use elsewhere in your program, e.g. buttons, menus and toolbars. The BeaconPanel shown below exports several actions that make it easy to use them in a control panel. To limit the proliferation of instances, the actions themselves can be class members. As an exercise, change controls to a JToolBar or add the same actions to a menu.

JPanel controls = new JPanel();
controls.add(new JButton(beaconPanel.getFlashAction()));
controls.add(new JButton(beaconPanel.getOnAction()));
controls.add(new JButton(beaconPanel.getOffAction()));

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Timer;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

/** @see http://stackoverflow.com/a/37063037/230513 */
public class Beacon {

    private static class BeaconPanel extends JPanel {

        private static final int N = 16;
        private final Ellipse2D.Double ball = new Ellipse2D.Double();
        private final Timer timer;
        private final Color on;
        private final Color off;
        private final AbstractAction flashAction = new AbstractAction("Flash") {
            @Override
            public void actionPerformed(ActionEvent e) {
                timer.restart();
            }
        };
        private final AbstractAction onAction = new AbstractAction("On") {
            @Override
            public void actionPerformed(ActionEvent e) {
                stop(on);
            }
        };
        private final AbstractAction offAction = new AbstractAction("Off") {
            @Override
            public void actionPerformed(ActionEvent e) {
                stop(off);
            }
        };
        private Color currentColor;

        public BeaconPanel(Color on, Color off) {
            this.on = on;
            this.off = off;
            this.currentColor = on;
            timer = new Timer(500, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    changeColors();
                }
            });
        }

        public void start() {
            timer.start();
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
            int x = getX() + N;
            int y = getY() + N;
            int w = getWidth() - 2 * N;
            int h = getHeight() - 2 * N;
            ball.setFrame(x, y, w, h);
            g2.setColor(currentColor);
            g2.fill(ball);
            g2.setColor(Color.black);
            g2.draw(ball);
        }

        private void changeColors() {
            currentColor = currentColor == on ? off : on;
            repaint();
        }

        private void stop(Color color) {
            timer.stop();
            currentColor = color;
            repaint();
        }

        public Action getFlashAction() {
            return flashAction;
        }

        public Action getOnAction() {
            return onAction;
        }

        public Action getOffAction() {
            return offAction;
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(N * N, N * N);
        }
    }

    public static void display() {
        JFrame f = new JFrame("Beacon");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final BeaconPanel beaconPanel = new BeaconPanel(Color.orange, Color.orange.darker());
        f.add(beaconPanel);
        JPanel controls = new JPanel();
        controls.add(new JButton(beaconPanel.getFlashAction()));
        controls.add(new JButton(beaconPanel.getOnAction()));
        controls.add(new JButton(beaconPanel.getOffAction()));
        f.add(controls, BorderLayout.SOUTH);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
        beaconPanel.start();
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                Beacon.display();
            }
        });
    }
}

这篇关于如何从不同的类定义多个 JButton 操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆