BasicScrollBarUI?如何使用

时间:2017-06-08 09:37:38

标签: java swing user-interface

我正在为Minecraft编写代码编辑器,我对Java中的常规UI设计不太满意。我设法更改了按钮,但它对滚动条不起作用。你能帮我吗?

这是我的ButtonUI课程。

public class UIButton extends BasicButtonUI implements MouseListener {

    private Color hoverColor;
    private Color normalColor;
    private Color pressedColor;

    public UIButton(ColorSheme cs) {
        super();
        if (cs == ColorSheme.WHITE_BLUE_GRAY) {
            hoverColor = new Color(212, 211, 255);
            normalColor = new Color(184, 183, 206);
            pressedColor = new Color(197, 196, 228);
        }else if (cs == ColorSheme.GREEN_GRAY) {
            hoverColor = new Color(22, 179, 103);
            normalColor = new Color(9, 126, 101);
            pressedColor = new Color(17, 158, 102);
        }else if (cs == ColorSheme.RED_GRAY) {
            hoverColor = new Color(204, 86, 88);
            normalColor = new Color(171, 79, 91);
            pressedColor = new Color(187, 82, 90);
        }else if (cs == ColorSheme.BLUE_GRAY) {
            hoverColor = new Color(126, 176, 191);
            normalColor = new Color(104, 134, 179);
            pressedColor = new Color(111, 148, 183);
        }
    }

    @Override
    public void installUI(JComponent c) {
        super.installUI(c);
        c.addMouseListener(this);
        c.setBorder(null);
        c.setFont(Main.getDefaultFont());
    }

    @Override
    public void uninstallUI(JComponent c) {
        super.uninstallUI(c);
        c.removeMouseListener(this);
    }

    @Override
    protected void installDefaults(AbstractButton b) {
        super.installDefaults(b);
        changeButtonColor((JButton) b, normalColor);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        changeButtonColor((JButton) e.getComponent(), hoverColor);
    }

    @Override
    public void mousePressed(MouseEvent e) {
        changeButtonColor((JButton) e.getComponent(), pressedColor);
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        changeButtonColor((JButton) e.getComponent(), normalColor);
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        changeButtonColor((JButton) e.getComponent(), hoverColor);
    }

    @Override
    public void mouseExited(MouseEvent e) {
        changeButtonColor((JButton) e.getComponent(), normalColor);
    }

    private void changeButtonColor(JButton b, Color c) {
        b.setBackground(c);
    }
}

这是我的UIScrollBarUI课程。

public class UIScrollBar extends BasicScrollBarUI implements MouseListener {

    private Color hoverColor;
    private Color normalColor;
    private Color pressedColor;

    public UIScrollBar(ColorSheme cs) {
        super();
        if (cs == ColorSheme.WHITE_BLUE_GRAY) {
            hoverColor = new Color(212, 211, 255);
            normalColor = new Color(184, 183, 206);
            pressedColor = new Color(197, 196, 228);
        }else if (cs == ColorSheme.GREEN_GRAY) {
            hoverColor = new Color(22, 179, 103);
            normalColor = new Color(9, 126, 101);
            pressedColor = new Color(17, 158, 102);
        }else if (cs == ColorSheme.RED_GRAY) {
            hoverColor = new Color(204, 86, 88);
            normalColor = new Color(171, 79, 91);
            pressedColor = new Color(187, 82, 90);
        }else if (cs == ColorSheme.BLUE_GRAY) {
            hoverColor = new Color(126, 176, 191);
            normalColor = new Color(104, 134, 179);
            pressedColor = new Color(111, 148, 183);
        }
    }

    @Override
    public void installUI(JComponent c) {
        super.installUI(c);
        c.addMouseListener(this);
        c.setBorder(null);
    }

    @Override
    public void uninstallUI(JComponent c) {
        super.uninstallUI(c);
        c.removeMouseListener(this);
    }

