Ramar Comeros GUI Compilation MS Word1
Ramar Comeros GUI Compilation MS Word1
public void actionPerformed(ActionEvent event) { { private JTextField textField1; String string = ""; private JTextField textField2; if (event.getSource()== textField1) private JTextField textField3; private JPasswordField passwordField; public FieldTextFrame() { super("Testing JTextField and JTextPassword"); setLayout(new FlowLayout()); textField1 = new JTextField(10); add(textField1); textField2 = new JTextField("ENTER TEXT HERE"); add(textField2); textField3 = new JTextField("UNEDITABLE TEXT FIELD",21); textField3.setEditable(false); add(textField3); } passwordField = new JPasswordField("HIDDEN TEXT"); } } string = String.format("textField1:%s", event.getActionCommand()); else if (event.getSource()== textField2) string = String.format("textField2:%s", event.getActionCommand()); else if (event.getSource()== textField3) string = String.format("textField3:%s", event.getActionCommand()); else if (event.getSource()== passwordField) string = String.format("passwordField:%s", event.getActionCommand()); new String (passwordField.getPassword()); JOptionPane.showMessageDialog(null, string);
package jtextfieldsandjpasswordfield; import javax.swing.JFrame; public class FieldTextTest { public static void main(String[] args) { FieldTextFrame textFieldTextFrame = new FieldTextFrame(); textFieldTextFrame.setDefaultCloseOperation(JFrame.EXI T_ON_CLOSE); textFieldTextFrame.setSize(325,120); textFieldTextFrame.setVisible(true); } }
plainButton = new JButton("Plain Button"); add(plainButton); Icon vice1 = new ImageIcon(getClass().getResource("vice1.jpg")); Icon vice2 = new ImageIcon(getClass().getResource("vice2.jpg")); fancyButton = new JButton("Fancy Button",vice1); fancyButton.setRolloverIcon(vice2); add(fancyButton); Buttonhandler handler = new Buttonhandler(); fancyButton.addActionListener(handler); plainButton.addActionListener(handler); } private class Buttonhandler implements ActionListener { public void actionPerformed(ActionEvent event) { JOptionPane.showMessageDialog(FrameButtton.this, String.format("You pressed:%S",event.getActionCommand())); } }
package commandbuttonsandactionevents; public class TestButton extends JFrame { import java.awt.FlowLayout; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JFrame; import javax.swing.JButton; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JOptionPane; public class FrameButtton extends JFrame { private JButton plainButton; private JButton fancyButton; public FrameButtton() { super("Test Button"); setLayout(new FlowLayout());
new
if (event.getSource()==italicJCheckBox) valItalic = italicJCheckBox.isSelected()? Font.ITALIC : Font.PLAIN; textField.setFont(new Font ("Safari", valBold + valItalic ,14)); } } } package jcheckboxbuttonsanditemevents; import javax.swing.JFrame; public class TestCheckBox extends JFrame { public static void main(String[] args) { FrameCheckBox FrameCheckBox(); checkBox = new
public class FrameCheckBox extends JFrame { private JTextField textField; private JCheckBox boldCheckBOx; private JCheckBox italicJCheckBox; public FrameCheckBox() { super("JCheckBox Test"); setLayout(new FlowLayout()); textField = new JTextField("Watch the font style change",20); textField.setFont(new Font("Safari",Font.PLAIN,14)); add(textField); boldCheckBOx = new JCheckBox("Bold"); italicJCheckBox = new JCheckBox("Italic"); add(boldCheckBOx); add(italicJCheckBox); CheckBoxHandler handler = new CheckBoxHandler(); boldCheckBOx.addItemListener(handler); italicJCheckBox.addItemListener(handler); } private class CheckBoxHandler implements ItemListener { private int valBold = Font.PLAIN; private int valItalic = Font.PLAIN;
5.JRadio Buttons
package jradiobuttons; import java.awt.FlowLayout; import java.awt.Font; import java.awt.event.ItemListener; import java.awt.event.ItemEvent; import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.JRadioButton; import javax.swing.ButtonGroup; public class ButtonRadio extends JFrame { private JTextField textField; private Font plainFont; private Font boldFont; private Font italicFont; private Font boldItalicFont; private JRadioButton plainJRadioButton; private JRadioButton boldJRadioButton; private JRadioButton italicJRadioButton; private JRadioButton boldItalicJRadioButton; private ButtonGroup radioGroup; public ButtonRadio() { super("RadioButton Test"); setLayout(new FlowLayout()); textField = new JTextField("WATCH THIS FONT STYLE CHANGE",25); add(textField); plainJRadioButton = new JRadioButton("Plain",true); boldJRadioButton = new JRadioButton("Bold",false); italicJRadioButton = new JRadioButton("Italic",false); boldItalicJRadioButton = new JRadioButton("Bold/Italic",false); add(plainJRadioButton); add(boldJRadioButton); add(italicJRadioButton); add(boldItalicJRadioButton); radioGroup = new ButtonGroup(); radioGroup.add(plainJRadioButton); radioGroup.add(boldJRadioButton); radioGroup.add(italicJRadioButton); radioGroup.add(boldItalicJRadioButton); plainFont = new Font("Safari",Font.PLAIN,14);
boldFont = new Font("Safari",Font.BOLD,14); italicFont = new Font("Safari",Font.ITALIC,14); boldItalicFont = new Font("Safari",Font.BOLD+Font.ITALIC,14); textField.setFont(plainFont); plainJRadioButton.addItemListener(new RadioButtonHandler(plainFont)); boldJRadioButton.addItemListener(new RadioButtonHandler(boldFont)); italicJRadioButton.addItemListener(new RadioButtonHandler(italicFont)); boldItalicJRadioButton.addItemListener(new RadioButtonHandler(boldItalicFont)); } private class RadioButtonHandler implements ItemListener { private Font font; public RadioButtonHandler(Font f) { font=f; } public void itemStateChanged(ItemEvent event) { textField.setFont(font); } } } package jradiobuttons; import javax.swing.JFrame; public class TestRadio { public static void main(String[] args) { ButtonRadio radioButtonFrame ButtonRadio();
new
6.JComboBox
package jcombobox; import java.awt.FlowLayout; import java.awt.event.ItemListener; import java.awt.event.ItemEvent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JComboBox; import javax.swing.Icon; import javax.swing.ImageIcon; public class ComboFrame extends JFrame { private JComboBox imageComboBox; private JLabel label; private String name[]={"pic1.jpg","pic2.jpg","pic3.jpg"}; private Icon icons[] = { new ImageIcon(getClass().getResource(names[0])), new ImageIcon(getClass().getResource(names[1])), new ImageIcon(getClass().getResource(names[2])) }; public ComboFrame() { super("Testing ComboBox"); setLayout(new FlowLayout()); imageComboBox = new JComboBox(names); imageComboBox.setMaximumRowCount(3); imageComboBox.addItemListener(new ItemListener() {
package jcombobox; import javax.swing.JFrame; public class TestComboBox { public static void main(String[] args) { ComboFrame comboBoxFrame = ComboFrame();
new
public void itemStateChanged(ItemEvent event) { if (event.getStateChange()== package jlistthatdisplaydifferentcolors; ItemEvent.SELECTED)label.setIcon(icons[imageComboB import java.awt.FlowLayout; import java.awt.Color; ox.getSelectedIndex()]); import javax.swing.JFrame; import javax.swing.JList; } }); import javax.swing.JScrollPane; add(imageComboBox); import javax.swing.event.ListSelectionListener; label = new JLabel(icons[0]); import javax.swing.event.ListSelectionEvent; add(label); import javax.swing.ListSelectionModel; } } public class FrameList extends JFrame {
private JList colorJList; private final String colorNames[]={"Black","Blue","Cyan","Dark Gray","Gray", "Green" ,"Light Gray","Magenta","Orange","Pick","Red","White","Yellow"} ; private final Color colors [] = {Color.BLACK,Color.BLUE,Color.CYAN,Color.DARK_GRAY, Color.GRAY,Color.GREEN,Color.LIGHT_GRAY ,Color.MAGENTA,Color.ORANGE,Color.PINK,Color.RED,Colo r.WHITE,Color.YELLOW}; public FrameList() { super("List Test"); setLayout(new FlowLayout()); colorJList = new JList (colorNames); colorJList.setVisibleRowCount(5); colorJList.setSelectionMode(ListSelectionModel.SINGLE_SE LECTION); add(new JScrollPane(colorJList)); colorJList.addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent event) { getContentPane().setBackground(colors[colorJList.getSelec tedIndex()]); } }); } }
public class MultipleSelectionFrame extends JFrame { private JList colorJList; private JList copyJList; private JButton copyJButton; private final String colorNames[] = { "Black", "Blue", package jlistthatdisplaydifferentcolors; "Cyan", import javax.swing.JFrame; "Dark Gray", "Gray", "Green", "Light Gray", "Magenta", public class ListTest { "Orange", public static void main(String[] args) "Pink", "Red", "White", "Yellow" }; { public MultipleSelectionFrame() FrameList listframe = new FrameList(); { listframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS super( "Multiple Selection Lists" ); E); setLayout( new FlowLayout() ); listframe.setSize(350, 150); listframe.setVisible(true); colorJList = new JList( colorNames ); } colorJList.setVisibleRowCount( 5 ); } colorJList.setSelectionMode(
ListSelectionModel.MULTIPLE_INTERVAL_SELECTION ); add( new JScrollPane( colorJList ) ); copyJButton = new JButton( "Copy >>>" ); copyJButton.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { copyJList.setListData( colorJList.getSelectedValues() ); } } ); add( copyJButton ); copyJList = new JList(); copyJList.setVisibleRowCount( 5 ); copyJList.setFixedCellWidth( 100 ); copyJList.setFixedCellHeight( 15 ); copyJList.setSelectionMode( ListSelectionModel.SINGLE_INTERVAL_SELECTION ); add( new JScrollPane( copyJList ) ); } } import javax.swing.JFrame; public class MultipleSelectionTest { public static void main( String args[] ) { MultipleSelectionFrame multipleSelectionFrame = new MultipleSelectionFrame(); multipleSelectionFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); multipleSelectionFrame.setSize( 350, 140 ); multipleSelectionFrame.setVisible( true ); } }
public class MouseTracer extends JFrame { private JPanel mousePanel; private JLabel statusBar; public MouseTracer() { super("Demostrating Mouse Event"); mousePanel = new JPanel(); mousePanel.setBackground(Color.WHITE); add(mousePanel,BorderLayout.CENTER); statusBar = new JLabel("Mouse outside JPanel"); add(statusBar, BorderLayout.SOUTH); MouseHandler handler = new MouseHandler(); mousePanel.addMouseListener(handler); mousePanel.addMouseMotionListener(handler); } private class MouseHandler MouseListener,MouseMotionListener { implements
public void mouseClicked(MouseEvent event) { statusBar.setText(String.format("Clicked [%d,%d]", event.getX(),event.getY())); } public void mousePressed(MouseEvent event) { statusBar.setText(String.format("Pressed [%d,%d]", event.getX(),event.getY())); } public void mouseReleased(MouseEvent event) { statusBar.setText(String.format("Released [%d,%d]", event.getX(),event.getY())); mousePanel.setBackground(Color.BLACK); }
at
package c9mouseeventhandling; import javax.swing.JFrame; public class TrackerMouse { public static void main(String[] args) { MouseTracer mouseTrackerFrame MouseTracer();
at
new
at
public void mouseEntered(MouseEvent event) { statusBar.setText(String.format("Enter at [%d,%d]", event.getX(),event.getY())); mousePanel.setBackground(Color.RED); } public void mouseExited(MouseEvent event) { statusBar.setText(String.format("Exited at [%d,%d]", event.getX(),event.getY())); mousePanel.setBackground(Color.WHITE); } public void mouseDragged(MouseEvent event) { statusBar.setText(String.format("Dragged [%d,%d]", event.getX(),event.getY())); mousePanel.setBackground(Color.GREEN); } public void mouseMoved(MouseEvent event) { statusBar.setText(String.format("Moved [%d,%d]", event.getX(),event.getY())); mousePanel.setBackground(Color.ORANGE); } } }
at import java.awt.BorderLayout; import java.awt.Graphics; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.JFrame; at import javax.swing.JLabel; public class MouseDetailsFrame extends JFrame { private String details; private JLabel statusBar; public MouseDetailsFrame() { super( "Mouse clicks and buttons" ); statusBar = new JLabel( "Click the mouse" );
add( statusBar, BorderLayout.SOUTH ); addMouseListener( new MouseClickHandler() ); } private class MouseClickHandler extends MouseAdapter { public void mouseClicked( MouseEvent event ) { int xPos = event.getX(); int yPos = event.getY(); details = String.format( "Clicked %d time(s)", event.getClickCount() ); if ( event.isMetaDown() ) details += " with right mouse button"; else if ( event.isAltDown() ) details += " with center mouse button"; else details += " with left mouse button"; statusBar.setText( details ); } } } import javax.swing.JFrame; public class MouseDetails { public static void main( String args[] ) { MouseDetailsFrame mouseDetailsFrame = MouseDetailsFrame(); mouseDetailsFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); mouseDetailsFrame.setSize( 400, 150 ); mouseDetailsFrame.setVisible( true ); } }
new
public static void main(String[] args) { { line1 = String.format( "Key released: %s", JFrame application = new JFrame( "A simple paint event.getKeyText( event.getKeyCode() ) ); program" ); setLines2and3( event ); paintpanel paintPanel = new paintpanel(); } application.add( paintPanel, BorderLayout.CENTER ); public void keyTyped( KeyEvent event ) application.add( new JLabel( "Drag the mouse to draw" { ), line1 = String.format( "Key typed: %s", BorderLayout.SOUTH ); event.getKeyChar() ); application.setDefaultCloseOperation( setLines2and3( event ); JFrame.EXIT_ON_CLOSE ); } application.setSize( 400, 200 ); private void setLines2and3( KeyEvent event ) application.setVisible( true ); { } line2 = String.format( "This key is %san action key", } ( event.isActionKey() ? "" : "not " ) );
String temp = event.getKeyModifiersText( event.getModifiers() ); line3 = String.format( "Modifier keys pressed: %s", ( temp.equals( "" ) ? "none" : temp ) ); textArea.setText( String.format( "%s\n%s\n%s\n", line1, line2, line3 ) ); } } import javax.swing.JFrame; public class KeyDemo { public static void main( String args[] ) { KeyDemoFrame keyDemoFrame = KeyDemoFrame(); keyDemoFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); keyDemoFrame.setSize( 350, 100 ); keyDemoFrame.setVisible( true ); } }
new
13.FlowLayout
package flowlayoutframe; import java.awt.FlowLayout; import java.awt.Container; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JFrame; import javax.swing.JButton; public class flowlayoutframe extends JFrame { private JButton leftJButton; private JButton centerJButton; private JButton rightJButton; private FlowLayout layout; private Container container; public flowlayoutframe() { super( "FlowLayout Demo" ); layout = new FlowLayout(); container = getContentPane(); setLayout( layout ); leftJButton = new JButton( "Left" ); add( leftJButton ); leftJButton.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { layout.setAlignment( FlowLayout.LEFT ); layout.layoutContainer( container ); } } ); centerJButton = new JButton( "Center" ); add( centerJButton ); centerJButton.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { layout.setAlignment( FlowLayout.CENTER ); }
layout.layoutContainer( container ); } ); rightJButton = new JButton( "Right" ); add( rightJButton ); rightJButton.addActionListener( new ActionListenerImpl() ); } private class ActionListenerImpl implements ActionListener { public ActionListenerImpl() { } public void actionPeformed(ActionEvent event) { layout.setAlignment(FlowLayout.RIGHT); layout.layoutContainer(container); } public void actionPerformed(ActionEvent e) { layout.setAlignment(FlowLayout.RIGHT); layout.layoutContainer(container); } } } package flowlayoutframe; import javax.swing.JFrame; public class FlowLayoutDemo { public static void main(String[] args) { flowlayoutframe flowLayoutFrame = flowlayoutframe(); flowLayoutFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); flowLayoutFrame.setSize( 300, 75 ); flowLayoutFrame.setVisible( true ); } }
new
14.BorderLayout
import java.awt.BorderLayout; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JFrame; import javax.swing.JButton; }
layout.layoutContainer( getContentPane() ); }
} public class BorderLayoutFrame extends JFrame implements ActionListener import javax.swing.JFrame; { private JButton buttons[]; public class BorderLayoutDemo private final String names[] = { "Hide North", "Hide { South", public static void main( String args[] ) "Hide East", "Hide West", "Hide Center" }; { private BorderLayout layout; BorderLayoutFrame borderLayoutFrame = BorderLayoutFrame(); borderLayoutFrame.setDefaultCloseOperation( public BorderLayoutFrame() JFrame.EXIT_ON_CLOSE ); { borderLayoutFrame.setSize( 300, 200 ); super( "BorderLayout Demo" ); borderLayoutFrame.setVisible( true ); } layout = new BorderLayout( 5, 5 ); } setLayout( layout ); buttons = new JButton[ names.length ];
new
for ( int count = 0; count < names.length; count++ ) { buttons[ count ] = new JButton( names[ count ] ); buttons[ count ].addActionListener( this ); } add( buttons[ 0 ], BorderLayout.NORTH ); add( buttons[ 1 ], BorderLayout.SOUTH ); add( buttons[ 2 ], BorderLayout.EAST ); add( buttons[ 3 ], BorderLayout.WEST ); add( buttons[ 4 ], BorderLayout.CENTER ); }
15.GridLayout
import java.awt.GridLayout; import java.awt.Container; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JFrame; import javax.swing.JButton;
public void actionPerformed( ActionEvent event ) { for ( JButton button : buttons ) { if ( event.getSource() == button )
public class GridLayoutFrame extends JFrame implements ActionListener { private JButton buttons[]; private final String names[] = { "one", "two", "three", "four", "five", "six" }; private boolean toggle = true; private Container container; private GridLayout gridLayout1; private GridLayout gridLayout2;
import javax.swing.JFrame; public class GridLayoutDemo { public static void main( String args[] ) { GridLayoutFrame gridLayoutFrame = GridLayoutFrame(); gridLayoutFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); gridLayoutFrame.setSize( 300, 200 ); gridLayoutFrame.setVisible( true ); } }
new
public GridLayoutFrame() { super( "GridLayout Demo" ); gridLayout1 = new GridLayout( 2, 3, 5, 5 ); gridLayout2 = new GridLayout( 3, 2 ); container = getContentPane(); setLayout( gridLayout1 ); buttons = new JButton[ names.length ]; for ( int count = 0; count < names.length; count++ ) { buttons[ count ] = new JButton( names[ count ] ); buttons[ count ].addActionListener( this ); add( buttons[ count ] ); } } // public void actionPerformed( ActionEvent event ) { if ( toggle ) container.setLayout( gridLayout2 ); else container.setLayout( gridLayout1 ); toggle = !toggle; container.validate(); } }
16.JPanel
import java.awt.GridLayout; import java.awt.BorderLayout; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JButton; public class PanelFrame extends JFrame { private JPanel buttonJPanel; private JButton buttons[]; public PanelFrame() { super( "Panel Demo" ); buttons = new JButton[ 5 ]; buttonJPanel = new JPanel(); buttonJPanel.setLayout( new GridLayout( 1, buttons.length ) ); for ( int count = 0; count < buttons.length; count++ ) { buttons[ count ] = new JButton( "Button " + ( count + 1 ) ); buttonJPanel.add( buttons[ count ] ); }
String demo = "This is a demo string to\n" + "illustrate copying text\nfrom one textarea to \n" + "another textarea using an\nexternal event\n"; textArea1 = new JTextArea( demo, 10, 15 ); box.add( new JScrollPane( textArea1 ) ); copyJButton = new JButton( "Copy >>>" ); box.add( copyJButton ); copyJButton.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { textArea2.setText( textArea1.getSelectedText() ); } } ); textArea2 = new JTextArea( 10, 15 ); textArea2.setEditable( false ); box.add( new JScrollPane( textArea2 ) ); add( box ); } }
import javax.swing.JFrame; public class PanelDemo extends JFrame { public static void main( String args[] ) { PanelFrame panelFrame = new PanelFrame(); panelFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); panelFrame.setSize( 450, 200 ); panelFrame.setVisible( true ); } }
17.JTextArea
import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.Box; import javax.swing.JFrame; import javax.swing.JTextArea; import javax.swing.JButton; import javax.swing.JScrollPane; public class TextAreaFrame extends JFrame { private JTextArea textArea1; private JTextArea textArea2; private JButton copyJButton; public TextAreaFrame() { super( "TextArea Demo" ); Box box = Box.createHorizontalBox();
import javax.swing.JFrame; public class TextAreaDemo { public static void main( String args[] ) { TextAreaFrame textAreaFrame = new TextAreaFrame(); textAreaFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); textAreaFrame.setSize( 425, 200 ); textAreaFrame.setVisible( true ); } }