An Event driven GUI developed in lecture

hundredcarriageSoftware and s/w Development

Nov 3, 2013 (3 years and 11 months ago)

94 views

An E
vent driven GUI

developed
in lecture
. All imports removed

to save space


public

class

FirstGUI
extends

JFrame {


public

static

void

main(String[] args) {


JFrame aWindow =
new

FirstGUI();


aWindow.setVisible(
true
);


}



public

FirstGUI() {


layoutGUI();
// The first of three things to do


registerListeners();


}



// Add 3 instance variables (Slide 9)


private

JLabel
aLabel

=
new

JLabel(
"Change with setText(String)"
);


private

JTextArea
textEditor

=
new

JTextArea(
"You can edit this

text "
);


private

JButton
clickMeButton

=
new

JButton(
"Nobody is listening to me"
);



private

void

layoutGUI() {


// Slide 7


setTitle(
"Graffiti"
);


setDefaultCloseOperation(JFrame.
EXIT_ON_CLOSE
);


// Slide 9



setSize(200, 150);
// 200 pixels wide, 150 pixels tall


setLocation(75, 40);
// left 75, down 40


// Slide 9


setLayout(
null
);
// Set JFrame’s layout manager



// Slide 10


aLabel
.setSize(180, 30);


aLabel
.setLocation(5, 0);


add(
aLabel
);


textEditor
.setSize(180, 60);


textEditor
.setLocation(5, 35);


add(
textEditor
);


clickMeButton
.setSize(180, 30);


clickMeButton
.setLocation(5, 95);


add(
clickMeButton
);


// Slide 11 (use this in the Boggle GUI


textEditor
.setLineWrap(
true
);


textEditor
.setWrapStyleWord(
true
);


}



// Slide 18


private

void

registerListeners() {


// Register the instance of the listener so the component


// can later send messages to that object


ButtonListener aListener =
n
ew

ButtonListener();


clickMeButton
.addActionListener(aListener);


}



// Slide 18
:
Inner class to listen to events


private

class

ButtonListener
implements

ActionListener {


// clickMeButton will call this method once registered (below)


public

void

actionPerformed(ActionEvent anActionEvent) {


String allInput =
textEditor
.getText();


JOptionPane.
showMessageDialog
(
null
, allInput);


}


}

}