簡體   English   中英

Java swing ActionEvent用於按鈕單擊

[英]Java swing ActionEvent for button click

@HoverCraftFullOfEels幫助創建了這段代碼, 添加一個選擇所有省略號並將其顏色INACTIVE_COLOR 更改 為ACTIVE_COLOR 的按鈕的最佳方法是什么

我的方法是在按鈕面板上添加另一個按鈕,並定義將要執行的操作。
但是我試圖從DrawPanel2類添加 無法訪問的功能 有什么可能的最佳方法呢?

與往常一樣,非常感謝任何其他建議的改進,謝謝。

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.util.*;
import java.util.List;

@SuppressWarnings("serial")
public class DrawPanelMain extends JPanel {

    private static final int PREF_W = 700;
    private static final int PREF_H = PREF_W;
    private List<Point> POINT_LIST = Arrays.asList(new Point[] {
            new Point(40, 40),
            new Point(40, 100),
            new Point(40, 160),
            new Point(40, 220),
            new Point(40, 280),
            new Point(40, 340),
            new Point(40, 400),
            new Point(40, 460),
            new Point(40, 520),
            new Point(40, 580),
            new Point(100, 100),
            new Point(100, 160),
            new Point(100, 220),
            new Point(100, 280),
            new Point(100, 340),
            new Point(100, 400),
            new Point(100, 460),
            new Point(100, 520),
            new Point(100, 580),
            new Point(160, 160),
            new Point(160, 220),
            new Point(160, 280),
            new Point(160, 340),
            new Point(160, 400),
            new Point(160, 460),
            new Point(160, 520),
            new Point(160, 580),
            new Point(220, 220),
            new Point(220, 280),
            new Point(220, 340),
            new Point(220, 400),
            new Point(220, 460),
            new Point(220, 520),
            new Point(220, 580),
            new Point(280, 280),
            new Point(280, 340),
            new Point(280, 400),
            new Point(280, 460),
            new Point(280, 520),
            new Point(280, 580),
            new Point(340, 340),
            new Point(340, 400),
            new Point(340, 460),
            new Point(340, 520),
            new Point(340, 580),
            new Point(400, 400),
            new Point(400, 460),
            new Point(400, 520),
            new Point(400, 580),
            new Point(460, 460),
            new Point(460, 520),
            new Point(460, 580),
            new Point(520, 520),
            new Point(520, 580),
            new Point(580, 580)
    });
    private JTabbedPane tabbedPane = new JTabbedPane();
    private int tabIndex = 0;

    public DrawPanelMain() {
        JPanel btnPanel = new JPanel();
        btnPanel.add(new JButton(new AddSwitchAction("Add Switch Panel")));
        btnPanel.add(new JButton(new PushConfigAction("Push Config")));
        btnPanel.add(new JButton(new ActivateAllAction("Activate All")));  //Button I want to work with

        setLayout(new BorderLayout());
        add(tabbedPane, BorderLayout.CENTER);
        add(btnPanel, BorderLayout.PAGE_END);
    }

    @Override
    public Dimension getPreferredSize() {
        if (isPreferredSizeSet()) {
            return super.getPreferredSize();
        }
        return new Dimension(PREF_W, PREF_H);
    }

    private class AddSwitchAction extends AbstractAction {
            public AddSwitchAction(String name) {
                super(name);
                int mnemonic = (int) name.charAt(0);
                putValue(MNEMONIC_KEY, mnemonic);
            }

        @Override
        public void actionPerformed(ActionEvent e) {
            tabIndex++;
            String title = "Switch " + tabIndex;
            DrawPanel2 tabComponent = new DrawPanel2(POINT_LIST);
            tabbedPane.add(title, tabComponent);
        }
    }

    private class PushConfigAction extends AbstractAction {
        public PushConfigAction(String name) {
            super(name);
            int mnemonic = (int) name.charAt(0);
            putValue(MNEMONIC_KEY, mnemonic);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            /*Add code sending the configuration to the switch panel*/
            JOptionPane.showMessageDialog(DrawPanelMain.this, "Configuration Pushed to Panel");
        }
    }

    //Here is the code I want to work with for making the button activate all circles
    private class ActivateAllAction extends AbstractAction {
        public ActivateAllAction(String name) {
            super(name);
            int mnemonic = (int) name.charAt(1);
            putValue(MNEMONIC_KEY, mnemonic);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            //Add code for performing action here
        }
    }

    private static void createAndShowGui() {
        DrawPanelMain mainPanel = new DrawPanelMain();
        final double version = 0.1;
        JFrame frame = new JFrame("RF Connection Panel " + version);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }

}

@SuppressWarnings("serial")
class DrawPanel2 extends JPanel {
    private static final int OVAL_WIDTH = 40;
    private static final Color INACTIVE_COLOR = Color.RED;
    private static final Color ACTIVE_COLOR = Color.green;
    private List<Point> points;
    private List<Ellipse2D> ellipses = new ArrayList<>();
    private Map<Ellipse2D, Color> ellipseColorMap = new HashMap<>();

    public DrawPanel2(List<Point> points) {
        this.points = points;
        for (Point p : points) {
            int x = p.x - OVAL_WIDTH / 2;
            int y = p.y - OVAL_WIDTH / 2;
            int w = OVAL_WIDTH;
            int h = OVAL_WIDTH;
            Ellipse2D ellipse = new Ellipse2D.Double(x, y, w, h);
            ellipses.add(ellipse);
            ellipseColorMap.put(ellipse, INACTIVE_COLOR);
        }

        MyMouseAdapter mListener = new MyMouseAdapter();
        addMouseListener(mListener);
        addMouseMotionListener(mListener);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        for (Ellipse2D ellipse : ellipses) {
            g2.setColor(ellipseColorMap.get(ellipse));
            g2.fill(ellipse);
        }
    }

    private class MyMouseAdapter extends MouseAdapter {
        @Override
        public void mousePressed(MouseEvent e) {
            for (Ellipse2D ellipse : ellipses) {
                if (ellipse.contains(e.getPoint())) {
                    Color c = ellipseColorMap.get(ellipse);
                    c =  (c == INACTIVE_COLOR) ? ACTIVE_COLOR : INACTIVE_COLOR;
                    ellipseColorMap.put(ellipse, c);
                }
            }
            repaint();
        }
    }
}

因為您可以擁有多個DrawPanel2實例,並且每個實例都包含在其自己的“選項卡”中,所以您需要找到所選選項卡並從中提取DrawPanel2實例,例如

private class ActivateAllAction extends AbstractAction {

    public ActivateAllAction(String name) {
        super(name);
        int mnemonic = (int) name.charAt(1);
        putValue(MNEMONIC_KEY, mnemonic);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //Add code for performing action here
        Component comp = tabbedPane.getSelectedComponent();
        if (comp instanceof DrawPanel2) {
            DrawPanel2 drawPanel = (DrawPanel2) comp;
        }
    }
}

在轉換之前,首先檢查選定的選項卡組件是否是DrawPanel2的實例。

接下來,你需要一些方法來改變省略號的狀態......

為此,我在DrawPanel2類中添加了一個activateAll方法,你可以添加一個允許傳遞一個設置狀態的boolean

public void activateAll() {
    for (Ellipse2D ellipse : ellipses) {
        ellipseColorMap.put(ellipse, ACTIVE_COLOR);
    }
    repaint();
}

然后從Action ,我簡單地稱它為

//...
DrawPanel2 drawPanel = (DrawPanel2) comp;
drawPanel.activateAll();

暫無
暫無

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

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