JButton背景图片 [英] JButton background images

查看:314
本文介绍了JButton背景图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个POS系统(销售点)以供娱乐. POS系统的典型特征是按钮具有不同产品的不同背景颜色.但是,我并不是说按钮图像背后的背景,我的意思是像这张随机图片一样,我下了google图片:

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:

注意,我改变了系统的外观

Note I changed the look and feel to the system one

我需要使用按钮执行类似THAT的操作.我知道有几种可能的方法:

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

  1. 为我的按钮创建外观吗?
  2. 完全重写我的JButtons中的paintComponenet方法(但这是一种相当可悲的方式...并且当我重写它时,不会绘制边框+文本...显然是个坏主意)
  3. 使用button.setContentAreaFilled(false);,并将与按钮大小相同的JPanel放在按钮后面.

我真的不知道如何创建自己的外观,这听起来很痛苦,尤其是对于一个按钮来说,尤其如此.我听到了一些破坏外观的感觉,这让我不敢相信.第三种方法听起来很合理,但并不是很困难,但是我做我想做的 最好 方法是什么?

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

推荐答案

    • 为按钮创建外观?

      Creating a look and feel for my buttons?

      1. 外观类似主题

      1. Look and Feel is something like as theme,

      默认情况下,此ColorScheme具有一种,两种,三种颜色,此主题适用于所有Swing JComponent,

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

      然后所有Swing JComponent都具有相同的颜色ColorScheme

      then all Swing JComponents have got the same Color, ColorScheme

      不是您想要的东西

    • 完全覆盖我的JButtons中的paintComponenet方法(但是 这是一种非常可悲的方式...而边框+文字不是 当我重写它时画出来……显然是个坏主意)

      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具有颜色数组

      1. JButton has array of Colors

      您可以覆盖paintComponent,使用一种颜色(不是您想要的颜色)填充整个区域,矩形或使用GradientPaint

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

      您可以覆盖 BasicButtonUI

      覆盖 UIManager中的适当键,并在其中放置数组颜色

    • 使用button.setContentAreaFilled(false);,并放置一个JPanel 与按钮后面的按钮大小相同.

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

      1. 这可能是最简单的方法,准备图标(或下载图标集)

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

      使用正确的方法,实现(鼠标和键和键绑定)事件

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

      覆盖ButtonModel(通过使用ChangeListener )

    • 这篇关于JButton背景图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