Új hozzászólás Aktív témák

  • smallmer

    őstag

    Sziasztok!

    Szeretném kérni a segítségeteket:
    Jelenleg így néz ki a kódom:

    public class testThread implements Runnable, ActionListener {

    JFrame frame;
    JButton gomb1;
    JButton gomb2;

    public void makeFrame() {
    frame = new JFrame("Frame");
    frame.setSize(1000, 500);
    frame.getContentPane().setBackground(new Color(74, 74, 74));
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    frame.setLayout(null);
    }

    public void makeButtons() {
    gomb1 = new JButton("Indítás");
    gomb1.setBounds(350, 300, 100, 30);
    gomb1.setBackground(new Color(127, 127, 127));
    frame.getContentPane().add(gomb1);
    gomb1.addActionListener(this);

    gomb2 = new JButton("Stop");
    gomb2.setBounds(550, 300, 100, 30);
    gomb2.setBackground(new Color(127, 127, 127));
    frame.getContentPane().add(gomb2);

    }

    public void actionPerformed(ActionEvent event) {
    if (event.getSource() == gomb1) {

    }
    }



    @Override
    public void run() {
    try {
    go();
    } catch (InterruptedException ex) {
    Logger.getLogger(server.class.getName()).log(Level.SEVERE, null, ex);
    }
    }


    public void go() throws InterruptedException {
    doMore();
    }

    public void doMore() throws InterruptedException {

    for (int i = 0; i < 20; i++) {
    System.out.println("Fut a thread" + i);

    }

    }
    }

    class Test {

    public static void main(String[] args) {
    testThread t = new testThread();
    t.makeFrame();
    t.makeButtons();
    Runnable r = new server();
    Thread szal = new Thread(r);
    Thread szal2 = new Thread(r);

    szal.start();
    szal2.start();
    }
    }

    Azt szeretném megvalósítani, hogy a gomb1-re nyomva elinduljon az egyik szám, majd a gomb2-re nyomva a thread stop() állapotba kerüljön. Tehát egyik gombra kattintva indítsa el a szal-t, majd a másikra nyomva állítsa meg. Hogyan kellene ehhez átalakítani a kódom?

    Köszönöm :R

Új hozzászólás Aktív témák

Hirdetés