User:Janabsalon/sandbox

02 import javax.swing.JOptionPane; 03 04 public class CoinToss { 05 06   public static void main (String[] args) { 07   int toss; 08   int counter;      // Number of times to flip coin 09   int heads, tails; // Number of heads and tails tossed 10   String input; 11 12   counter = 0; 13   heads = 0; 14   tails = 0; 15 16 17   input = JOptionPane.showInputDialog("Enter amount of times you want the coin to be tossed"); 18   toss = Integer.parseInt(input); 19 20 21   Random coinToss = new Random; 22   int random = coinToss.nextInt(1); 23 24 25   while (counter < toss){ 26 27       if (random > 0.5) { 28           heads = heads + 1; 29           System.out.println("You got heads"); 30       } 31        else { 32           tails = tails + 1; 33           System.out.println("You got tails"); 34       } 35            counter++; 36   } 37    JOptionPane.showMessageDialog(null, "For your tosses you got \n" + "---\n" + "Heads: " + heads + "\nTails: " + tails); 38 39 40   } // end main 41 42 } // end class CoinToss ANoTHER article Java JOptionPane Hierarchy

javax.swing Class JOptionPane java.lang.Object java.awt.Component java.awt.Container javax.swing.JComponent javax.swing.JOptionPane All Implemented Interfaces: Accessible, ImageObserver, MenuContainer, Serializable

JOptionPane Constructor

JOptionPane Creates a JOptionPane with a test message.

JOptionPane(Object message) Creates a instance of JOptionPane to display a message using the plain-message message type and the default options delivered by the UI.

JOptionPane(Object message, int messageType) Creates an instance of JOptionPane to display a message with the specified message type and the default options,

JOptionPane(Object message, int messageType, int optionType) Creates an instance of JOptionPane to display a message with the specified message type and options.

JOptionPane(Object message, int messageType, int optionType, Icon icon) Creates an instance of JOptionPane to display a message with the specified message type, options, and icon.

JOptionPane(Object message, int messageType, int optionType, Icon icon, Object[] options) Creates an instance of JOptionPane to display a message with the specified message type, icon, and options.

JOptionPane(Object message, int messageType, int optionType, Icon icon, Object[] options, Object initialValue) Creates an instance of JOptionPane to display a message with the specified message type, icon, and options, with the initially-selected option spec ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// JPanel is Swing’s version of the AWT class Panel and uses the same default layout, FlowLayout. JPanel is descended directly from JComponent. JFrame is Swing’s version of Frame and is descended directly from that class. The components added to the frame are referred to as its contents; these are managed by the contentPane. To add a component to a JFrame, we must use its contentPane instead. JInternalFrame is confined to a visible area of a container it is placed in. It can be iconified, maximized and layered. JWindow is Swing’s version of Window and is descended directly from that class. Like Window, it uses BorderLayout by default. JDialog is Swing’s version of Dialog and is descended directly from that class. Like Dialog, it uses BorderLayout by default. Like JFrame and JWindow, JDialog contains a rootPane hierarchy including a contentPane, and it allows layered and glass panes. All dialogs are modal, which means the current thread is blocked until user interaction with it has been completed. JDialog class is intended as the basis for creating custom dialogs; however, some of the most common dialogs are provided through static methods in the class JOptionPane. JLabel, descended from JComponent, is used to create text labels. The abstract class AbstractButton extends class JComponent and provides a foundation for a family of button classes, including JButton.

JTextField allows editing of a single line of text. New features include the ability to justify the text left, right, or center, and to set the text’s font. JPasswordField (a direct subclass of JTextField) you can suppress the display of input. Each character entered can be replaced by an echo character. This allows confidential input for passwords, for example. By default, the echo character is the asterisk, *. JTextArea allows editing of multiple lines of text. JTextArea can be used in conjunction with class JScrollPane to achieve scrolling. The underlying JScrollPane can be forced to always or never have either the vertical or horizontal scrollbar; JButton is a component the user clicks to trigger a specific action. JRadioButton is similar to JCheckbox, except for the default icon for each class. A set of radio buttons can be associated as a group in which only one button at a time can be selected. JCheckBox is not a member of a checkbox group. A checkbox can be selected and deselected, and it also displays its current state. JComboBox is like a drop down box. You can click a drop-down arrow and select an option from a list. For example, when the component has focus, pressing a key that corresponds to the first character in some entry’s name selects that entry. A vertical scrollbar is used for longer lists. JList provides a scrollable set of items from which one or more may be selected. JList can be populated from an Array or Vector. JList does not support scrolling directly, instead, the list must be associated with a scrollpane. The view port used by the scroll pane can also have a user-defined border. JList actions are handled using ListSelectionListener. JTabbedPane contains a tab that can have a tool tip and a mnemonic, and it can display both text and an image. JToolbar contains a number of components whose type is usually some kind of button which can also include separators to group related components within the toolbar. FlowLayout when used arranges swing components from left to right until there’s no more space available. Then it begins a new row below it and moves from left to right again. Each component in a FlowLayout gets as much space as it needs and no more. BorderLayout places swing components in the North, South, East, West and center of a container. You can add horizontal and vertical gaps between the areas. GridLayout is a layout manager that lays out a container’s components in a rectangular grid. The container is divided into equal-sized rectangles, and one component is placed in each rectangle. GridBagLayout is a layout manager that lays out a container’s components in a grid of cells with each component occupying one or more cells, called its display area. The display area aligns components vertically and horizontally, without requiring that the components be of the same size. JMenubar can contain several JMenu’s. Each of the JMenu’s can contain a series of JMenuItem ‘s that you can select. Swing provides support for pull-down and popup menus. Scrollable JPopupMenu is a scrollable popup menu that can be used whenever we have so many items in a popup menu that exceeds the screen visible height. Java Swing Projects

Java Swing Calculator developed using Java Swing. It is a basic four-function calculator java program source code. Java Swing Address Book demonstrates how to create a simple free address book program using java swing and jdbc. Also you will learn to use the following swing components like Jbuttons, JFrames, JTextFields and Layout Manager (GridBagLayout). ////////////////////////////////////////// //////////////////////////////// ////////////////////// ////////////////// import java.util.Scanner; 02 import java.util.Random; 03 04 public class Coin_Head_Tail { 05   public static void main(String[] args) { 06       Scanner input =new Scanner(System.in); 07       Random generation=new Random; 08       int coin=generation.nextInt(2); 09       System.out.println("Welcome to the coin guess game."); 10 11       System.out.println("What is your guess?  0 for Head or 1 for Tail: "); 12 13 14 15       String ch= input.nextLine; 16 17 18       if(coin==0 && ch.equalsIgnoreCase("0")) 19       System.out.println("Congratulation, it is head"); 20       else if(coin==1 && ch.equalsIgnoreCase("1")) 21       System.out.println("Congratulation, it is tail"); 22       else 23       System.out.println("Sorry, try again"); 24       } 25 }/////sample String one = "1"; 2 int uno = Integer.parseInt(one);