作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
所以我的 GUI 有这么一小段代码:
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.BoxLayout;
import javax.swing.JSeparator;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class IPGUI extends JFrame implements ActionListener
{
private static JPanel contentPane;
private static boolean btn1Clicked = false;
private static boolean btn2Clicked = false;
private static boolean btn3Clicked = false;
private static boolean btn4Clicked = false;
//Create the frame
public IPGUI()
{
//Sets frame properties
setTitle("IP Extractor");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 250, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
//Creates new JPanel with boxlayout
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.CENTER);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
//////////////////New Button//////////////////
JButton btnConvertDocuments = new JButton("1. Convert Documents");
btnConvertDocuments.setAlignmentX(Component.CENTER_ALIGNMENT);
btnConvertDocuments.setMaximumSize(new Dimension(160, 0));
btnConvertDocuments.setPreferredSize(new Dimension(0, 50));
panel.add(btnConvertDocuments);
btnConvertDocuments.setActionCommand("w");
btnConvertDocuments.addActionListener((ActionListener) this);
if (btn1Clicked == true)
{
btnConvertDocuments.setEnabled(false);
}
JSeparator separator_3 = new JSeparator();
panel.add(separator_3);
//////////////////New Button//////////////////
JButton btnExtractImages = new JButton("2. Extract Images");
btnExtractImages.setAlignmentX(Component.CENTER_ALIGNMENT);
btnExtractImages.setMaximumSize(new Dimension(160, 0));
btnExtractImages.setPreferredSize(new Dimension(0, 50));
panel.add(btnExtractImages);
btnExtractImages.setActionCommand("x");
btnExtractImages.addActionListener((ActionListener) this);
if (btn2Clicked == true)
{
btnExtractImages.setEnabled(false);
}
JSeparator separator_2 = new JSeparator();
panel.add(separator_2);
//////////////////New Button//////////////////
JButton btnParseRIDValues = new JButton("3. Parse rId Values");
btnParseRIDValues.setAlignmentX(Component.CENTER_ALIGNMENT);
btnParseRIDValues.setMaximumSize(new Dimension(160, 0));
btnParseRIDValues.setPreferredSize(new Dimension(0, 50));
panel.add(btnParseRIDValues);
btnParseRIDValues.setActionCommand("y");
btnParseRIDValues.addActionListener((ActionListener) this);
if (btn3Clicked == true)
{
btnParseRIDValues.setEnabled(false);
}
JSeparator separator_1 = new JSeparator();
panel.add(separator_1);
//////////////////New Button//////////////////
JButton btnParseImageInfo = new JButton("4. Parse Image Info.");
btnParseImageInfo.setAlignmentX(Component.CENTER_ALIGNMENT);
btnParseImageInfo.setMaximumSize(new Dimension(160, 0));
btnParseImageInfo.setPreferredSize(new Dimension(0, 50));
panel.add(btnParseImageInfo);
btnParseImageInfo.setActionCommand("z");
btnParseImageInfo.addActionListener((ActionListener) this);
if (btn4Clicked == true)
{
btnParseImageInfo.setEnabled(false);
}
}
public void actionPerformed(ActionEvent event)
{
String command = event.getActionCommand();
if (command.equals("w"))
{
FileConverter fc = new FileConverter();
btn1Clicked = true;
}
else if (command.equals("x"))
{
ImageExtractor ie = new ImageExtractor();
btn2Clicked = true;
}
else if (command.equals("y"))
{
XMLIDParser xip = new XMLIDParser();
btn3Clicked = true;
}
else if (command.equals("z"))
{
XMLTagParser xtp = new XMLTagParser();
btn4Clicked = true;
}
}
}
我想特别关注的部分是有条件的:
if (btn1Clicked == true)
{
btnConvertDocuments.setEnabled(false);
}
所以我相信该命令应该做的是:单击按钮并调用执行操作的方法后,btnClicked 应该设置为 true,因此该按钮应该被禁用。
有人能解释一下我这里哪里出错了,或者我的想法是否正确?预先感谢您的任何意见!
最佳答案
您应该将语句 btnConvertDocuments.setEnabled(false);
放在 actionPerformed(ActionEvent event)
方法中。当实例化 IPGUI 对象时,上面的条件仅在构造函数中调用一次。
if (command.equals("w")) {
FileConverter fc = new FileConverter();
btn1Clicked = true;
btnConvertDocuments.setEnabled(false);
}
关于java - 如何在 Java 中禁用 GUI 按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6835682/
我是一名优秀的程序员,十分优秀!