Jump to content

99 bottles of beer: Difference between revisions

m
→‎GUI: Added comments
(+D)
m (→‎GUI: Added comments)
Line 192:
private JTextArea text;
public static void main(String[] args){
new Beer();//build and show the GUI
}
 
Line 198:
x= 99;
take= new JButton("Take one down, pass it around");
text= new JTextArea(4,30);//size the area to 4 lines, 30 chars each
text.setText(x + " bottles of beer on the wall\n" + x + " bottles of beer");
take.addActionListener(this);//listen to the button
setLayout(new BorderLayout());//handle placement of components
add(text, BorderLayout.CENTER);//put the text area in the largest section
add(take, BorderLayout.SOUTH);//put the button underneath it
pack();//auto-size the window
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//exit on "X" (I hate System.exit...)
setVisible(true);//show it
}
 
public void actionPerformed(ActionEvent arg0){
if(arg0.getSource() == take){//if they clicked the button
--x;
JOptionPane.showMessageDialog(null, x + " bottles of beer on the wall");//show a popup message
text.setText(x + " bottles of beer on the wall\n" + x + " bottles of beer");//change the text area
}
if(x == 0){//if it's the end
dispose();//end
}
}
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.