GUI Tutorial Day 3

slimwhimperΛογισμικό & κατασκευή λογ/κού

3 Νοε 2013 (πριν από 3 χρόνια και 9 μήνες)

61 εμφανίσεις

GUI Tutorial Day 3

Custom Dialog

Create, display, hide, retrieve information

Start as usual


with JFrame

import javax.swing.*;

import java.awt.BorderLayout;

import java.awt.event.*;


public class ShowDialogGUI extends JFrame {


private JButton button1, button2;


public ShowDialogGUI(){



setTitle("Show Dialog");



setSize(200, 100);


setDefaultCloseOperation(JFrame.
EXIT_ON_CLOSE);



button1 = new JButton("Login");



button2 = new JButton("Say hello");



// Experiment with different locations!



add(button1, BorderLayout.CENTER);


add(button2, BorderLayout.SOUTH);


}

public static void main(String[] args) {


ShowDialogGUI gui = new ShowDialogGUI();



gui.setVisible(true);

}

}


http://leepoint.net/notes
-
java/GUI/layouts/20borderlayout.html

Now create a custom dialog


extend JDialog

public class MyDialog
extends JDialog
{

private JTextField name;

private JPasswordField password;

public MyDialog()

{


setTitle("Login Dialog");


setSize(300, 200);


setLayout(new GridLayout(2, 2));


JLabel nameLabel = new JLabel("Name");


name = new JTextField(20);


JLabel pwdLabel = new JLabel("Password");


password = new JPasswordField();


add(nameLabel);


add(name);


add(pwdLabel);


add(password);

… continued next slide


Notice the use of inheritance

JDialog has many of the same methods as JPanel

Creates a grid layout with the specified
number of rows and columns. All
components in the layout are given equal
size. One, but not both, of rows and cols
can be zero, which means that any number
of objects can be placed in a row or in a
column.


Try: add a
JButton

while grid is set to 2x2


what happen?

Try: remove the layout manager


what happens?

Now get the dialog to display


Add an action listener to the button in the JFrame

public ShowDialogGUI() {


. . .


class ButtonListener implements ActionListener {


public void actionPerformed(ActionEvent e) {



if (e.getSource() == button1) {



dialog = new MyDialog();



dialog.setVisible(true);



} else {



if (dialog != null) {




String name = dialog.getName();


JOptionPane.showMessageDialog(null, "Hello " + name);



}

}}}


Create an instance variable of type MyDialog


Remember to add the button listener to
both

buttons.


Note use of getSource to check which button

Actions for button2, first ensure dialog has been created

Test

May want an OK button

public MyDialog()

{





JButton button = new JButton("OK");


button.addActionListener(new ActionListener() {


public void actionPerformed(ActionEvent e)


{


setVisible(false);


}

});

add(button);


Syntax Hint:

button.addActionListener(new ButtonListener());


What about the grid layout?


anonymous listener; class definition

notice ); at end of statement

Need to return the name

public String getName()

{


return name.getText();

}



What happens if you don’t have this method but press Hello?

Complete program for reference



dialog JFrame

import javax.swing.*;

import java.awt.BorderLayout;

import java.awt.event.*;


public class ShowDialogGUI extends JFrame {

private MyDialog dialog;

private JButton button1, button2;

public ShowDialogGUI()

{

setTitle("Show Dialog");

setSize(200, 100);

button1 = new JButton("Login");

button2 = new JButton("Say Hello");

button1.addActionListener(new ButtonListener());

button2.addActionListener(new ButtonListener());

add(button1, BorderLayout.
CENTER);

add(button2, BorderLayout.
SOUTH);

}

class ButtonListener implements ActionListener

{

public void actionPerformed(ActionEvent e)

{


if (e.getSource() == button1) {


dialog = new MyDialog();


dialog.setVisible(true);


}


else {


if (dialog != null)


{



String name = dialog.getName();



JOptionPane.
showMessageDialog(null, "Hello "
+ name);


}


}

}

}

public static void main(String[] args) {


ShowDialogGUI gui = new ShowDialogGUI();


gui.setDefaultCloseOperation(JFrame.
EXIT_ON_CLO
SE);


gui.setVisible(true);

}

}



Complete program for reference



custom dialog

import javax.swing.*;

import java.awt.*;


public class MyDialog extends JDialog {

private JTextField name;

private JPasswordField password;

public MyDialog()

{


setTitle("Login Dialog");


setSize(300, 200);


setLayout(new GridLayout(2, 2));


JLabel nameLabel = new JLabel("Name");


name = new JTextField(20);


JLabel pwdLabel = new JLabel("Password");


password = new JPasswordField();


add(nameLabel);


add(name);


add(pwdLabel);


add(password);


JButton button = new JButton("OK");


button.addActionListener(new ActionListener() {


public void actionPerformed(ActionEvent e)


{


setVisible(false);


}


});


add(button);


}

} // end of MyDialog ctor


public String getName()

{


return name.getText();

}


} // end of class