当某些组件更改其边框颜色时启用 JButton
Enable a JButton when some component changed its border color
我的目标是仅当某些 JTextFields 和 JComboBox 将其边框颜色从红色更改为绿色时才启用 JButton。
这些组件包含在三个不同的 JPanel 中。
我尝试创建一个读取 JPanel 中所有组件的函数,但是当我要比较颜色时,程序 returns 我认为我以错误的方式转换变量。
下面是我的函数。
有人可以帮助我吗?
public static boolean countBoards(JPanel panel){
boolean red = false;
for(Component control : panel.getComponents())
{
if(control instanceof JTextField)
{
JTextField ctrl = (JTextField) control;
Color lineColor = ((LineBorder)ctrl.getBorder()).getLineColor();
if(lineColor.equals(Color.red))
red = true;
}
else if(control instanceof JComboBox)
{
JComboBox ctr = (JComboBox) control;
Color lineColor = ((LineBorder)ctr.getBorder()).getLineColor();
if(lineColor.equals(Color.red))
red = true;
}
}
return red;
}
当您更改组件的边框时,将触发 属性 侦听器。您可以根据新边框向 combobox/textfield 和 enable/disable 按钮注册一个 属性 侦听器。
一个例子:
@Test
public void test() {
JButton myButton = new JButton();
JComboBox<String> combo = new JComboBox<>();
combo.addPropertyChangeListener("border", e -> {
if (e.getNewValue() != null && e.getNewValue() instanceof LineBorder) {
LineBorder border = (LineBorder) e.getNewValue();
myButton.setEnabled(border.getLineColor().equals(Color.green));
}
});
assertTrue(myButton.isEnabled(), "Button should be initially enabled.");
combo.setBorder(BorderFactory.createLineBorder(Color.red));
assertFalse(myButton.isEnabled(), "Button should be disabled when red line border occurs.");
combo.setBorder(BorderFactory.createLineBorder(Color.green));
assertTrue(myButton.isEnabled(), "Button should be enabled when green line border occurs.");
}
我的目标是仅当某些 JTextFields 和 JComboBox 将其边框颜色从红色更改为绿色时才启用 JButton。
这些组件包含在三个不同的 JPanel 中。
我尝试创建一个读取 JPanel 中所有组件的函数,但是当我要比较颜色时,程序 returns 我认为我以错误的方式转换变量。
下面是我的函数。
有人可以帮助我吗?
public static boolean countBoards(JPanel panel){
boolean red = false;
for(Component control : panel.getComponents())
{
if(control instanceof JTextField)
{
JTextField ctrl = (JTextField) control;
Color lineColor = ((LineBorder)ctrl.getBorder()).getLineColor();
if(lineColor.equals(Color.red))
red = true;
}
else if(control instanceof JComboBox)
{
JComboBox ctr = (JComboBox) control;
Color lineColor = ((LineBorder)ctr.getBorder()).getLineColor();
if(lineColor.equals(Color.red))
red = true;
}
}
return red;
}
当您更改组件的边框时,将触发 属性 侦听器。您可以根据新边框向 combobox/textfield 和 enable/disable 按钮注册一个 属性 侦听器。
一个例子:
@Test
public void test() {
JButton myButton = new JButton();
JComboBox<String> combo = new JComboBox<>();
combo.addPropertyChangeListener("border", e -> {
if (e.getNewValue() != null && e.getNewValue() instanceof LineBorder) {
LineBorder border = (LineBorder) e.getNewValue();
myButton.setEnabled(border.getLineColor().equals(Color.green));
}
});
assertTrue(myButton.isEnabled(), "Button should be initially enabled.");
combo.setBorder(BorderFactory.createLineBorder(Color.red));
assertFalse(myButton.isEnabled(), "Button should be disabled when red line border occurs.");
combo.setBorder(BorderFactory.createLineBorder(Color.green));
assertTrue(myButton.isEnabled(), "Button should be enabled when green line border occurs.");
}