不可编辑JTextField的Swing Nimbus更改样式 [英] Swing Nimbus change style of a non editable JTextField

查看:172
本文介绍了不可编辑JTextField的Swing Nimbus更改样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法改变不可编辑的JTextField的样式?如果将TextField设置为editable = false,我们希望为它提供更灰色的背景.我想一种方法是

Is there a way to change the style of non editable JTextField? We would like to give the TextField a slightly more grayish background if it is set to editable = false. I guess one way would be

readonlyComponent.setBackground(Color.LIGHT_GRAY);

在每个组件上

,但这似乎容易出错且乏味.

on every component but this seems error prone and tedious.

我要实现的目标是:正常的JTextFiels应该按照灵气外观定义.只读字段,例如editable = false,应具有不同的背景色.禁用字段应按照nimbus LAF的定义显示.

What I want to achieve is this: Normal JTextFiels should appear as defined by the nimbus look and feel. Readonly fields, e.g. editable = false, should have a different background color. Disabled fields should appear as defined by the nimbus LAF.

我在 Nimbus样式列表中找不到条目

推荐答案

我在Nimbus样式列表中找不到条目

I couldn't find an entry in the Nimbus style list

    在这种情况下,
  • 可以从标准代码访问密钥,请注意,大多数密钥在Java7中是不可访问的(从sun.com更改为java.swing),f.i.超过一半的JLabel等方法.

    • in this case is there way, keys are accesible from standard code, note most of keys aren't accesible in Java7(changes from sun.com to java.swing), f.i. more than half methods for JLabel etc.

      另一种方法是通过覆盖主要和次要颜色

      another way is by override primary and secondary Colors

      以测试SeaGlass L& F(基于Nimbus),也许所有键都是固定的,并且可以设置颜色而不会被黑客入侵

      to test SeaGlass L&F (based on Nimbus), maybe all key are fixed and is possible to set Colors without hacks

      来自代码

      import java.awt.BorderLayout;
      import java.awt.Color;
      import java.awt.Graphics2D;
      import javax.swing.JButton;
      import javax.swing.JComponent;
      import javax.swing.JFrame;
      import javax.swing.JTextField;
      import javax.swing.Painter;
      import javax.swing.UIManager;
      
      public class NimbusTest3 {
      
          private static final long serialVersionUID = 1L;
          private JFrame frame = new JFrame();
          private JButton button = new JButton("Text");
          private JTextField label = new JTextField("Text");
          private JTextField label1 = new JTextField("Text");
      
          public NimbusTest3() {
              label.setEnabled(false);
              frame.add(button);
              frame.add(label, BorderLayout.NORTH);
              frame.add(label1, BorderLayout.SOUTH);
              frame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
              frame.pack();
              frame.setVisible(true);
          }
      
          public static void main(String args[]) {
              try {
                  for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
                      if ("Nimbus".equals(laf.getName())) {
                          UIManager.setLookAndFeel(laf.getClassName());
                          UIManager.getLookAndFeelDefaults().put("TextField[Disabled].backgroundPainter",
                                  new FillPainterUI(new Color(127, 255, 191)));
                          UIManager.getLookAndFeelDefaults().put("TextField[Disabled].textForeground", new Color(255, 0, 0));
                      }
                  }
              } catch (Exception e) {
                  e.printStackTrace();
              }
              java.awt.EventQueue.invokeLater(new Runnable() {
                  @Override
                  public void run() {
                      NimbusTest3 nimbusTest3 = new NimbusTest3();
                  }
              });
          }
      
          static class FillPainterUI implements Painter<JComponent> {
              // fills whole, available area, ignoring rounded corners, Borders e.i.
              private final Color color;
      
              FillPainterUI(Color c) {
                  color = c;
              }
      
              @Override
              public void paint(Graphics2D g, JComponent object, int width, int height) {
                  g.setColor(color);
                  g.fillRect(0, 0, width - 1, height - 1);
              }
          }
      }
      

      这篇关于不可编辑JTextField的Swing Nimbus更改样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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