繁体   English   中英

尝试在适当的时候禁用JButton

[英]Trying to disable JButtons when appropriate

该程序读取数据文件并将内容存储到向量中。 然后,当用户单击下一个和上一个按钮时,它将在文本字段中显示每个项目。 我想在显示第一个项目时禁用previousButton,而在显示最后一个项目时禁用nextButton。

import java.io.*;
import java.util.Vector;
import java.util.Scanner;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
import javax.swing.*;

public class choosing_fruit extends JFrame implements ActionListener
{
    private JTextField dataItemTextField;
    private JButton nextButton, previousButton, closeButton;
    private Vector<String> v; 
    private int current = 0; // index value of current item shown in text field
    public static void main(String[] args) throws FileNotFoundException
    {
        choosing_fruit f = new choosing_fruit();
    }

    public choosing_fruit()
    {
        v = new Vector<String>();
        nextButton = new JButton(">>");
        previousButton = new JButton("<<");
        closeButton = new JButton("Close");
        dataItemTextField = new JTextField(20);
        setLayout(new FlowLayout());

        this.add(dataItemTextField);
        this.add(previousButton);
        this.add(nextButton);
        this.add(closeButton);

        nextButton.addActionListener(this);
        previousButton.addActionListener(this);
        closeButton.addActionListener(this);

        try
        {  
            Scanner in = new Scanner(new File("fruit.dat"));
            String inputLine;
            while(in.hasNextLine()) 
            {
                inputLine = in.nextLine();
                v.add(inputLine);
            }

            in.close(); 
        }
        catch(Exception e)      
        {       
            JOptionPane.showMessageDialog(null,"error");    
        }

        dataItemTextField.setText(v.get(current)); // show 1st item

        this.setTitle("Choosing Fruit");
        this.pack();
        this.setVisible(true);
    }
    public void actionPerformed(ActionEvent ae)
    {   
        if(current > 0)
        {
            previousButton.setEnabled(true);
        }
        else
        {
            previousButton.setEnabled(false);
        }
        if(current < 5)
        {
            nextButton.setEnabled(true);
        }
        else
        {
            nextButton.setEnabled(false);
        }

        if(ae.getSource() == nextButton) 
        {
            if(current >= 0)
            {
                current = current + 1;
                dataItemTextField.setText(v.get(current));
                System.out.println(current);
            }           
        }

        else if(ae.getSource() == previousButton) 
        {
            if(current <= 5)
            {
                current = current - 1;
                dataItemTextField.setText(v.get(current));
                System.out.println(current);
            }   
        }
        else if(ae.getSource() == closeButton)
        {
            shutDown();
        }

    }
        private void shutDown()     
        {
            this.dispose();
        }

}

更改当前位置后,尝试更新按钮的状态

public void actionPerformed(ActionEvent ae) {
    if (ae.getSource() == nextButton) {
        if (current >= 0) {
            current = current + 1;
            dataItemTextField.setText(v.get(current));
            System.out.println(current);
        }
    } else if (ae.getSource() == previousButton) {
        if (current <= 5) {
            current = current - 1;
            dataItemTextField.setText(v.get(current));
            System.out.println(current);
        }
    } else if (ae.getSource() == closeButton) {
        shutDown();
    }

    previousButton.setEnabled(current > 0);
    nextButton.setEnabled(current < v.size() - 1);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM