0% found this document useful (0 votes)
53 views

Ramar Comeros GUI Compilation MS Word1

The document describes several Swing components in Java: 1) JLabels with text and icons 2) JTextFields and JPasswordFields 3) Command buttons and action events 4) JCheckbox buttons and item events 5) JRadio buttons 6) JComboBox

Uploaded by

Bryan Abesta
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Ramar Comeros GUI Compilation MS Word1

The document describes several Swing components in Java: 1) JLabels with text and icons 2) JTextFields and JPasswordFields 3) Command buttons and action events 4) JCheckbox buttons and item events 5) JRadio buttons 6) JComboBox

Uploaded by

Bryan Abesta
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

1.

JLabels with text and icons


label3.setVerticalTextPosition(SwingConstants.BOTTOM); package jlabels_with_text_and_icons label3.setToolTipText("This is lable3"); import java.awt.FlowLayout; add(label3); import javax.swing.JFrame; } import javax.swing.JLabel; } import javax.swing.SwingConstants; import javax.swing.Icon; import javax.swing.ImageIcon; public class FrameLabel extends JFrame package jlabels_with_text_and_icons; { private JLabel label1; import javax.swing.JFrame; private JLabel label2; private JLabel label3; public class LabelTest { public FrameLabel() public static void main(String[] args) { { FrameLabel labelFrame = new FrameLabel(); super("Testing JLabel"); setLayout(new FlowLayout()); label1 = new JLabel("Label with text"); label1.setToolTipText("THIS IS LABEL1"); add(label1); Icon image1 =new ImageIcon(getClass().getResource("image1.jpg")); label2 = new JLabel ("Label with text and icon,",image1,SwingConstants.LEFT); label2.setToolTipText("This is label2"); add(label2); label3 = new JLabel(); label3.setText("Label with icon ang text at the buttom"); label3.setIcon(image1); label3.setHorizontalTextPosition(SwingConstants.CENTE R); } } labelFrame.setDefaultCloseOperation(JFrame.EXIT_ON_C LOSE); labelFrame.setSize(640,250); labelFrame.setVisible(true);

2. JTextFields and JPasswordField


package jtextfieldsandjpasswordfield; import java.awt.FlowLayout; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.JPasswordField; import javax.swing.JOptionPane; { public class FieldTextFrame extends JFrame }

add(passwordField); TextFieldHandler handler = new TextFieldHandler(); textField1.addActionListener(handler); textField2.addActionListener(handler); textField3.addActionListener(handler); passwordField.addActionListener(handler);

private class TextFieldHandler implements ActionListener

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())); } }

3.Command buttons and action events

} package commandbuttonsandactionevents; import javax.swing.JFrame;

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());

public static void main(String[] args) { FrameButtton buttonFrameButtton FrameButtton();

new

buttonFrameButtton.setDefaultCloseOperation(JFra me.EXIT_ON_CLOSE); buttonFrameButtton.setSize(400,220); buttonFrameButtton.setVisible(true); } }

4.JCheckbox buttons and item events


package jcheckboxbuttonsanditemevents; 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.JCheckBox;

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;

checkBox.setDefaultCloseOperation(JFrame.EXIT _ON_CLOSE); checkBox.setSize(270,100); checkBox.setVisible(true); } }

public void itemStateChanged(ItemEvent event) { if (event.getSource()==boldCheckBOx) valBold = boldCheckBOx.isSelected()? Font.BOLD : 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

radioButtonFrame.setDefaultCloseOperation(JFram e.EXIT_ON_CLOSE); radioButtonFrame.setSize(300,100); radioButtonFrame.setVisible(true); } }

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

comboBoxFrame.setDefaultCloseOperation(JFrame .EXIT_ON_CLOSE); comboBoxFrame.setSize(250,180); comboBoxFrame.setVisible(true); } }

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 {

7.JList that display different colors

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()]); } }); } }

8.JList that allows multiple selections


import java.awt.FlowLayout; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JFrame; import javax.swing.JList; import javax.swing.JButton; import javax.swing.JScrollPane; import javax.swing.ListSelectionModel;

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 ); } }

9.Mouse Event Handling


package c9mouseeventhandling; import java.awt.Color; import java.awt.BorderLayout; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.awt.event.MouseEvent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel;

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

mouseTrackerFrame.setDefaultCloseOperation(JFr ame.EXIT_ON_CLOSE); mouseTrackerFrame.setSize(300,100); mouseTrackerFrame.setVisible(true); } }

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); } } }

10. Left, Center and Right mousebutton clicks

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 ); } }

11. Adapter classes used to implement event handler


package adapterclassesusedtoimplementeventhandler; import java.awt.Point; import java.awt.Graphics; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionAdapter; import javax.swing.JPanel; public class paintpanel extends JPanel { private int pointCount = 0; private Point point[]= new Point [10000]; public paintpanel () { addMouseMotionListener( new MouseMotionAdapter() { public void mouseDragged(MouseEvent event ) { if (pointCount < point.length) { point[pointCount]=event.getPoint(); pointCount++; repaint(); } } } ); } public void paintComponent(Graphics g ) { super.paintComponent(g); for (int i = 0; i <pointCount; i++) g.fillOval(point[i].x,point [i].y, 4, 4); } } package adapterclassesusedtoimplementeventhandler; import java.awt.BorderLayout; import javax.swing.JFrame; import javax.swing.JLabel; public class paint {

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 " ) );

12.Key Event Handling


import java.awt.Color; import java.awt.event.KeyListener; import java.awt.event.KeyEvent; import javax.swing.JFrame; import javax.swing.JTextArea; public class KeyDemoFrame extends JFrame implements KeyListener { private String line1 = ""; private String line2 = ""; private String line3 = ""; private JTextArea textArea; public KeyDemoFrame() { super( "Demonstrating Keystroke Events" ); textArea = new JTextArea( 10, 15 ); textArea.setText( "Press any key on the keyboard..." ); textArea.setEnabled( false ); textArea.setDisabledTextColor( Color.BLACK ); add( textArea ); addKeyListener( this ); } public void keyPressed( KeyEvent event ) { line1 = String.format( "Key pressed: %s", event.getKeyText( event.getKeyCode() ) ); setLines2and3( event ); } public void keyReleased( KeyEvent event )

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; }

button.setVisible( false ); else button.setVisible( true );

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 ] ); }

add( buttonJPanel, BorderLayout.SOUTH ); } }

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 ); } }

You might also like