JButton background images

2019-07-16 13:39发布

问题:

I'm writign a POS system(point of sale) for fun. A typical feature of a POS system is buttons that have different background colors for different products. But I don't mean the background behind the button's image, i mean like in this random picture I got off google images:

Edit: Note I changed the look and feel to the system one

I need to do something like THAT with my buttons. I know a few ways this COULD be possible:

  1. Creating a look and feel for my buttons?
  2. Completely overriding the paintComponenet method in my JButtons(But that's a pretty pathetic way to do it... and the border + text isn't drawn when I override that... obviously a bad idea)
  3. Using button.setContentAreaFilled(false);, and putting a JPanel of the same size as the button behind the button.

I don't really know how to create my own look and feel, and it sounds like a pain, especially just for 1 button, and I heard something about breaking the look and feel which scared me away from that idea. The 3rd way sounds plausible, and not extremely difficult, but I what is the very best way to do what I am trying to do?

Right now my code for creating butons is this:

JButton b = new JButton(text);
    b.addActionListener(this);
    b.setFont(Main.f);
    b.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
    buttons.add(b);
    return b;

I've messed around with this code(Mostly for fun, not practicality):

JButton b = new JButton(text){
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            Color bg = getBackground();
            int borderchange = -50;
            g.setColor(new Color(
                    Math.max(0,bg.getRed()+borderchange),
                    Math.max(bg.getGreen()+borderchange,0),
                    Math.max(bg.getBlue()+borderchange,0)));
            g.fillRect(0,0,getWidth(),getHeight());
            g.setColor(getBackground());
            int border = 4;
            g.fillRect(border,border,getWidth()-border,getHeight()-border);
            g.setColor(Color.BLACK);
            g.setFont(getFont());
            g.drawString(getText(),getWidth()/2,getHeight()/2);
        }
    };

回答1:

  • Creating a look and feel for my buttons?

    1. Look and Feel is something like as theme,

    2. by default this ColorScheme has a one, two, three Colors, this theme is applied for all Swing JComponents,

    3. then all Swing JComponents have got the same Color, ColorScheme

    4. not something that you looking for

  • Completely overriding the paintComponenet method in my JButtons(But that's a pretty pathetic way to do it... and the border + text isn't drawn when I override that... obviously a bad idea)

    1. JButton has array of Colors

    2. you can to override paintComponent, fills whole area, Rectangle with one Color (not something that you looking for) or to use GradientPaint

    3. you can to override BasicButtonUI

    4. override proper key in UIManager and put there arrays of Colors

  • Using button.setContentAreaFilled(false);, and putting a JPanel of the same size as the button behind the button.

    1. this could be easiest of ways, prepare Icons (or download set of Icons)

    2. use proper methods, implemented (Mouse & Key & KeyBindings) events

      • in the JButton API

      • override ButtonModel (by using ChangeListener)



回答2:

Have you tried:

yourButton.setBackground(COLOR)

?