JPanel - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

JPanel

Description:

typically used as top level container in content pane. also for ... setIcon ( Icon icon ) M. Winter. COSC 3P40 Advanced Object-Oriented Programming ... – PowerPoint PPT presentation

Number of Views:241
Avg rating:3.0/5.0
Slides: 21
Provided by: michael742
Category:
Tags: icon | jpanel

less

Transcript and Presenter's Notes

Title: JPanel


1
JPanel
  • contained container
  • lightweight
  • typically used as top level container in content
    pane
  • also for controlling layout
  • default layout manager FlowLayoutManager
  • constructor
  • JPanel()

2
JButton
  • button widget
  • button with label
  • press generates ActionEvent
  • constructor
  • JButton ( String label )
  • methods
  • addActionListener ( ActionListener l )
  • setActionCommand ( String actionCommand )
  • setEnabled ( boolean b )
  • setText ( String text )

3
JMenuBar
  • associated with a JFrame
  • frame.setJMenuBar ( JMenuBar mBar )
  • constructor
  • JMenuBar ( )
  • methods
  • add ( JMenu menu )

4
JMenu
  • menu in a JMenuBar
  • constructor
  • JMenu ( String label )
  • methods
  • add ( JMenuItem item )
  • addSeparator ( )
  • insert ( JMenuItem item, int pos)
  • insertSeparator ( int pos )
  • remove ( JMenuItem item )
  • remove ( int pos )
  • removeAll ( )

5
JMenuItem
  • selectable item in a JMenu
  • constructors
  • JMenuItem ( String text )
  • methods
  • addActionListener ( ActionListener listener )
  • setActionCommand ( String command )
  • setEnabled ( boolean b )

