HW10 Solution1

HW10 Solution1 - } setLayout(new GridLayout(items,...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
Sheet1 Page 1 import javax.swing.* import java.awt.GridLayout public class Ratings extends JFrame { // Constructor public Ratings() { String rowsString = JOptionPane.showInputDialog("How many items?") int items = Integer.parseInt(rowsString) String[] itemNames = new String[items] int[] ratings = new int[items] int maxRating = 5 for (int i = 0 i < items i++) { itemNames[i] = JOptionPane.showInputDialog("Enter an item.") String ratingString = JOptionPane.showInputDialog("Enter a rating (1 - 5).") ratings[i] = Integer.parseInt(ratingString)
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: } setLayout(new GridLayout(items, maxRating + 1, 5, 5)) for(int i = 0 i &lt; items i++) { add(new JLabel(itemNames[i])) for(int j = 0 j &lt; maxRating j++) { if (j &lt; ratings[i]) add(new JLabel(new ImageIcon(&quot;star.jpg&quot;))) else add(new JLabel()) } } } // Main method public static void main(String args) { Ratings f = new Ratings() f.setTitle(&quot;Ratings&quot;) f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) f.setSize(400, 200) f.setVisible(true) } }...
View Full Document

Ask a homework question - tutors are online