java swing program odliczający problem

0

witam, chcę napisać program który będzie wypisywał liczby od 0 do 100 w odstępach czasowych po kliknięciu na przycisk i wypisywane w komponencie JTextArea. Chciałem się dowiedzieć czy do tego lepiej jest użyć Timera który te liczby będzie wypisywał w odstępach czasowych czy lepiej SwingWorkera?? Według mnie Timer odpada ponieważ zależy mi na aktywności pozostałych przycisków a tak to będą zamarzać. Drugi mój problem to ScrollBar w komponencie JTextArea, jest dodany i widoczny ale pozostaje tak jakby nie aktywny. proszę o opinie, zamieszczam również kod

 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaswingodliczacz;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

/**
 *
 * @author marcin
 */
public class KlasaOdliczacz extends JFrame implements ActionListener{
   
    private int wysokosc = 100;
    private int szerokosc = 300;
    private int pozycja_okna_x = 500;
    private int pozycja_okna_y = 400;
    private JPanel jPanel = new JPanel();
    private JPanel jPanelLewy = new JPanel();
    private JPanel jPanelPrawy = new JPanel();
    private JButton jButtonStart = new JButton();
    private JButton jButtonStop = new JButton();
    private JButton jButtonReset = new JButton();
    private JTextArea jTextArea = new JTextArea();
    private JScrollPane jScrollPane = new JScrollPane();
    private GridLayout gridLayoutGlowny = new GridLayout(1, 2);
    private BorderLayout borderLayout = new BorderLayout();
    private GridLayout gridLayoutPrawy = new GridLayout(3, 1);

    public KlasaOdliczacz() {
        setTitle("odliczacz !!!");
        setLocation(pozycja_okna_x, pozycja_okna_y);
        setSize(szerokosc, wysokosc);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        add(jPanel);
       
        jTextArea.setText("");
        jTextArea.setLineWrap(true);
       
        jScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
       
        jButtonStart.setText("start");
        jButtonStart.addActionListener(this);
        jButtonReset.setText("reset");
        jButtonStop.setText("stop");
       
        jPanel.setLayout(gridLayoutGlowny);
        jPanel.add(jPanelLewy);
        jPanel.add(jPanelPrawy);
       
        jPanelLewy.setLayout(borderLayout);
        jPanelLewy.add(jTextArea, BorderLayout.CENTER);
        jPanelLewy.add(jScrollPane, BorderLayout.EAST);
       
        jPanelPrawy.setLayout(gridLayoutPrawy);
        jPanelPrawy.add(jButtonStart);
        jPanelPrawy.add(jButtonStop);
        jPanelPrawy.add(jButtonReset);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object object = e.getSource();
        if(object == jButtonStart){
            startLiczenia();
        }
    }
   
    void startLiczenia(){
        for(int i = 0; i < 100; i++){
            jTextArea.append(Integer.toString(i) + "\n");
            Thread.currentThread();
            try {   
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
                Logger.getLogger(KlasaOdliczacz.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
   
}

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaswingodliczacz;

import javax.swing.SwingUtilities;

/**
 *
 * @author marcin
 */
public class JavaSwingOdliczacz {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                KlasaOdliczacz klasaOdliczacz = new KlasaOdliczacz();
            }
        });
    }
}
0
  1. Może być Timer.
  2. JScrollPanel nie jest u Ciebie powiązany z JTextArea
private JScrollPane jScrollPane = new JScrollPane(jTextArea);
...
jPanelLewy.setLayout(borderLayout);
//jPanelLewy.add(jTextArea, BorderLayout.CENTER); tego wiersza ma nie być
jPanelLewy.add(jScrollPane, BorderLayout.CENTER);
 
jPanelPrawy.setLayout(gridLayoutPrawy);
0

działa ScrollBar, dziękuje

0

wstawiam, przerobiony kod. użyłem timera jednak timer wyświetla liczby w sposób zapętlony w odstępach czasowych między tymi pętlami. mi chodziło aby między każdą liczbą był odstęp czasowy i nie działało to w sposób zapętlony. chciałem się również dowiedzieć czemu nie działa metoda stop() i restart() obiektu timer podpięte do buttonów buttonStop i buttonRestart.

 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaswingodliczacz;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

/**
 *
 * @author marcin
 */
public class KlasaOdliczacz extends JFrame implements ActionListener{
    
    private int wysokosc = 150;
    private int szerokosc = 300;
    private int pozycja_okna_x = 500;
    private int pozycja_okna_y = 400;
    private JPanel jPanel = new JPanel();
    private JPanel jPanelLewy = new JPanel();
    private JPanel jPanelPrawy = new JPanel();
    private JButton jButtonStart = new JButton();
    private JButton jButtonStop = new JButton();
    private JButton jButtonReset = new JButton();
    private JTextArea jTextArea = new JTextArea();
    private JScrollPane jScrollPane = new JScrollPane(jTextArea); 
    private GridLayout gridLayoutGlowny = new GridLayout(1, 2);
    private BorderLayout borderLayout = new BorderLayout();
    private GridLayout gridLayoutPrawy = new GridLayout(3, 1);