    @Override
    public void mouseClicked(MouseEvent e) {

    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {
        System.out.print("Entered");
        changeBarColor(e.getComponent(), hoverColor);
    }

    @Override
    public void mouseExited(MouseEvent e) {
        System.out.print("Exited");
    }

    private void changeBarColor(Component c, Color co) {
        c.setBackground(co);
    }
}

1 个答案:

答案 0 :(得分:1)

首先,如果您对“普通”UI设计不满意,您应该考虑改变外观。 我个人讨厌MetalLookAndFeel,我习惯于为我的swing应用程序设置一个依赖于系统的外观。 查看this链接了解更多详情。

如果您只想更改曲目和/或拇指颜色,可以覆盖BasicScrollBarUI paintThumb和paintTrack方法。

滚动条的按钮也可以更改。

试试这个例子(下面的截图):

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.plaf.basic.BasicScrollBarUI;
public class Main
{
    public static void main (String [] a) {
        SwingUtilities.invokeLater (new Runnable () {
            @Override public void run () {
                try {
                    UIManager.setLookAndFeel (UIManager.getSystemLookAndFeelClassName ());
                    createAndShowGUI ();
                }
                catch (Exception e) {
                    JOptionPane.showMessageDialog (null, "An unexpected error occurred : " + e.getClass ().getSimpleName (), "Error", JOptionPane.ERROR_MESSAGE);
                }
            }
        });
    }
    private static void createAndShowGUI () {
        JFrame frame = new JFrame ("Test ScrollBar");
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        frame.setContentPane (new CustomScrollPane ());
        frame.pack ();
        frame.setLocationRelativeTo (null);
        frame.setVisible (true);
    }
}
class CustomScrollPane extends JScrollPane
{
    public CustomScrollPane () {
        super (VERTICAL_SCROLLBAR_ALWAYS, HORIZONTAL_SCROLLBAR_ALWAYS);
        Image imageThumb = createImage (16, 16, Color.RED);
        Image imageThumbPressed = createImage (16, 16, Color.MAGENTA);
        Image imageTrack = createImage (16, 16, Color.YELLOW);
        // Adding some test labels
        JPanel view = new JPanel (new GridLayout (10, 5, 20, 50));
        for (int i = 0; i < 50; i++) view.add (new JLabel ("Test Label " + String.valueOf (i)));
        setViewportView (view);
        setHorizontalScrollBar (new CustomScrollBar (JScrollBar.HORIZONTAL, imageThumb, imageThumbPressed, imageTrack));
        setVerticalScrollBar (new CustomScrollBar (JScrollBar.VERTICAL, imageThumb, imageThumbPressed, imageTrack));
        // Setting preferred size for convenience. Not a good practice !
        setPreferredSize (new Dimension (200, 200));
    }
    private Image createImage (int width, int height, Color color) {
        BufferedImage image = new BufferedImage (width, height, BufferedImage.TYPE_INT_ARGB);
        Graphics g = image.getGraphics ();
        g.setColor (color);
        g.fillRect (0, 0, width, height);
        g.dispose ();
        return image;
    }
}
class CustomScrollBar extends JScrollBar
{
    private boolean isThumbPressed;

    public CustomScrollBar (final int orientation, final Image thumb, final Image thumbPressed, final Image track) { 
        super (orientation);
        addMouseListener (new MouseAdapter () {
            public void mousePressed (MouseEvent e) {
                isThumbPressed = true;
            }
            public void mouseReleased (MouseEvent e) {
                isThumbPressed = false;
            }
        });
        setUI (new BasicScrollBarUI () {
            @Override protected void paintThumb (Graphics g, JComponent c, Rectangle r) {
                if (isThumbPressed) g.drawImage (thumbPressed, r.x, r.y, r.width, r.height, null);
                else g.drawImage (thumb, r.x, r.y, r.width, r.height, null);
            }
            @Override protected void paintTrack (Graphics g, JComponent c, Rectangle r) {
                g.drawImage(track, r.x, r.y, r.width, r.height, null);
            }
        });
    }
}

GUI screenshot