Hi I did a Simpletron program in GUI interface, but when I want to print the instructions in the window, it will not be as I want to. I used JGrasp to run the program and it just prints the instruction after each and not the way I wrote it in the program. For instance I want the program to be like:

                  *** Welcome to Simpletron! ***
                  ***Please enter your program one instruction***  
                  ***(or data word) at a time into the input***   
                  ***text field. I will display the location***   
                  ***number and a question mark (?). You then***  
                  ***type the word for that location. Press the***

But it's written in the program like:

*** Welcome to Simpletron! Please enter your program one instruction(or data word) at a time into the inputtext field. I will display the locationnumber and a question mark (?). You thentype the word for that location. Press theDone button to stop entering your program***

Here is my code:

 // Simpletron a computer simulator in Java GUI
 import java.awt.*;
 import java.awt.event.*;
 import javax.swing.*;

public class Simulator extends JApplet implements ActionListener {

 static final int READ = 10, WRITE = 11, LOAD = 20, STORE = 21,
 ADD = 30, SUBTRACT = 31, MULTIPLY = 32, DIVIDE = 33, BRANCH = 40,
 BRANCH_NEG = 41, BRANCH_ZERO = 42, HALT = 43;

 JTextField input, prompt, accum, counter, operandField,
 operCode, register;
 JLabel accumLabel, counterLabel, operandLabel,
 operCodeLabel, registerLabel, instructionLabel;
 JButton doneButton, executeButton;
 boolean readFlag;
 int memory[], accumulator, instructionCounter, index,
 operand, operationCode, instructionRegister;

 public void init()
 {
 initializeRegisters();
 instructions();

 // create GUI components
 doneButton = new JButton( "Done" );
 doneButton.addActionListener( this );
 executeButton = new JButton( "Execute next instruction" );
 executeButton.setEnabled( false );
 executeButton.addActionListener( this );
 input = new JTextField( 5 );
 input.addActionListener( this );
 prompt = new JTextField( 15 );

 register = new JTextField( 4 );
 counter = new JTextField( 2 );
 operCode = new JTextField( 2 );
 operandField = new JTextField( 2 );
 accum = new JTextField( 4 );

 prompt.setEditable( false );
 prompt.setText( "0" + index + " ? " );
 register.setEditable( false );
 counter.setEditable( false );
 operCode.setEditable( false );
 operandField.setEditable( false );

 accumLabel = new JLabel( "Accumulator:" );
 counterLabel = new JLabel( "InstructionCounter:" );
 registerLabel = new JLabel( "InstructionRegister:" );
 operCodeLabel = new JLabel( "OperationCode:" );
 operandLabel = new JLabel( "Operand:" );

 instructionLabel = new JLabel ( "*** Welcome to Simpletron! ***" +
                     "\n*** Please enter your program one instruction  ***" +
                     "\n*** (or data word) at a time into the input    ***" +
                     "\n*** text field. I will display the location    ***" +
                     "\n*** number and a question mark (?). You then   ***" +
                     "\n*** type the word for that location. Press the ***" +
                     "\n*** Done button to stop entering your program  ***" );
 // add components to applet
 Container container = getContentPane();
 container.setLayout( new FlowLayout() );
 container.add( accumLabel );
 container.add( accum );
 container.add( counterLabel );
 container.add( counter );
 container.add( registerLabel );
 container.add( register );
 container.add( operCodeLabel );
 container.add( operCode );
 container.add( operandLabel );
 container.add( operandField );
 container.add( prompt );
 container.add( input );
 container.add( doneButton );
 container.add( executeButton );
 container.add( instructionLabel );
 displayRegisters(); // place correct values in fields
 }

 // set values held by text fields
 public void displayRegisters()
 {
 operandField.setText( "" + operand );
 counter.setText( "" + instructionCounter );
 accum.setText( "" + accumulator );
 operCode.setText( "" + operationCode );
 register.setText( "" + instructionRegister );
 }

 // set all registers to the correct start value
 public void initializeRegisters()
 {
 memory = new int[ 100 ];
 accumulator = 0;
 instructionCounter = 0;
 instructionRegister = 0;
 operand = 0;
 operationCode = 0;

 index = 0;
 readFlag = false;

 for ( int k = 0; k < memory.length; k++ )
 memory[ k ] = 0;
 }

