Hi,
Here is my problem: I wanted to make a GUI for a code that solves Hailstone Sequence (The Hailstone Sequence).
It is an easy one, so I could solve it but I was printing to the console.
I prepared the window using Windowpro. Here is my code (There are two classes because Hail class is basicly the copy of my console app):
import java.awt.BorderLayout; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.JButton; import javax.swing.JTextPane; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class mainWindow extends JFrame { private JPanel contentPane; private JTextField Number; private JTextField Count; public JTextPane textPane; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { mainWindow frame = new mainWindow(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public mainWindow() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JLabel lblNumber = new JLabel("Number"); lblNumber.setBounds(10, 11, 46, 14); contentPane.add(lblNumber); Number = new JTextField(); Number.setBounds(66, 8, 86, 20); contentPane.add(Number); Number.setColumns(10); JLabel lblCount = new JLabel("Count"); lblCount.setBounds(10, 44, 46, 14); contentPane.add(lblCount); Count = new JTextField(); Count.setBounds(66, 41, 86, 20); contentPane.add(Count); Count.setColumns(10); JButton btnStart = new JButton("START"); btnStart.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String s1 = Number.getText(); int num = Integer.parseInt(s1); String s2 = Count.getText(); int count = Integer.parseInt(s2); Hail h = new Hail(); h.test(num, count); } }); btnStart.setBounds(162, 7, 89, 23); contentPane.add(btnStart); textPane = new JTextPane(); textPane.setEditable(false); textPane.setBounds(10, 69, 414, 181); contentPane.add(textPane); } public void setYazi(String s){ textPane.setText(s); repaint(); } }
This is the hail class, which does the job:
import java.util.Scanner; public class Hail { public void test(int c, int d){ int sayac=0; System.out.println("Number "+c); System.out.println("How many repetitions?"); System.out.println("Will do."); System.out.println("Started."); while(c!=1){ if(c%2==0){ c = c/2; System.out.println("Number/2: "+c); }else{ c = (3*c)+1; System.out.println("Number*3+1:"+c); } sayac++; if(sayac==d){ break; } } mainWindow m = new mainWindow(); if(c==1){ m.setYazi("Suitable. Count: "+sayac+"); System.out.println("Suitable. Count: "+sayac+"); }else{ System.out.println("Not suitable or not enough repetition"); m.setYazi("Not suitable or not enough repetition"); } } }
I am gonna be honest. If I return a String from Hail class, I can update the textPane. But I want to print out all of the process to the textPane. That is why I cannot use it.
System.out.println codes are there to test it out. What I want is, to pass everything I am writing to console to textPane.
What am I doing wrong?
Possible reason might be creating an instance of mainWindow from inside of Hail, but how else could I do that?