gpt4 book ai didi

java - 禁用 java 边框

转载 作者:行者123 更新时间:2023-11-30 11:46:21 27 4
gpt4 key购买 nike

我想在组件被禁用时禁用该组件的任何边框。

当您按原样运行下面的代码时,您会在其中一个文本区域周围看到一个红色的 LineBorder。我想在禁用 TextArea 时禁用该边框。

下面代码中的 useGenericSolution block 中是否有适用于所有边框的代码?

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

public class EnableBorder extends JFrame
{
private static final long serialVersionUID = 1L;
private static JFrame _instance;

public EnableBorder()
{
setResizable(false);
setTitle("My Frame");
setSize(300, 300);
getContentPane().add(new MyPanel());
_instance = this;
}

private class MyPanel extends JPanel
{
private static final long serialVersionUID = 1L;

public MyPanel()
{
setLayout(new FlowLayout());
JButton btnDisableAll = new JButton("Disable everything");
btnDisableAll.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
EnableBorder.setEnabledAllComponents(_instance, false);
}
});
add(btnDisableAll);

// Use TitledBorder
JTextArea titledTA = new JTextArea("This is some text that should be wide enough ");
titledTA.setSize(400, 50);
titledTA.setBorder(new TitledBorder("A titled border"));
add(titledTA);

// Use LineBorder
JTextArea lineBorderTA = new JTextArea("This is just some more text ...");
lineBorderTA.setSize(400, 50);
lineBorderTA.setBorder(new LineBorder(Color.RED, 1, true));
add(lineBorderTA);
}
}

/**
* Enables or disables a Container and all Components within a Container.
*
* @param b
* - The value to set the 'enabled' flag to
*/
public static void setEnabledAllComponents(Container cont, boolean b)
{
for (Component c : cont.getComponents())
{
setEnabledComponent(c, b);
}
setEnabledComponent(cont, b);
}

/**
* Enables or disables a Component.
* <p>
* If the component is a Container, all of it the Components within the Container will also be modified.
*
* @param c
* - The Component to modify
* @param b
* - The value to set the 'enabled' flag to
*/
public static void setEnabledComponent(Component c, boolean b)
{

if (c instanceof Container)
{
for (Component containerComp : ((Container) c).getComponents())
{
setEnabledComponent(containerComp, b);
}
}

c.setEnabled(b);

if (c instanceof JComponent)
{

Border border = ((JComponent) c).getBorder();
if (border != null)
{
boolean useGenericSolution = true;
if (useGenericSolution)
{
Insets insets = border.getBorderInsets(c);
System.out.println("Insets: " + insets);
Graphics g = c.getGraphics();

if (g != null)
{
/*
* TODO: Is it possible to get foreground color from the current object
* and force the border to be painted using that color
*/

Color color = g.getColor();

border.paintBorder(c, g, c.getX() - 5 - insets.left, c.getY() - insets.top, c.getWidth() + insets.left + insets.right,
c.getHeight() + insets.top + insets.bottom);
}
}
else
{
// TitledBorders can be done this way... but, a generic solution would be better
if (border instanceof TitledBorder)
{
if (b)
{
// Change border colors to color found in an enabled label
((TitledBorder) border).setTitleColor((Color) UIManager.get("Label.enabledForeground"));
}
else
{
// Change border colors to color found in a disabled label
((TitledBorder) border).setTitleColor((Color) UIManager.get("Label.disabledForeground"));
}
}
}
}
}

}

private static void createAndShowGUI()
{
// Create and set up the window.
EnableBorder frame = new EnableBorder();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
try
{
createAndShowGUI();
}
catch (Exception e)
{
e.printStackTrace();
System.exit(0);
}
}
});
}

}

最佳答案

只需在通用解决方案 block 中添加以下行

if (useGenericSolution) {
((JComponent) c).setBorder(BorderFactory.createEmptyBorder());
}

关于java - 禁用 java 边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9833867/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com