I have a working JFrame GUI with my JPanel all setup. I am trying to combine two different codes that I've got setup and working. The first code was a text converter toUpperCase in a JPanel, and the second is a Prime Factor (not prime numbers) code. I've been trying to get the JPanel to give an output of Prime Factors for any number that a user inputs. Here is what I have....
public class Prime extends JPanel { private JLabel formattedText; public static void main(String[] args) { JFrame frame = new JFrame(); frame.getContentPane().add(new Prime()); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.setVisible(true); } public Prime(){ setLayout(new BoxLayout(this,BoxLayout.Y_AXIS)); JPanel panel = new JPanel(); panel.setPreferredSize(new Dimension(640,100)); JLabel label = new JLabel("Enter a number to check for it's prime number(s)."); JTextField field = new JTextField("0"); field.addActionListener(new FieldListener()); add(label); add(field); add(panel); panel = new JPanel(); panel.setPreferredSize(new Dimension(640,380)); formattedText = new JLabel(); panel.add(formattedText); add(panel); } private class FieldListener implements ActionListener { public void actionPerformed(ActionEvent event) { JTextField field = (JTextField)event.getSource(); formattedText.setText(field.getText().toUpperCase()); // I know this is wrong... I can't figure out what to change here to get it to pull the code below. } } public class PrimeFactors { } }
and here is the Prime Factor code
public class Temp { static int primeCheck = 1; public static void main(String[] args) { System.out.println("Enter a number whose Prime factors are desired: "); Scanner numS = new Scanner(System.in); int numPriFac = 0; if (numS.hasNextInt()) { numPriFac = numS.nextInt(); } System.out.println("All the Prime Factors of the entered number are:"); for (int tap = 1; tap <= numPriFac; tap++) { if (numPriFac % tap == 0) { for (int primeTest = 2; primeTest < tap; primeTest++) { if (tap % primeTest == 0) { primeCheck = 1; break; } else { primeCheck = 0; } } if (primeCheck == 0 || tap == 2) { System.out.print(tap + " "); } } } } }
That last PrimeFactors code in the bottom is just something left over from when I was trying to get it working on my own. Thanks so much for any help!!!