首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >禁用JTextField的边框

禁用JTextField的边框
EN

Stack Overflow用户
提问于 2014-03-31 03:00:46
回答 2查看 506关注 0票数 0

有没有一种方法可以禁用JTextField而不松开它的边界?基本上,我得到了几个文本字段,其中一些是启用的,一些是禁用的。然而,残疾人的没有边界。我希望所有的文本字段看起来都一样,无论他们是启用还是禁用。有没有办法做到这一点?

感谢您的回复

EN

回答 2

Stack Overflow用户

发布于 2014-03-31 03:28:26

在本程序中,您可以找到解决方案

代码语言:javascript
运行
复制
  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);  
}  
}  
票数 1
EN

Stack Overflow用户

发布于 2014-03-31 03:26:53

你能试试JTextField text = new JTextField; text.setVisible(false);吗?我不确定这能不能行得通,但试一试也不会有什么损失。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22748399

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档