    public KlasaOdliczacz() {
        setTitle("odliczacz !!!");
        setLocation(pozycja_okna_x, pozycja_okna_y);
        setSize(szerokosc, wysokosc);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        add(jPanel);
        
        jTextArea.setText("");
        jTextArea.setLineWrap(true);
        
        jScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        
        jButtonStart.setText("start");
        jButtonStart.addActionListener(this);
        jButtonReset.setText("reset");
        jButtonStop.setText("stop");
        
        jPanel.setLayout(gridLayoutGlowny);
        jPanel.add(jPanelLewy);
        jPanel.add(jPanelPrawy);
        
        jPanelLewy.setLayout(borderLayout);
        jPanelLewy.add(jScrollPane, BorderLayout.CENTER);
        
        jPanelPrawy.setLayout(gridLayoutPrawy);
        jPanelPrawy.add(jButtonStart);
        jPanelPrawy.add(jButtonStop);
        jPanelPrawy.add(jButtonReset);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        
        Timer timer = new Timer(1000, actionListener);
        
        Object object = e.getSource();
        if(object == jButtonStart){
            timer.start();
            //jButtonStart.setEnabled(false);
            //jButtonStop.setEnabled(true);
        } if(object == jButtonStop){
            timer.stop();
            //jButtonStop.setEnabled(false);
            //jButtonStart.setEnabled(true);
        } if(object == jButtonReset){
            timer.restart();
        }
    }
    
    ActionListener actionListener = new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            for(int i = 0; i < 100; i++){
                jTextArea.append(Integer.toString(i) + "\n");
            }
        }
    };
    
}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaswingodliczacz;

import javax.swing.SwingUtilities;

/**
 *
 * @author marcin
 */
public class JavaSwingOdliczacz {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                KlasaOdliczacz klasaOdliczacz = new KlasaOdliczacz();
            }
        });
    }
}


0

A czemu tworzysz Timer w metodzie actionPerformed? Utwórz go w konstruktorze, a w actionPerformed tylko startu, zatrzymuj i wznawiaj.

0

przerobiłem kod ale timer jest chyba kiepskim pomysłem chociaż nie wykluczone że źle tego używam ;), muszę ten kod przerobić żeby użyć swingworkera.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaswingodliczacz;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

/**
 *
 * @author marcin
 */
public class KlasaOdliczacz extends JFrame implements ActionListener{
    
    private int wysokosc = 150;
    private int szerokosc = 300;
    private int pozycja_okna_x = 500;
    private int pozycja_okna_y = 400;
    private JPanel jPanel = new JPanel();
    private JPanel jPanelLewy = new JPanel();
    private JPanel jPanelPrawy = new JPanel();
    private JButton jButtonStart = new JButton();
    private JButton jButtonStop = new JButton();
    private JButton jButtonReset = new JButton();
    private JTextArea jTextArea = new JTextArea();
    private JScrollPane jScrollPane = new JScrollPane(jTextArea); 
    private GridLayout gridLayoutGlowny = new GridLayout(1, 2);
    private BorderLayout borderLayout = new BorderLayout();
    private GridLayout gridLayoutPrawy = new GridLayout(3, 1);
    private Timer timer;

    public KlasaOdliczacz() {
        setTitle("odliczacz !!!");
        setLocation(pozycja_okna_x, pozycja_okna_y);
        setSize(szerokosc, wysokosc);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        add(jPanel);
        
        timer = new Timer(1000, actionListener);
        
        jTextArea.setText("");
        jTextArea.setLineWrap(true);
        
        jScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        
        jButtonStart.setText("start");
        jButtonStart.addActionListener(this);
        jButtonReset.setText("reset");
        jButtonStop.setText("stop");
        
        jPanel.setLayout(gridLayoutGlowny);
        jPanel.add(jPanelLewy);
        jPanel.add(jPanelPrawy);
        
        jPanelLewy.setLayout(borderLayout);
        jPanelLewy.add(jScrollPane, BorderLayout.CENTER);
        
        jPanelPrawy.setLayout(gridLayoutPrawy);
        jPanelPrawy.add(jButtonStart);
        jPanelPrawy.add(jButtonStop);
        jPanelPrawy.add(jButtonReset);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        
        
        
        Object object = e.getSource();
        if(object == jButtonStart){
            timer.start();
            //jButtonStart.setEnabled(false);
            //jButtonStop.setEnabled(true);
        } if(object == jButtonStop){
            timer.stop();
            //jButtonStop.setEnabled(false);
            //jButtonStart.setEnabled(true);
        } if(object == jButtonReset){
            timer.restart();
        }
    }
    
    ActionListener actionListener = new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            for(int i = 0; i < 100; i++){
                jTextArea.append(Integer.toString(i) + "\n");
            }
        }
    };
    
}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaswingodliczacz;

