GUI Design
GUI Design
String msg; }
• https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html
void showFrame(){
GridBagLayout
GridBagLayout gridbag = new GridBagLayout(); import java.awt.*;
GridBagConstraints c = new GridBagConstraints();
setFont(new Font("SansSerif", Font.PLAIN, 14)); import java.awt.event.*;
setLayout(gridbag); public class Demo extends Frame {
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0; protected void makebutton(String name,
makebutton("Button1", gridbag, c);
GridBagLayout gridbag,
makebutton("Button2", gridbag, c);
makebutton("Button3", gridbag, c); GridBagConstraints c)
c.gridwidth = GridBagConstraints.REMAINDER; //end row
makebutton("Button4", gridbag, c); {
c.weightx = 0.0; //reset to the default Button button = new
makebutton("Button5", gridbag, c); //another row Button(name);
c.gridwidth = GridBagConstraints.RELATIVE; //next-to-last in
row gridbag.setConstraints(button, c);
makebutton("Button6", gridbag, c);
c.gridwidth = GridBagConstraints.REMAINDER; //end row add(button);
makebutton("Button7", gridbag, c); }
c.gridwidth = 1; //reset to the default
c.gridheight = 2;
c.weighty = 1.0;
makebutton("Button8", gridbag, c);
public static void main(String args[]) {
c.weighty = 0.0; //reset to the default Demo obj=new Demo();
c.gridwidth = GridBagConstraints.REMAINDER; //end row
c.gridheight = 1; //reset to the default obj.showFrame();
makebutton("Button9", gridbag, c);
makebutton("Button10", gridbag, c);
void showFrame(){
GridBagLayout
GridBagLayout gridbag = new GridBagLayout(); import java.awt.*;
GridBagConstraints c = new GridBagConstraints();
setFont(new Font("SansSerif", Font.PLAIN, 14)); import java.awt.event.*;
setLayout(gridbag); public class Demo extends Frame {
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0; protected void makebutton(String name,
makebutton("Button1", gridbag, c);
GridBagLayout gridbag,
makebutton("Button2", gridbag, c);
makebutton("Button3", gridbag, c); GridBagConstraints c)
c.gridwidth = GridBagConstraints.REMAINDER; //end row
makebutton("Button4", gridbag, c); {
c.weightx = 0.0; //reset to the default Button button = new
makebutton("Button5", gridbag, c); //another row Button(name);
c.gridwidth = GridBagConstraints.RELATIVE; //next-to-last in
row gridbag.setConstraints(button, c);
makebutton("Button6", gridbag, c);
c.gridwidth = GridBagConstraints.REMAINDER; //end row add(button);
makebutton("Button7", gridbag, c); }
c.gridwidth = 1; //reset to the default
c.gridheight = 2;
c.weighty = 1.0;
makebutton("Button8", gridbag, c);
public static void main(String args[]) {
c.weighty = 0.0; //reset to the default Demo obj=new Demo();
c.gridwidth = GridBagConstraints.REMAINDER; //end row
c.gridheight = 1; //reset to the default obj.showFrame();
makebutton("Button9", gridbag, c);
makebutton("Button10", gridbag, c);
void showFrame(){
GridBagLayout
GridBagLayout gridbag = new GridBagLayout(); import java.awt.*;
GridBagConstraints c = new GridBagConstraints();
setFont(new Font("SansSerif", Font.PLAIN, 14)); import java.awt.event.*;
setLayout(gridbag); public class Demo extends Frame {
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0; protected void makebutton(String name,
makebutton("Button1", gridbag, c);
GridBagLayout gridbag,
makebutton("Button2", gridbag, c);
makebutton("Button3", gridbag, c); GridBagConstraints c)
c.gridwidth = GridBagConstraints.REMAINDER; //end row
makebutton("Button4", gridbag, c); {
c.weightx = 0.0; //reset to the default Button button = new
makebutton("Button5", gridbag, c); //another row Button(name);
c.gridwidth = GridBagConstraints.RELATIVE; //next-to-last in
row gridbag.setConstraints(button, c);
makebutton("Button6", gridbag, c);
c.gridwidth = GridBagConstraints.REMAINDER; //end row add(button);
makebutton("Button7", gridbag, c); }
c.gridwidth = 1; //reset to the default
c.gridheight = 2;
c.weighty = 1.0;
makebutton("Button8", gridbag, c);
public static void main(String args[]) {
c.weighty = 0.0; //reset to the default Demo obj=new Demo();
c.gridwidth = GridBagConstraints.REMAINDER; //end row
c.gridheight = 1; //reset to the default obj.showFrame();
makebutton("Button9", gridbag, c);
makebutton("Button10", gridbag, c);
Dialog Container
• A Dialog is a top-level window with a title and a border that is
typically used to take some form of input from the user.
• The default layout for a dialog is BorderLayout.
• A dialog may have another window as its owner when it's
constructed.
• Dialog boxes may be modal or modeless.
• When a modal dialog box is active, all input is directed to it until it is
closed.
• This means that you cannot access other parts of your program until
you have closed the dialog box.
• When a modeless (the default) dialog box is active, input focus can be
directed to another window in your program.
• Thus, other parts of your program remain active and accessible
Dialog Container
• Dialog(Frame parentWindow, boolean mode)
• Dialog(Frame parentWindow, String title, boolean mode)
• Here, parentWindow is the owner of the dialog box. If mode is true,
the dialog box is modal.
• Otherwise, it is modeless. The title of the dialog box can be passed in
title. Generally, you will subclass Dialog, adding the functionality
required by your application.
• Notice that when the dialog box is closed, we need to call dispose( ).
This method is defined by Window, and it frees all system resources
associated with the dialog box window.
Dialog Container
import java.awt.*; public void actionPerformed( ActionEvent e )
{
import java.awt.event.*;
Exam.d.dispose();
class Exam extends Frame implements
ActionListener{ }
static Dialog d;
void showFrame(){ public static void main(String args[]) {
add(new Label("this is frame")); Exam obj=new Exam();
d = new Dialog(this , "Dialog Example", obj.showFrame();
true); }
d.setLayout( new FlowLayout() ); }
Button b = new Button ("OK");
b.addActionListener (this);
d.add( new Label ("Click button to
continue."));
d.add(b);
d.setSize(300,300);
d.setVisible(true);
}
Dialog Container
import java.awt.*; public void actionPerformed( ActionEvent e )
{
import java.awt.event.*;
Exam.d.dispose();
class Exam extends Frame implements
ActionListener{ }
static Dialog d;
void showFrame(){ public static void main(String args[]) {
add(new Label("this is frame")); Exam obj=new Exam();
d = new Dialog(this , "Dialog Example", obj.showFrame();
true); }
d.setLayout( new FlowLayout() ); }
Button b = new Button ("OK");
b.addActionListener (this);
d.add( new Label ("Click button to
continue."));
d.add(b);
d.setSize(300,300);
d.setVisible(true);
}
ScrollPane
• A container class which implements automatic horizontal and/or
vertical scrolling for a single child component.
• The display policy for the scrollbars can be set to:
SCROLLBARS_AS_NEEDED: scrollbars created and shown only when
needed by scrollpane
SCROLLBARS_ALWAYS: scrollbars created and always shown by the
scrollpane
SCROLLBARS_NEVER: scrollbars never created or shown by the scrollpane
ScrollPane()
• Create a new scrollpane container with a scrollbar display policy of "as
needed".
ScrollPane(int scrollbarDisplayPolicy)
• Create a new scrollpane container.
ScrollPane
• The initial size of this container is set to 100x100, but can be reset
using setSize().
setScrollPosition(int x, int y)
• Scrolls to the specified position within the child component.
Swing Containers
• javax.swing package provides classes for java swing components.
• Creation of swing containers and components is very similar to AWT
based Containers and Components.
• Containers
JPanel
JFrame
Jwindow
JDialog
Swing components
• JButton • Jmenu • JComboBox
• JTextArea • Jpasswordfield • JComponent
• JTextField • JPanel • JDialog
• JCheckBox • JPopupMenu • JTextPane
• JLabel • JProgressBar • JToolBar
• JList • JScrollPane • JTree
Swings vs AWT
AWT Swing
JCheckBox
JList
JComboBox
JTextField
JSlider
JSpinner
JPasswordField
JMenu
JLabel JRadioButton
09/23/2024 93
• Top-Level Containers
JApplet JFrame
JDialog
• General-Purpose Containers
JPanel
JScrollPane
09/23/2024
JTabbedPane 94