 // ensure value is within range
 public boolean validation( int value )
 {
 if ( value < 9999 && value > -9999 )
 return false;

 else
 return true;
 }

 // ensure that accumulator has not overflowed
 public boolean testOverflow()
 {
 if ( validation( accumulator ) ) {
 System.out.println( "*** Fatal error." +
 "Accumulator overflow. ***" );
 executeButton.setEnabled( false );
 return true;
 }

 return false;
 }

 // perform all simulator functions
 public void execute()
 {
  showStatus( "*** Executing ***" );
 prompt.setText( "" );

 instructionRegister = memory[ instructionCounter ];
 operationCode = instructionRegister / 100;
 operand = instructionRegister % 100;

 switch( operationCode ) {
 case READ:

 // actual value is assigned in actionPeformed
 readFlag = true;
 input.setText( "" );
 input.setEditable( true );
 prompt.setText( "Enter an integer:" );
 instructionCounter++;
 break;

 case WRITE:
 System.out.println( "Contents of " + operand +
 " is " + memory[ operand ] );
 instructionCounter++;
 break;

 case LOAD:
 accumulator = memory[ operand ];
 instructionCounter++;
 break;

 case STORE:
 memory[ operand ] = accumulator;
 instructionCounter++;
 break;

 case ADD:
 accumulator += memory[ operand ];

 if ( testOverflow() == true )
 return;

 instructionCounter++;
 break;

 case SUBTRACT:
 accumulator -= memory[ operand ];
 if ( testOverflow() == true )
 return;

 instructionCounter++;
 break;

 case MULTIPLY:
 accumulator *= memory[ operand ];

 if ( testOverflow() == true )
 return;

 instructionCounter++;
 break;

 case DIVIDE:

 if ( memory[ operand ] == 0 ) {
 System.out.println( "*** Fatal error." +
 "Attempt to divide by zero. ***" );

 executeButton.setEnabled( false );
 return;
 }

 accumulator /= memory[ operand ];
 instructionCounter++;
 break;

 case BRANCH:
 instructionCounter = operand;
 break;

 case BRANCH_NEG:

 if ( accumulator < 0 )
 instructionCounter = operand;

 else
 instructionCounter++;

 break;
 case BRANCH_ZERO:

 if ( accumulator == 0 )
 instructionCounter = operand;
 else

 instructionCounter++;

 break;

 case HALT:
 showStatus( "*** Simpletron execution terminated ***" );
 executeButton.setEnabled( false );
 dump();
 break;

 default:
 executeButton.setEnabled( false );
 System.out.println( "*** Fatal error. " +
 " Invalid operation code. ***" );
 }

 // update the register textfields
 displayRegisters();
 }

 // read in user input, test it, perform operations
 public void actionPerformed( ActionEvent e )
 {
 if ( e.getSource() == input && index < 100 ) {
 int temp = Integer.parseInt( input.getText() );

 // not a read instruction
 if ( !readFlag ) {

 // allow user to reenter values
 if ( validation( temp ) == false ) {
 memory[ index++ ] = temp;

 // clear text field
 input.setText( "" );

 if ( index < 10 )
 prompt.setText( "0" + index + " ? " );

 else
 prompt.setText( index + " ? " );
 }

 else
 input.setText( "" );

 showStatus( "*** Loading ***" );
 }

 // read instruction
 else {
 if ( validation( temp ) == false ) {
 memory[ operand ] = temp;
 input.setEditable( false );
 readFlag = false;
 executeButton.setEnabled( true );
 }

 else {
 input.setText( "" );
 showStatus( "Invalid input!" );
 executeButton.setEnabled( false );
 }
 }
 }

 else if ( e.getSource() == doneButton ) {
 input.setEditable( false );
 executeButton.setEnabled( true );
 doneButton.setEnabled( false );
 index = 0;
 execute(); // execute first instruction
 }

 else if ( e.getSource() == executeButton ) {
 index++;
 execute();
 }
 }

