For my school project i made a game but i didn't add imports, i have been told i need to have imports and it needs to be in a normal swing format or i will not pass
I was wondering if anyone can help me to change my code to swing format please. any advice will be appreciated
my code is
public class TextTwist extends javax.swing.JFrame implements java.awt.event.ActionListener { // hard code, should be picked from a Problem class private String[] letters = {"F","O","C","I","E","F"}; // hard code, should be picked from a Problem class private String[] solutions = {"ICE","OFF","FOE","FOCI","OFFICE"}; // required designer variables private javax.swing.JButton[] letterButtons; private javax.swing.JButton enterButton; private javax.swing.JButton clearButton; private javax.swing.JLabel bufferLabel; private javax.swing.JLabel[] solutionLabels; public TextTwist() { // needed for the Windows GUI initializeComponent(); } // these are the GUI components private void initializeComponent() { this.letterButtons = new javax.swing.JButton[6]; for (int i=0;i<this.letterButtons.length;i++) { this.letterButtons[i] = new javax.swing.JButton(); } this.solutionLabels = new javax.swing.JLabel[solutions.length]; for (int i=0;i<this.solutions.length;i++) { this.solutionLabels[i] = new javax.swing.JLabel(); } this.setLayout(null); javax.swing.JButton button = null; for (int i=0;i<letterButtons.length;i++) { button = this.letterButtons[i]; button.setBounds(300+50*i, 100, 45, 45); button.setActionCommand("letterButton"+i); button.setText(letters[i]); button.setFocusable(false); button.addActionListener(this); } // button = enterButton = new javax.swing.JButton(); button.setBounds(300, 200, 80, 25); button.setActionCommand("enterButton"); button.setText("Enter"); button.setFocusable(false); button.addActionListener(this); button = clearButton = new javax.swing.JButton(); button.setBounds(390, 200, 80, 25); button.setActionCommand("clearButton"); button.setText("Clear"); button.setFocusable(false); button.addActionListener(this); // // bufferLabel // javax.swing.JLabel label = null; label = this.bufferLabel = new javax.swing.JLabel(); label.setBounds(300,50,200,30); for (int i=0;i<solutionLabels.length;i++) { label = this.solutionLabels[i]; int row=i%10; int column=i/10; label.setBounds(50+column*50, 50+row*40, 200,30); for (int letterCount=0;letterCount<solutions[i].length();letterCount++) { label.setText( label.getText() + "-" ); } } // basic Window Frame // this.setBounds(0,0,640,480); for (int i=0;i<letterButtons.length;i++) { this.add(this.letterButtons[i]); } this.add(this.enterButton); this.add(this.clearButton); this.add(this.bufferLabel); for (int i=0;i<solutionLabels.length;i++) { this.add(this.solutionLabels[i]); } this.setBackground(java.awt.Color.CYAN); this.setTitle("Macteki Text Twist"); this.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE); this.setVisible(true); } public void actionPerformed(java.awt.event.ActionEvent ev) { String command = ev.getActionCommand(); if (command.equals("clearButton")) { clearButton_click(clearButton, ev); } else if (command.equals("enterButton")) { enterButton_click(enterButton, ev); } else if (command.startsWith("letterButton")) { int i = Integer.parseInt(command.substring(command.length()-1,command.length())); letterButton_click(letterButtons[i], ev); } } private void letterButton_click(Object sender, java.awt.event.ActionEvent ev) { javax.swing.JButton button = (javax.swing.JButton) sender; String letter = button.getText(); button.setEnabled(false); bufferLabel.setText(bufferLabel.getText() + letter); } private void clearButton_click(Object sender, java.awt.event.ActionEvent ev) { clearBuffer(); } private void clearBuffer() { for (int i=0;i<letterButtons.length;i++) { letterButtons[i].setEnabled(true); } bufferLabel.setText(""); } private void enterButton_click(Object sender, java.awt.event.ActionEvent ev) { for (int i=0;i<solutions.length;i++) { if (bufferLabel.getText().equals(solutions[i])) { solutionLabels[i].setText(solutions[i]); clearBuffer(); } } } public static void main(String[] args) throws Exception { new TextTwist(); } } // class TextTwist
i have tried the following please can you tell me if this is correct?
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; public class TextTwist extends JFrame implements ActionListener{ // hard code, should be picked from a Problem class String[] letters = {"F","O","C","I","E","F"}; // hard code, should be picked from a Problem class String[] solutions = {"ICE","OFF","FOE","FOCI","OFFICE"}; // required designer variables JButton[] letterButtons; JButton enterButton; JButton clearButton; JLabel bufferLabel; JLabel[] solutionLabels; public TextTwist() { // needed for the Windows GUI initializeComponent(); } // these are the GUI components private void initializeComponent() { this.letterButtons = new JButton[6]; for (int i=0;i<this.letterButtons.length;i++) { this.letterButtons[i] = new JButton(); } this.solutionLabels = new JLabel[solutions.length]; for (int i=0;i<this.solutions.length;i++) { this.solutionLabels[i] = new JLabel(); } this.setLayout(null); JButton button = null; for (int i=0;i<letterButtons.length;i++) { button = this.letterButtons[i]; button.setBounds(300+50*i, 100, 45, 45); button.setActionCommand("letterButton"+i); button.setText(letters[i]); button.setFocusable(false); button.addActionListener(this); } // button = enterButton = new javax.swing.JButton(); button.setBounds(300, 200, 80, 25); button.setActionCommand("enterButton"); button.setText("Enter"); button.setFocusable(false); button.addActionListener(this); button = clearButton = new javax.swing.JButton(); button.setBounds(390, 200, 80, 25); button.setActionCommand("clearButton"); button.setText("Clear"); button.setFocusable(false); button.addActionListener(this); // // bufferLabel // javax.swing.JLabel label = null; label = this.bufferLabel = new javax.swing.JLabel(); label.setBounds(300,50,200,30); for (int i=0;i<solutionLabels.length;i++) { label = this.solutionLabels[i]; int row=i%10; int column=i/10; label.setBounds(50+column*50, 50+row*40, 200,30); for (int letterCount=0;letterCount<solutions[i].length();letterCount++) { label.setText( label.getText() + "-" ); } } // basic Window Frame // this.setBounds(0,0,640,480); for (int i=0;i<letterButtons.length;i++) { this.add(this.letterButtons[i]); } this.add(this.enterButton); this.add(this.clearButton); this.add(this.bufferLabel); for (int i=0;i<solutionLabels.length;i++) { this.add(this.solutionLabels[i]); } this.setBackground(java.awt.Color.CYAN); this.setTitle("Macteki Text Twist"); this.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE); this.setVisible(true); } public void actionPerformed(ActionEvent ev) { String command = ev.getActionCommand(); if (command.equals("clearButton")) { clearButton_click(clearButton, ev); } else if (command.equals("enterButton")) { enterButton_click(enterButton, ev); } else if (command.startsWith("letterButton")) { int i = Integer.parseInt(command.substring(command.length()-1,command.length())); letterButton_click(letterButtons[i], ev); } } private void letterButton_click(Object sender, ActionEvent ev) { javax.swing.JButton button = (javax.swing.JButton) sender; String letter = button.getText(); button.setEnabled(false); bufferLabel.setText(bufferLabel.getText() + letter); } private void clearButton_click(Object sender, ActionEvent ev) { clearBuffer(); } private void clearBuffer() { for (int i=0;i<letterButtons.length;i++) { letterButtons[i].setEnabled(true); } bufferLabel.setText(""); } private void enterButton_click(Object sender, ActionEvent ev) { for (int i=0;i<solutions.length;i++) { if (bufferLabel.getText().equals(solutions[i])) { solutionLabels[i].setText(solutions[i]); clearBuffer(); } } } public static void main(String[] args) throws Exception { new TextTwist(); } } // class TextTwist
i need some help with: After all the soulotions have been found how do i get the program to move on to the next one String?
--- Update ---
please help me