Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: change to swing format

  1. #1
    Member
    Join Date
    Apr 2014
    Posts
    82
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default change to swing format

    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
    Last edited by newtolearningjava; April 20th, 2014 at 08:26 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: change to swing format

    get the program to move on to the next one String?
    Can you explain what that means?
    What happens when the program executes?
    What should the user do?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jan 2014
    Posts
    209
    My Mood
    Depressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: change to swing format

    The game is called text twist, you have find fords from a given set of letters. For example, you are given the following 6 letters :
    ["F","O","C","I","E","F"]

    And your tasks is to find out all possible words by selecting subsets of the letters. The solution would be :

    [ "ICE","OFF","FOE","FOCI","OFFICE" ]

    I would like there to be another set of letters and anther souloution, and one is picked at random

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: change to swing format

    I would like there to be another set of letters and anther souloution, and one is picked at
    random
    Put the data (the two arrays) into a class, create many instances of that class and put the instances in a list where they can be chosen randomly.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Need to format this!!
    By Dtank123456 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 7th, 2014, 09:17 AM
  2. how to change DB design for a change in java
    By harry7ster in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 7th, 2013, 12:57 PM
  3. Read Date from Excel and change its Format
    By vector_ever in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 22nd, 2013, 05:13 AM
  4. How do you change tab stop index in Swing?
    By IndraG in forum AWT / Java Swing
    Replies: 3
    Last Post: May 7th, 2013, 06:05 AM
  5. How to format?
    By maximus20895 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 15th, 2010, 01:07 PM