Hello
I 'm new to java and I have trouble adding scrollbars to the textArea of my program. The program makes a kind of magical calculation and displays the result - a lot of numbers - in a textarea: resultField. The program runs well, except for the scrollbars I want to add.
The class for the window of the program is:
//scrollbars.java package h03; import javax.swing.JFrame; public class scrollbars extends JFrame { public scrollbars() { JFrame window = new JFrame(); window.setSize(300, 300); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setTitle("scrollbars"); window.setLocation(100, 50); window.add(new scrollbarsPanel()); window.setVisible(true); } public static void main(String[] args) { new scrollbars(); } }
and for the panel the code is:
//scrollbarsPane.java package h03; import javax.swing.*; import java.awt.event.*; public class scrollbarsPanel extends JPanel implements ActionListener { private JTextField inputField; private JTextArea resultField; private JButton calcButton; //private JScrollPane scrollArea; private int input; public scrollbarsPanel() { calcButton = new JButton("start calculation"); inputField = new JTextField("000", 3); resultField = new JTextArea(9, 24); JScrollPane scrollArea = new JScrollPane(resultField); resultField.setLineWrap(true); resultField.setWrapStyleWord(true); this.add(scrollArea); add(new JLabel ("number")); add(inputField); inputField.addActionListener(this); add(calcButton); calcButton.addActionListener(this); add(resultField); } public void showResult(int input) { input = Integer.parseInt(inputField.getText()); resultField.setText(input + " "); while (input != 1) { if (input % 2 == 0) { resultField.append((input / 2) + " "); input = input/2; System.out.println(input); } else { resultField.append((input * 3 + 1) + " "); input = input * 3 + 1; System.out.println(input); } } } public void actionPerformed(ActionEvent e) { showResult(input); } }
I really can 't figure out what I am doing wrong. Can someone help me please?
Thanks a lot!
fagus