Java 禁用JTextField';边界
有没有一种方法可以在不丢失JTextField边界的情况下禁用JTextField?基本上我有几个文本字段,其中一些是启用的,一些是禁用的。然而,残疾人没有边界。我希望所有文本字段在启用或禁用时看起来都一样。有办法吗Java 禁用JTextField';边界,java,swing,border,jtextfield,Java,Swing,Border,Jtextfield,有没有一种方法可以在不丢失JTextField边界的情况下禁用JTextField?基本上我有几个文本字段,其中一些是启用的,一些是禁用的。然而,残疾人没有边界。我希望所有文本字段在启用或禁用时看起来都一样。有办法吗 谢谢你的回答,你能试试吗; text.setVisible(false)我不确定这是否有效,但尝试并不会造成任何损失 在这个程序中,您可以找到解决方案 import java.awt.*; import java.awt.event.*; import java
谢谢你的回答,你能试试吗;
text.setVisible(false)代码>我不确定这是否有效,但尝试并不会造成任何损失 在这个程序中,您可以找到解决方案
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);
}
}
您是否使用了任何特殊的外观,因为禁用控件时默认的
JTextField
不会消失。