【问题标题】:How to define multiple JButton actions from a different class如何定义来自不同类的多个 JButton 动作
【发布时间】:2016-08-31 23:03:26
【问题描述】:

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

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));
        }
    }
}

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

【问题讨论】:

  • 请提出更具体的问题。到目前为止,您所做的只是发布一些模糊的过于宽泛的要求。你的问题越具体,答案通常就越好、越具体。请查看help centerhow to ask good questions 部分,了解有关如何改进您的问题并增加获得体面帮助的机会的更多信息。
  • 不相关的记录:以上字段都不应该是静态的,尤其是 JButtons。类名应以大写字母开头以符合 Java 命名约定,因为如果您这样做,其他人会更好地理解您的代码和您的问题。
  • 感谢您的更名,但更重要的更正是改进您的问题,使其更易于理解和回答。
  • 如需尽快获得更好的帮助,请发帖minimal reproducible exampleShort, Self Contained, Correct Example
  • 你“解决”这个问题的方式与你应该做的完全相反。正确的解决方案是不要以静态方式访问这些按钮。不过,您仍然没有澄清您的主要问题。

标签: java swing user-interface awt


【解决方案1】:

使用Action 封装功能以在程序的其他地方使用,例如按钮、菜单和工具栏。下面显示的BeaconPanel 导出了几个操作,可以在控制面板中轻松使用它们。为了限制实例的扩散,动作本身可以是类成员。作为练习,将controls 更改为JToolBar 或将相同的操作添加到菜单中。

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();
            }
        });
    }
}

【讨论】:

  • 还可以考虑导出List&lt;Action&gt;
猜你喜欢
  • 2013-03-08
  • 2021-07-02
  • 2015-05-09
  • 2014-12-21
  • 2013-02-03
  • 1970-01-01
  • 1970-01-01
  • 2020-05-25
  • 1970-01-01
相关资源
最近更新 更多