jbutton不起作用

时间:2015-11-25 02:50:14

标签: java swing awt jbutton

您好我有以下代码,我有一个开始和停止按钮。当按下开始时画布开始绘画但是我不能按下停止按钮直到开始操作完成。我需要在按钮按下时停止并恢复油漆。按下开始按钮后,在绘制画布之前无法按下其他按钮。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Hashtable;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.Timer;


public abstract class JUIApp extends JPanel implements ActionListener {
    public static CACanvas cacanvas= null;
    public ArrayList<int[]> genL;
    public JFrame frame = null;
    protected JPanel mainPanel = null;
    private JButton btn0 = null;
    private JButton btn1 = null;
    private JButton btn2 = null;
    @SuppressWarnings("rawtypes")
    DefaultComboBoxModel rules = null;
    @SuppressWarnings("rawtypes")
    JComboBox rulesCombo =null;
    JScrollPane ruleListScrollPane=null;
    JLabel lable=null;
    JTextField generation=null;
    JLabel gLable=null;
    public static String Rule;
    public  static String Generations;
    public boolean isChanged =false;
    public int gridCellSize=4;
    private static final long serialVersionUID = 1L;
    public int genCurrent=0;
    public int posCurrent=0;
    public int i;
    public Color cellColor= null;
    public Timer waitTimer;
    public static boolean waitFlag;
    public static boolean alert1Flag=false;
    public boolean stopFlag=false;
    public JLabel Alert1=new JLabel();
    public int genCheck=0;
    //private List<Point> fillCells;
    public JUIApp() {
        initGUI();

    }
    public void initGUI() {
        //fillCells = new ArrayList<>(25);
        frame = new JFrame();
        frame.setTitle("Cellular Automata Demo");
        frame.setSize(1050, 610);
        frame.setResizable(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setLayout(new BorderLayout());
        frame.add(getMainPanel(),BorderLayout.NORTH);
        frame.setVisible(true);
        Toolkit.getDefaultToolkit().setDynamicLayout(false); 
         cacanvas=new CACanvas();
            frame.add(cacanvas);
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    public JPanel getMainPanel() {
        mainPanel = new JPanel();
        mainPanel.setLayout(new FlowLayout());
        //cacanvas=new CACanvas();

        btn0 = new JButton("Start");
        btn0.addActionListener(this);
        //waitTimer = new Timer(1000, this);
        mainPanel.add(btn0);

        JButton btn2 = new JButton("Stop");
        btn2.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent s)
            {
                stopFlag=true;
                //cacanvas.repaint();
            }
        });
        mainPanel.add(btn2);
        btn1 = new JButton("Clear");
        btn1.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e)
            {
                CACanvas.clearFlag=true;
                generation.setText("");
                alert1Flag=true;
                rulesCombo.setSelectedIndex(0);
                  Alert1.setText("Please enter the number of generations");
                  Alert1.setBounds(30, 20, 5, 5);
                  Alert1.setVisible(alert1Flag);
                  mainPanel.add(Alert1);
                   cacanvas.repaint();
                   frame.setSize(1060, 610);

            }
    });
        mainPanel.add(btn1);
        lable=new JLabel();
        lable.setText("Select Rule :");
        mainPanel.add(lable);
        rules=new DefaultComboBoxModel();
        for (int i=0;i<=100;i++)
        {
            String p=String.valueOf(i);
        rules.addElement(p);
        }
        rules.addElement("250");
        rules.addElement("254");
        rulesCombo = new JComboBox(rules);    
        rulesCombo.setSelectedIndex(0);

         ruleListScrollPane = new JScrollPane(rulesCombo);
        mainPanel.add(ruleListScrollPane);
        //mainPanel.revalidate();
        gLable=new JLabel();
        gLable.setText("Enter the number of Generations (Max 64)");
        generation=new JTextField(2);

        mainPanel.add(gLable);
        mainPanel.add(generation);
       // mainPanel.add(cacanvas);
        return mainPanel;
    }


    public abstract void run();

    @Override
    public void actionPerformed(ActionEvent arg0) {
        Alert1.setVisible(false);
        waitFlag=false;
        System.out.println("We received an ActionEvent " + arg0);
         Generations=generation.getText();
        System.out.println(Generations);

         Rule = "";
        if (rulesCombo.getSelectedIndex() != -1) {                     
           Rule = 
                   (String) rulesCombo.getItemAt
                (rulesCombo.getSelectedIndex());             
        } 
        System.out.println(Rule);
        int rule=Integer.parseInt(Rule);
     Hashtable<String,Integer> rules= new Hashtable<String,Integer>();
        CARule ruleClass=new CARule();
        rules=ruleClass.setRule(rule);
        CAGenetationSet sa =new CAGenetationSet(100, false,rules);
         genL=new ArrayList<int[]>();
         genL=sa.runSteps(Integer.parseInt(Generations));
         System.out.println("calling pattern set");
        for(int i=0;i<=genL.size()-1;i++)
        {
            System.out.println("Painting generation :"+i);

            if(stopFlag==false)
            {

               cacanvas.repaint();
            }

            //genPaint();
            //sleep();
            int[] newCell=genL.get(i);
            for(int r=0;r<newCell.length;r++)
            {

                if(newCell[r]==1)
                {


                    System.out.println("Generaton is"+i+"CellLife is"+r);
                    cacanvas.fillCell(i,r);


        }
            }

        }




                    /*cacanvas.patternSet(genL);
                    waitFlag=true;


                  System.out.println("run completed");
                 // cacanvas.clearFlag=true;
*/  
    }   

    public void genPaint()
    {
        cacanvas.repaint();
    }

    public void sleep()
    {
        try
        {
            Thread.sleep(100);
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }

    }
    public static void main(String[] args) {
        JUIApp app = new JUIApp() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                System.out.println("Run method");

            }



    };
}
}

1 个答案:

答案 0 :(得分:0)

看起来你正在使用该任务的当前线程,你应该创建另一个线程并向新线程添加一个侦听器代码。