6
Example
  • public MyFrame()
  • JMenuBar myBar new JMenuBar()
  • JMenu fileMenu new JMenu("File")
  • JMenuItem startItem new JMenuItem("Start
    Game")
  • startItem.setEnabled(false)
  • fileMenu.add(startItem)
  • JMenuItem quitItem new JMenuItem("Quit")
  • quitItem.addActionListener(new AbstractAction()
  • public void actionPerformed(ActionEvent e)
  • System.exit(0)
  • )
  • fileMenu.add(quitItem)
  • myBar.add(fileMenu)
  • setJMenuBar(myBar)
  • setSize(500,300)
  • setLocation(200,200)
  • setVisible(true)

7
JRadioButton
  • buttons that can be grouped
  • ButtonGroup
  • constructor
  • ButtonGroup ( )
  • methods
  • add ( AbstractButton b )
  • ButtonModel getSelection ( )
  • constructors
  • JRadioButton ( String label )
  • JRadioButton ( String label, boolean state )
  • methods
  • addActionListener ( Actionlistener listener )
  • setActionCommand ( String actionCommand )
  • setEnabled ( boolean b )
  • boolean isSelected ( )
  • setSelected ( boolean state )

8
JDialog
  • dialog window
  • has a parent (owner)
  • modal or non-modal
  • content pane
  • constructors
  • JDialog ( )
  • JDialog ( Dialog owner )
  • JDialog ( Frame owner )
  • JDialog ( Dialog owner, boolean modal )
  • JDialog ( Frame owner, boolean modal )
  • methods
  • Container getContentPane ( )
  • dispose ( ) // from Window
  • pack ( ) // from Window
  • setResizable ( boolean b ) // from Dialog
  • setTitle ( String title ) // from Dialog
  • setDefaultCloseOperation ( int operation )

9
Example
  • public MyDialog(JFrame parent)
  • super(parent,true)
  • setSize(220,160)
  • setTitle("Select number of players")
  • ButtonGroup group new ButtonGroup()
  • JRadioButton one new JRadioButton("One",true)
  • JRadioButton two new JRadioButton("Two",false
    )
  • JRadioButton three new JRadioButton("Three",fal
    se)
  • JRadioButton four new JRadioButton("Four",false
    )
  • group.add(one) group.add(two)
    group.add(three) group.add(four)
  • JPanel selectPanel new JPanel()
  • selectPanel.setLayout(new GridLayout(4,1))
  • selectPanel.add(one) selectPanel.add(two)
  • selectPanel.add(three) selectPanel.add(four)
  • getContentPane().add(selectPanel,BorderLayout.NOR
    TH)

10
Example (contd)
  • JPanel buttonPanel new JPanel()
  • JButton ok new JButton("OK")
  • ok.addActionListener(new AbstractAction()
  • public void actionPerformed(ActionEvent e)
  • setVisible(false)
  • )
  • buttonPanel.add(ok,BorderLayout.CENTER)
  • getContentPane().add(buttonPanel,BorderLayout.SOU
    TH)
  • setVisible(false)
  • In MyFrame private MyDialog dialog new
    MyDialog(this)
  • ...
  • startItem.addActionListener(new AbstractAction()
  • public void actionPerformed(ActionEvent e)
  • dialog.setVisible(true)
  • )

11
JScrollBar
  • constructors
  • JScrollBar (int orientation, int value,
    int extent, int min, int max )
  • methods
  • addAdjustmentListener( AdjustmentListener
    listener )
  • getOrientation( )
  • getValue( )
  • setValue( int value )

12
JLabel
  • text label
  • constructors
  • JLabel ( String text )
  • JLabel ( String text, int align )
  • JLabel ( Icon icon )
  • JLabel ( String text, Icon icon, int align )
  • alignment
  • JLabel.LEFT
  • JLabel.RIGHT
  • JLabel.CENTER
  • methods
  • setText ( String text )
  • setIcon ( Icon icon )

13
Example
  • private JLabel widthLabel new JLabel("
    Width 5000")
  • private JLabel heightLabel new JLabel("
    Height 5000")
  • private JScrollBar widthScrollbar
  • new JScrollBar(JScrollBar.HORIZONTAL,5000,0,0,320
    00)
  • private JScrollBar heightScrollbar
  • new JScrollBar(JScrollBar.HORIZONTAL,5000,0,0,320
    00)
  • ?
  • widthScrollbar.addAdjustmentListener(new
    AdjustmentListener()
  • public void adjustmentValueChanged(AdjustmentEven
    t e)
  • widthLabel.setText(" Width "
    widthScrollbar.getValue())
  • )
  • heightScrollbar.addAdjustmentListener(new
    AdjustmentListener()
  • public void adjustmentValueChanged(AdjustmentEven
    t e)
  • heightLabel.setText(" Height "
    heightScrollbar.getValue())
  • )

14
JOptionPane
  • easy pop-up of standard dialogs
  • modal
  • methods (static)
  • int showMessageDialog ( Component parent, Object
    msg)
  • int showMessageDialog ( Component parent, Object
    msg, String title, int
    type )
  • int showOptionDialog ( )
  • int showConfirmDialog ( )
  • String showInputDialog ( )

15
JFileChooser
  • file selection dialog
  • can specify subdirectory and file filter
  • constructor
  • JFileChooser ( )
  • JFileChooser ( File currentDirectory )
  • methods
  • File getSelectedFile
  • setCurrentDirectory ( File dir )
  • setFileFilter ( FileFilter filter )
  • showOpenDialog ( Component Parent )
  • showSaveDialog ( Component Parent )

16
JTextComponent
  • text entry areas
  • new lines (\n)
  • JTextField
  • editable single-line text
  • JTextField ( int cols )
  • JTextArea
  • editable multi-line text
  • JTextArea ( int rows, int cols )
  • methods
  • setText ( String text )
  • String getText ( )
  • setEditable ( boolean b )
  • JPasswordField
  • setEchoChar ( char echo )
  • char getPassword ( )

17
JCheckbox
  • a checkbox
  • generates ActionEvent
  • constructors
  • JCheckbox ( String label )
  • JCheckbox ( String label, boolean state )
  • methods
  • addActionListener ( Actoinlistener listener )
  • setActionCommand ( String actionCommand )
  • setEnabled ( boolean b )
  • boolean isSelected ( )
  • setSelected ( boolean state )

18
JList
  • list selector
  • generates ListSelectionEvent
  • constructor
  • JList ( Object items )
  • JList ( ListModel dataModel )
  • methods
  • setVisibleRowCount ( int rows )
  • setFixedCellWidth ( int width)
  • setSelectionMode ( int mode )
  • JList.SINGLE_SELECTION, JList.SINGLE_INTERVAL_SELE
    CTION, JList.MULTIPLE_INTERVAL_SELECTION
  • addListSelectionListener ( ListSelectionListener
    l )
  • Object getSelectedValues ( )
  • Object getSelectedValue ( )

19
ListSelectionListener
  • javax.swing.event
  • method
  • valueChanged ( ListSelectionEvent e )
  • ListSelectionEvent
  • boolean getValueIsAdjusting ( )
  • Object getSource ( )

20
JScrollPane
  • scrolling for other components
  • place component in JScrollPane and then add
    scroll pane to container
  • constructor
  • JScrollPane ( Component c )
Write a Comment
User Comments (0)
About PowerShow.com