,所以我必須爲我的GUI這一小塊代碼:如何禁用GUI按鈕在Java中
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,因此該按鈕應該被禁用。
有人可以解釋我在哪裏錯了,或者我在這裏有正確的想法嗎?提前感謝您的任何意見!
你還沒有聲明,如果它沒有做你想做的。我知道這很明顯,但當你點擊按鈕會發生什麼?任何錯誤? – DaMainBoss
它只是不禁用按鈕,用戶可以反覆點擊按鈕(我不想)。不過,我現在修好了。謝謝你:)。 –
看到我的回覆下面 – DaMainBoss