99 bottles of beer: Difference between revisions
Content added Content deleted
(Added Java, needed to spearate the verses in BASIC version) |
(→{{header|Java}}: Addd GUI version) |
||
Line 33: | Line 33: | ||
=={{header|Java}}== |
=={{header|Java}}== |
||
===Console=== |
|||
<java>public class Beer{ |
<java>public class Beer{ |
||
public static void main(String[] args){ |
public static void main(String[] args){ |
||
Line 38: | Line 39: | ||
System.out.println(x+"bottles of beer on the wall\n"+x+"bottles of beer\n"+"Take one down, pass it around\n"+(x-1)+"bottles of beer on the wall\n"); |
System.out.println(x+"bottles of beer on the wall\n"+x+"bottles of beer\n"+"Take one down, pass it around\n"+(x-1)+"bottles of beer on the wall\n"); |
||
} |
} |
||
}</java> |
|||
===GUI=== |
|||
{{works with|Swing}} |
|||
This version requires user interaction. The first two lines are shown in a text area on a window. The third line is shown on a button which you need to click to see the fourth line in a message box. The numbers update and the process repeats until "0 bottles of beer on the wall" is shown in a message box, when the program ends. |
|||
<java>import java.awt.BorderLayout; |
|||
import java.awt.event.ActionEvent; |
|||
import java.awt.event.ActionListener; |
|||
import javax.swing.JButton; |
|||
import javax.swing.JFrame; |
|||
import javax.swing.JOptionPane; |
|||
import javax.swing.JTextArea; |
|||
public class Beer extends JFrame implements ActionListener{ |
|||
private int x; |
|||
private JButton take; |
|||
private JTextArea text; |
|||
public static void main(String[] args){ |
|||
new Beer(); |
|||
} |
|||
public Beer(){ |
|||
x= 99; |
|||
take= new JButton("Take one down, pass it around"); |
|||
text= new JTextArea(4,30); |
|||
text.setText(x + " bottles of beer on the wall\n" + x + " bottles of beer"); |
|||
take.addActionListener(this); |
|||
setLayout(new BorderLayout()); |
|||
add(text, BorderLayout.CENTER); |
|||
add(take, BorderLayout.SOUTH); |
|||
pack(); |
|||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
|||
setVisible(true); |
|||
} |
|||
public void actionPerformed(ActionEvent arg0){ |
|||
if(arg0.getSource() == take){ |
|||
--x; |
|||
JOptionPane.showMessageDialog(null, x + " bottles of beer on the wall"); |
|||
text.setText(x + " bottles of beer on the wall\n" + x + " bottles of beer"); |
|||
} |
|||
if(x == 0){ |
|||
dispose(); |
|||
} |
|||
} |
|||
}</java> |
}</java> |