1. Engineering
  2. Computer Science
  3. assignment is to create a jbutton on java using swing...

Question: assignment is to create a jbutton on java using swing...

Question details

Assignment is to create a jbutton on java using swing. Every time it is clicked it switches between characters + * = %. The button also rotates 90 degrees everytime clicked as well. Posted below is my code so far, but I can only get it to switch from + to * but doesn't continue to switch after that. If possible try to edit/add-to my pre-existing code posted below, but if it's completely wrong then feel free to write up a whole new program.

import javax.swing.*;

import java.awt.event.*;

//New button for each rotated symbol?

// while loop to run through str arr

public class Button

{

public void createButton()

{

JFrame f = new JFrame("Button Example");

String str = "+";

JButton b = new JButton(str);

b.setBounds(50,50,50,50);

f.add(b);

f.setSize(400,400);

f.setLayout(null);

f.setVisible(true);

b.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

String str[] = {"+","*","=","%"};

   b.setText(str[1]);

}

} );

}

}

public class ButtonTest

{

public static void main(String[] args)

{

Button b = new Button();

b.createButton();

//b.setText()

}

}

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution