【问题标题】:Disabled JTextField's border禁用 JTextField 的边框
【发布时间】:2014-03-31 03:57:59
【问题描述】:

有没有办法在不丢失边框的情况下禁用 JTextField?基本上我有几个文本字段,其中一些是启用的,有些是禁用的。然而,残疾人没有边界。我希望所有文本字段看起来都一样,无论它们是启用还是禁用。有什么办法可以做到吗?

感谢您的任何回答

【问题讨论】:

  • 您是否使用了任何特殊的外观,因为禁用控件时默认的JTextField 不会消失。

标签: java swing border jtextfield


【解决方案1】:

在这个程序中你可以找到解决方案

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

 public class DressingUpComponents  
{  
   JTextField disabled,  
              normal;  
  JLabel     label;  

public DressingUpComponents()  
{  
    configureDisabledTextField();  
    normal = new JTextField("hello world");  
    configureLabel();  
}  

private void configureDisabledTextField()  
{  
    disabled = new JTextField("hello world");  
    disabled.setEnabled(false);  
    Color bgColor = UIManager.getColor("TextField.background");  
    disabled.setBackground(bgColor);  
    Color fgColor = UIManager.getColor("TextField.foreground");  
    disabled.setDisabledTextColor(fgColor);  
    disabled.setBorder(BorderFactory.createEtchedBorder());  
}  

private void configureLabel()  
{  
    label = new JLabel("hello world");  
    label.setBorder(BorderFactory.createEtchedBorder());  
    label.setOpaque(true);         // required for background colors  
    label.setBackground(UIManager.getColor("TextField.background"));  
    label.setFont(UIManager.getFont("TextField.font"));  
}  

public static void main(String[] args)  
{  
    DressingUpComponents dup = new DressingUpComponents();  
    JFrame f = new JFrame();  
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
    Container cp = f.getContentPane();  
    cp.setLayout(new GridBagLayout());  
    GridBagConstraints gbc = new GridBagConstraints();  
    gbc.weighty = 1.0;                 // allow vertical dispersion  
    gbc.gridwidth = GridBagConstraints.REMAINDER;  // single column  
    cp.add(dup.disabled, gbc);  
    cp.add(dup.normal,   gbc);  
    cp.add(dup.label,    gbc);  
    f.setSize(200,200);  
    f.setLocation(200,200);  
    f.setVisible(true);  
}  
}  

【讨论】:

    【解决方案2】:

    你能试试JTextField text = new JTextField; text.setVisible(false);我不确定这是否可行,但尝试不会让任何东西丢失。

    【讨论】:

      猜你喜欢
      • 2012-05-16
      • 1970-01-01
      • 2022-11-21
      • 2012-09-09
      • 1970-01-01
      • 2011-01-17
      • 2011-02-20
      • 2021-04-28
      相关资源
      最近更新 更多