 // print out user instructions
 public void instructions()
 {
 System.out.println( "*** Welcome to Simpletron! ***\n" +
 "*** Please enter your program one instruction ***\n" +
 "*** ( or data word ) at a time into the input ***\n" +
 "*** text field. I will display the location ***\n" +
 "*** number and a question mark (?). You then ***\n" +
 "*** type the word for that location. Press the ***\n" +
 "*** Done button to stop entering your program ***\n" );
 }
 // create a string version of the number to output
 public String prepareNumber( int number )
 {
 int count = 0, factor = 1000;
 String output;

 // account for sign of number
 if ( number < 0 ) {
 number *= -1;
 output = "-";
 }

 else
 output = "+";
 // build String representation of number
 while ( factor >= 1 ) {
 output += number / factor;
 number %= factor;
 factor /= 10;
 }

 return output;
 }

 // output information in registers
 public void dump()
 {
 System.out.println( "REGISTERS:" );
 System.out.print( "accumulator " );
 System.out.println( prepareNumber( accumulator ) );
 System.out.print( "instructionCounter " );
 System.out.print( ( instructionCounter / 10 ) );
 System.out.println( ( instructionCounter % 10 ) );
 System.out.print( "instructionRegister " );
 System.out.println( prepareNumber( instructionRegister ) );
 System.out.print( "operationCode " );
 System.out.print( ( operationCode / 10 ) );
 System.out.println( ( operationCode % 10 ) );
 System.out.print( "operand " );
 System.out.print( ( operand / 10 ) );
 System.out.println( ( operand % 10 ) );
 System.out.println( "\nMEMORY:" );
 System.out.println( " 0 1 2 3" +
 " 4 5 6 7 8 9" );

 for ( int k = 0; k < 10; k++ ) {

 if ( k == 0 )
 System.out.print( " " + k + " " );
 else
 System.out.print( ( k * 10 ) + " " );

 for ( int i = 0; i < 10; i++ )
 System.out.print( prepareNumber(
 memory[ k * 10 + i ] ) + " " );

 System.out.println();
 }
 }

 } // end class Simulator
***Done button to stop entering your program***

It just won't show in aligned line. You can run the program to see how it runs. Please help me on this thank you.

JLabel does not display multi-line simple text.
You could use a JTextArea or somesuch, or you can use simple HTML formatting to display multi-line text in a JLabel

The JTextArea is something new to me, so do you know how I can use it in this program?

OK I used the HTML method as you told me:

 JFrame frame = new JFrame();
 frame.setLayout(new GridLayout());
 JLabel label = new JLabel("<html>*** Welcome to Simpletron!***<br>***Please  enter your program one instruction  ***<br>*** (or data word) at a time into the input    ***<br>*** text field. I will display the location    ***<br>*** number and a question mark (?). You then   ***<br>*** type the word for that location. Press the ***<br>*** Done button to stop entering your program  *** </html>");
 frame.add(label);
 frame.pack();
 frame.setVisible(true);

But when I run it, it pops up in a new windows. I want to know how can I put it in the same window that my program instruction are. Do you know about this?

Add that JLabel to your existing window
Or use that HTML in instructionLabel (line 55)

Thank you for responding, but I don't get how to add the JLabel to existing window! I tried to add instruction label to a new JLabel but nothing happens when I do that. Do you know how I can print the instructions in the main window?

Did you try to replace the code at line 55

 instructionLabel = new JLabel ( "*** Welcome to Simpletron! ***" +
                     "\n*** Please enter your program one instruction  ***" +
                     "\n*** (or data word) at a time into the input    ***" +
                     "\n*** text field. I will display the location    ***" +
                     "\n*** number and a question mark (?). You then   ***" +
                     "\n*** type the word for that location. Press the ***" +
                     "\n*** Done button to stop entering your program  ***" );

with the HTML version

 instructionLabel = new JLabel("<html>*** Welcome to Simpletron!***<br>***Please  enter your program one instruction  ***<br>*** (or data word) at a time into the input    ***<br>*** text field. I will display the location    ***<br>*** number and a question mark (?). You then   ***<br>*** type the word for that location. Press the ***<br>*** Done button to stop entering your program  *** </html>");

Yes, but nothing happens when I do that!!

It seems unlikely that literally "nothing happened" when you changed the contents of a JLabel in your window...

You are going to have to be clearer in your explanations - the info you are giving is incomplete and inconsistent.

You have some multi-line text to display. Where do you want to display it? When do you want to display it?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.