Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 尝试在适当时禁用JButtons_Java_Swing_Awt_Actionevent - Fatal编程技术网

Java 尝试在适当时禁用JButtons

Java 尝试在适当时禁用JButtons,java,swing,awt,actionevent,Java,Swing,Awt,Actionevent,程序读入数据文件并将内容存储到向量中。然后,当用户单击“下一步”和“上一步”按钮时,它会在文本字段中显示每个项目。我想在显示第一项时禁用previousButton,在显示最后一项时禁用nextButton import java.io.*; import java.util.Vector; import java.util.Scanner; import java.awt.*; import java.awt.event.*; import java.awt.event.ActionListe

程序读入数据文件并将内容存储到向量中。然后,当用户单击“下一步”和“上一步”按钮时,它会在文本字段中显示每个项目。我想在显示第一项时禁用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();
        }

}
import java.io.*;
导入java.util.Vector;
导入java.util.Scanner;
导入java.awt.*;
导入java.awt.event.*;
导入java.awt.event.ActionListener;
导入javax.swing.*;
公共类扩展JFrame实现ActionListener
{
私有JTextField数据项textfield;
私有JButton nextButton、previousButton、closeButton;
私有向量v;
private int current=0;//文本字段中显示当前项的索引值
公共静态void main(字符串[]args)引发FileNotFoundException
{
选择水果f=新选择水果();
}
公众选择水果()
{
v=新向量();
nextButton=新的JButton(“>>”);

previousButton=new JButton(“尝试在更改当前位置后更新按钮的状态

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);
}
public void actionPerformed(ActionEvent ae){
if(ae.getSource()==nextButton){
如果(当前>=0){
电流=电流+1;
dataItemTextField.setText(v.get(当前));
系统输出打印项次(当前);
}
}else if(ae.getSource()==previousButton){
如果(当前为0);
nextButton.setEnabled(当前
更改当前位置后,尝试更新按钮的状态

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);
}
public void actionPerformed(ActionEvent ae){
if(ae.getSource()==nextButton){
如果(当前>=0){
电流=电流+1;
dataItemTextField.setText(v.get(当前));
系统输出打印项次(当前);
}
}else if(ae.getSource()==previousButton){
如果(当前为0);
nextButton.setEnabled(当前