1

试图替换a to的JButton控制器。但由于某种原因,它似乎不听任何添加)。任何想法为什么以及如何解决?JSpinnerImageIconmouseclicks(installButtonListeners()BasicSpinnerUIMouseListener

public class SpinnerIconBtn extends JFrame {

    public SpinnerIconBtn(){
        JSpinner spinner = new JSpinner();
        spinner.setUI(new JSpinnerArrow());

        this.add(spinner);
        this.pack();
        this.setVisible(true);
    }

    class JSpinnerArrow extends BasicSpinnerUI {

        @Override
        protected Component createNextButton() {
            Component c = createArrowButton(SwingConstants.NORTH);
            c.setName("Spinner.nextButton");
            installNextButtonListeners(c);
            return c;
        }

        @Override
        protected Component createPreviousButton() {
            Component c = createArrowButton(SwingConstants.SOUTH);
            c.setName("Spinner.previousButton");
            installPreviousButtonListeners(c);
            return c;
        }

        private Component createArrowButton(int direction) {
            String path = "/Users/tst.png";
            JLabel icon = new JLabel(new ImageIcon(path));
            return icon;
        }
    }

    public static void main(String[] args) {
        new SpinnerIconBtn();
    }
}
4

3 回答 3

2
  • 不要改变图标,使用paintIcon()

  • 如果 ArrowsButtons 被移动的 Icons 太大,则更改 LayoutManager

.

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

class Testing {
    //int counter = 0;//proper setting

    int counter = 7195;//testing hours 'tick over' correctly
    JSpinner spinner = new JSpinner();
    JTextField editor = ((JSpinner.DefaultEditor) spinner.getEditor()).getTextField();
    java.text.DecimalFormat df = new java.text.DecimalFormat("00");

    public void buildGUI() {
        spinner.setUI(new EndlessHoursUI());
        JFrame f = new JFrame();
        f.getContentPane().add(spinner);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }

    class EndlessHoursUI extends javax.swing.plaf.basic.BasicSpinnerUI {

        public EndlessHoursUI() {
            setTime();
        }

        @Override
        protected void installNextButtonListeners(Component c) {
        }// do nothing

        @Override
        protected void installPreviousButtonListeners(Component c) {
        }// do nothing

        @Override
        protected Component createNextButton() {
            JButton btnNext = (JButton) super.createNextButton();
            btnNext.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent ae) {
                    changeSpinner(1);
                }
            });
            return btnNext;
        }

        @Override
        protected Component createPreviousButton() {
            JButton btnPrevious = (JButton) super.createPreviousButton();
            btnPrevious.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent ae) {
                    changeSpinner(-1);
                }
            });
            return btnPrevious;
        }
    }

    public void changeSpinner(int amount) {
        if (counter > 0 || amount > 0) {
            counter += amount;
            setTime();
        }
    }

    public void setTime() {
        int hours = counter / 3600;
        int mins = (counter / 60) % 60;
        int secs = counter % 60;
        String time = df.format(hours) + ":" + df.format(mins) + ":" + df.format(secs);
        editor.setText(time);
    }

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

            @Override
            public void run() {
                new Testing().buildGUI();
            }
        });
    }
}
于 2013-05-20T07:04:03.197 回答
1

没有设法用答案解决它。通过删除所有没有箭头的按钮来调整按钮,然后我将箭头一起移动:

private Component createArrowButton(int direction) {

        JButton b = new BasicArrowButton(direction){

            private static final long serialVersionUID = 1L;

            @Override
            public void paintTriangle(Graphics g, int x, int y, int size,
                    int direction, boolean isEnabled) {


                if(direction == NORTH){
                    y +=3;
                }
                else if(direction == SOUTH){
                    y -=3;
                }

                Color shadow = UIManager.getColor("controlShadow");                 
                Color darkShadow = UIManager.getColor("controlDkShadow");
                Color highlight = UIManager.getColor("controlLtHighlight");

                Color oldColor = g.getColor();
                int mid, i, j;

                j = 0;
                size = Math.max(size, 2);
                mid = (size / 2) - 1;

                g.translate(x, y);
                if (isEnabled)
                    g.setColor(darkShadow);
                else
                    g.setColor(shadow);

                switch (direction) {
                case NORTH:

                    for (i = 0; i < size; i++) {
                        g.drawLine(mid - i, i, mid + i, i);
                    }
                    if (!isEnabled) {
                        g.setColor(highlight);
                        g.drawLine(mid - i + 2, i, mid + i, i);
                    }
                    break;
                case SOUTH:
                    if (!isEnabled) {
                        g.translate(1, 1);
                        g.setColor(highlight);
                        for (i = size - 1; i >= 0; i--) {
                            g.drawLine(mid - i, j, mid + i, j);
                            j++;
                        }
                        g.translate(-1, -1);
                        g.setColor(shadow);
                    }

                    j = 0;
                    for (i = size - 1; i >= 0; i--) {
                        g.drawLine(mid - i, j, mid + i, j);
                        j++;
                    }
                    break;
                case WEST:
                    for (i = 0; i < size; i++) {
                        g.drawLine(i, mid - i, i, mid + i);
                    }
                    if (!isEnabled) {
                        g.setColor(highlight);
                        g.drawLine(i, mid - i + 2, i, mid + i);
                    }
                    break;
                case EAST:
                    if (!isEnabled) {
                        g.translate(1, 1);
                        g.setColor(highlight);
                        for (i = size - 1; i >= 0; i--) {
                            g.drawLine(j, mid - i, j, mid + i);
                            j++;
                        }
                        g.translate(-1, -1);
                        g.setColor(shadow);
                    }

                    j = 0;
                    for (i = size - 1; i >= 0; i--) {
                        g.drawLine(j, mid - i, j, mid + i);
                        j++;
                    }
                    break;
                }
                g.translate(-x, -y);
                g.setColor(oldColor);
            }
        };
        b.setBorder(BorderFactory.createEmptyBorder());
        //removes content area
        b.setContentAreaFilled(false);
        b.setInheritsPopupMenu(true);
        b.setOpaque(false);
        b.setBackground(color);

        return b;
    }
于 2013-05-20T08:45:38.400 回答
0

installNextButtonListeners()installPreviousButtonListeners()调用以下方法:

private void installButtonListeners(Component c,
                                    ArrowButtonHandler handler) {
    if (c instanceof JButton) {
        ((JButton)c).addActionListener(handler);
    }
    c.addMouseListener(handler);
}

instanceof检查的原因ArrowButtonHandler不会附加ActionListener到您的JLabel,它处理旋转。

您可以使用的子类JButton代替JLabel.

于 2013-05-20T07:01:01.730 回答