禁用剩余按钮,单击一个按钮 [英] Disabling remaining Buttons, On Click of one button
问题描述
我在Swing应用程序中禁用/启用按钮时遇到小问题。这是一个非常基本的问题,但我是这个概念的新手。
我有一个要求,有2个按钮可以执行2个单独的操作。
1)最初跑步B1& B2应该是可见的/启用的。
2)如果B1点击,那么B2应该被禁用/不可见。完成B1工作后,B1和& B2应该是可见/已启用。
3)如果B2单击,则B1应被禁用/不可见。 B2工作完成后,B1& B2应该是可见的/启用的。
但是,我的问题是,处理完B1 / B2后SetEnable / SetVisible正在工作。
它应该在开始之前被禁用并在完成任务后启用
在这种情况下有人可以提供帮助吗?
以下是我的代码...
i have small problem while disabling/enabling buttons in Swing Application. It's very basic problem, but I'm new to this concept.
I have a requirement like, having 2 buttons to perform 2 individual operations.
1)While Running initially B1 & B2 Should be Visible/Enabled.
2)If B1 Clicked, then B2 should be disabled/Invisible. After B1 work Completed, then Both B1 & B2 Should be Visible/Enabled.
3)If B2 Clicked, then B1 should be disabled/Invisible. After B2 work Completed, then Both B1 & B2 Should be Visible/Enabled.
But, my problem, is, after processing B1/B2 SetEnable/SetVisible is working.
It should be disabled before start and enabled after completing task
Can anyone help in this case?
Below is my Code...
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
public class FinCSV implements ActionListener {
JFrame frame = new JFrame("ICON");
JPanel panel = new JPanel(new GridLayout(2, 1, 4, 4));
JButton primaryButton = new JButton(" Primary Process");
JButton secondaryButton = new JButton(" Secondary Process");
public static void main(String[] args)
throws Exception {
FinCSV msql = new FinCSV();
msql.initGUI();
}
public void initGUI() {
this.primaryButton.setActionCommand("Primary");
this.secondaryButton.setActionCommand("Secondary");
this.primaryButton.addActionListener(this);
this.secondaryButton.addActionListener(this);
this.primaryButton.setFont(new Font("Serif", Font.BOLD, 22));
this.secondaryButton.setFont(new Font("Serif", Font.BOLD, 22));
panel.add(this.primaryButton);
panel.add(this.secondaryButton);
frame.setContentPane(panel);
frame.setSize(650, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Primary")) {
secondaryButton.setVisible(false);
// secondaryButton.setEnabled(false);
for (int i = 0; i < 10000; i++) {
System.out.println("[ " + (i + 1) + " ] Inside Primary Process");
}
secondaryButton.setVisible(true);
// secondaryButton.setEnabled(true);
} else if (e.getActionCommand().equals("Secondary")) {
primaryButton.setVisible(false);
// primaryButton.setEnabled(false);
for (int i = 0; i < 10000; i++) {
System.out.println("[ " + (i + 1) + " ] Inside Secondary Process");
}
primaryButton.setVisible(true);
// primaryButton.setEnabled(true);
}
}
}
推荐答案
我更新了标记。
您正在使用:
I updated the markup.
You are using:
primaryButton.setVisible(true);
使按钮工作/不工作。这不是一个好习惯,因为用户会错过控件 - 并且UI中的动态更改可能会轻易搞砸所有内容。
请使用
to make the buttons working/not working. That is not good practice, cause the user is going to miss the controlls - and the dynamic change in your UI will probably easily mess up everything.
Please use
primaryButton.setEnable(true); // enable
primaryButton.setEnable(false); // disable
哪个应该更好。
你可能需要在改变按钮状态后重绘你的UI。
that should work better.
YOu might have to redraw your UI after changing the state of the buttons.
这篇关于禁用剩余按钮,单击一个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!