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();
}
});
}
}
[/code]
JTextArea text = new JTextArea ("");
JScrollPane scroll = new JScrollPane (text,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
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();
}
});
}
}[/code]
Dodałeś słuchacza do jednego JButtona. Imho nie powinno się obsługiwać akcji if else if, bo tworzysz w ten sposób mało ogólny kod. Przyjmij zasadę listener na akcję. Pozwoli to również skorzystać z tego samego kodu słuchacza w wielu miejscach (zasada DRY). Model wciąż nie jest rozgraniczony z widokiem. Mało modyfikowalne.