import javax.swing.*;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class HelloWorld implements ActionListener{
static String text= "hello world";
public static int count1;
public static int count2;
JFrame frame;
JPanel content;
JLabel lblGeneral1;
JLabel lblGeneral2;
JButton btnTest1;
JButton btnTest2;
JLabel label1;
JLabel label2;
public HelloWorld(){
//create frame
frame=new JFrame ("Hello World");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//create panel
content= new JPanel();
content.setLayout(new BoxLayout(content, BoxLayout.PAGE_AXIS));
//set button1
btnTest1=new JButton("Superior");
btnTest1.setActionCommand("1");
btnTest1.addActionListener(this);
btnTest1.setAlignmentY(Component.CENTER_ALIGNMENT);
btnTest1.setAlignmentX(Component.LEFT_ALIGNMENT);
content.add(btnTest1);
//set button2
btnTest2=new JButton("Superior");
btnTest2.setActionCommand("2");
btnTest2.addActionListener(this);
btnTest2.setAlignmentY(Component.CENTER_ALIGNMENT);
btnTest2.setAlignmentX(Component.RIGHT_ALIGNMENT);
content.add(btnTest2);
//create label
lblGeneral1=new JLabel(Integer.toString(count1));
lblGeneral1.setAlignmentY(Component.BOTTOM_ALIGNMENT);
lblGeneral1.setAlignmentX(Component.LEFT_ALIGNMENT);
content.add(lblGeneral1);
//create label
lblGeneral2=new JLabel(Integer.toString(count2));
lblGeneral2.setAlignmentY(Component.BOTTOM_ALIGNMENT);
lblGeneral2.setAlignmentX(Component.RIGHT_ALIGNMENT);
content.add(lblGeneral2);
//add picture box
String imgStr = "Penguins.jpg";
ImageIcon image = new ImageIcon(imgStr);
JLabel label1 = new JLabel(image);
label1.setAlignmentY(Component.TOP_ALIGNMENT);
label1.setAlignmentX(Component.LEFT_ALIGNMENT);
content.add(label1);
//add picture box
String imgStr2 = "Penguins.jpg";
ImageIcon image2 = new ImageIcon(imgStr);
JLabel label2 = new JLabel(image);
label2.setAlignmentY(Component.TOP_ALIGNMENT);
label2.setAlignmentX(Component.RIGHT_ALIGNMENT);
content.add(label1);
//content to frame
frame.setContentPane(content);
//set display
frame.pack();
frame.setVisible(true);
}
//handle button click
public void actionPerformed(ActionEvent event){
String btn=event.getActionCommand();
if (btn.equals("1"))
count1++;
else
count2++;
lblGeneral1.setText(Integer.toString(count1));
lblGeneral2.setText(Integer.toString(count2));
}
//show the GUI
public static void runGUI(){
JFrame.setDefaultLookAndFeelDecorated(true);
HelloWorld greeting=new HelloWorld();
}
public static void main (String[] args){
//methods that show GUI should be run from a event dispatcher
count1=0;
count2=0;
javax.swing.SwingUtilities.invokeLater(new Runnable(){
public void run(){
runGUI();
}
});
}
}