import javax.swing.SwingUtilities;

/**
 *
 * @author marcin
 */
public class JavaSwingOdliczacz {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                KlasaOdliczacz klasaOdliczacz = new KlasaOdliczacz();
            }
        });
    }
}

 
0

Obecny Timer co sekundę dopisuje 100 liczb (od 0 do 99). Powiedz co chcesz uzyskać, co sekundę jest dopisywana jedna liczba?

0

chce uzyskać aby co sekundę była dopisywana jedna liczba, a nie jak teraz co sekundę 100 liczb

0
private Timer timer;
private int i = 0; //nowe pole w klasie
...
ActionListener actionListener = new ActionListener() { 
        @Override
        public void actionPerformed(ActionEvent e) {
            i++;
            jTextArea.append(i + "\n");
        }
    };

Brakuje ponadto instrukcji

jButtonStop.addActionListener(this);
jButtonReset.addActionListener(this);

Czym ma się różnić start Timera od resetu? Zgaduję, że powinno być tak

if(object == jButtonReset){
     i = 0;
     timer.start();
}
//lub tak
if(object == jButtonReset){
     i = 0;
     jTextArea.setText("");
     timer.start();
}
0

przepraszam nie pomyślałem żeby użyć inkrementacji wywoływanej co sekundę zamiast pętli for, najtrudniej znaleźć te najprostsze sposoby.

0

wstawiam przerobiony kod, program na tą chwilę działa tak jak chce ;)

 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaswingodliczacz;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

/**
 *
 * @author marcin
 */
public class KlasaOdliczacz extends JFrame implements ActionListener{
    
    private int wysokosc = 150;
    private int szerokosc = 300;
    private int pozycja_okna_x = 500;
    private int pozycja_okna_y = 400;
    private JPanel jPanel = new JPanel();
    private JPanel jPanelLewy = new JPanel();
    private JPanel jPanelPrawy = new JPanel();
    private JButton jButtonStart = new JButton();
    private JButton jButtonStop = new JButton();
    private JButton jButtonReset = new JButton();
    private JTextArea jTextArea = new JTextArea();
    private JScrollPane jScrollPane = new JScrollPane(jTextArea); 
    private GridLayout gridLayoutGlowny = new GridLayout(1, 2);
    private BorderLayout borderLayout = new BorderLayout();
    private GridLayout gridLayoutPrawy = new GridLayout(3, 1);
    private Timer timer;
    private int i = 0;

    public KlasaOdliczacz() {
        setTitle("odliczacz !!!");
        setLocation(pozycja_okna_x, pozycja_okna_y);
        setSize(szerokosc, wysokosc);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        add(jPanel);
        
        timer = new Timer(1000, actionListener);
        
        jTextArea.setText("");
        jTextArea.setLineWrap(true);
        
        jScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        
        jButtonStart.setText("start");
        jButtonStart.addActionListener(this);
        jButtonReset.setText("reset");
        jButtonReset.addActionListener(this);
        jButtonStop.setText("stop");
        jButtonStop.addActionListener(this);
        
        jPanel.setLayout(gridLayoutGlowny);
        jPanel.add(jPanelLewy);
        jPanel.add(jPanelPrawy);
        
        jPanelLewy.setLayout(borderLayout);
        jPanelLewy.add(jScrollPane, BorderLayout.CENTER);
        
        jPanelPrawy.setLayout(gridLayoutPrawy);
        jPanelPrawy.add(jButtonStart);
        jPanelPrawy.add(jButtonStop);
        jPanelPrawy.add(jButtonReset);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object object = e.getSource();
        if(object == jButtonStart){
            timer.start();
            jButtonStart.setEnabled(false);
            jButtonStop.setEnabled(true);
        } if(object == jButtonStop){
            timer.stop();
            jButtonStop.setEnabled(false);
            jButtonStart.setEnabled(true);
        } if(object == jButtonReset){
            i = 0;
            jTextArea.setText("");
            timer.start();
            jButtonStop.setEnabled(true);
            jButtonStart.setEnabled(false);
        }
    }
    
    ActionListener actionListener = new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            i++;
            jTextArea.append(i + "\n");
        }
    };
    
}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaswingodliczacz;

import javax.swing.SwingUtilities;

/**
 *
 * @author marcin
 */
public class JavaSwingOdliczacz {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                KlasaOdliczacz klasaOdliczacz = new KlasaOdliczacz();
            }
        });
    }
}

0

chociaż chciałbym użyć fora z metodą sleep i będę szukał rozwiązania tego sposobu

0

Nie chciało mi się analizować poprzednich postów, ale po tym, co tu napisałeś:

  1. tworzysz nowy wątek
  2. w tym wątku dajesz for'a i sleep oraz aktualizujesz/wypisujesz te liczby

I to wszystko...

1 użytkowników online, w tym zalogowanych: 0, gości: 1