Ulaen, Hendra Sandag, Green Mogot, Larry Assignment Visual Programming Program Remote TV Sederhana
Ulaen, Hendra Sandag, Green Mogot, Larry Assignment Visual Programming Program Remote TV Sederhana
//File //Date //Desc import import import import : TestRemote.java : 15 September 2010 : Program Remote Control TV sederhana java.awt.*; java.awt.event.*; javax.swing.*; javax.swing.event.*;
public class TestRemote extends JFrame { //inisialisasi channel dan volume public static int Channel = 0; public static int Volume = 1; //inisialisasi button remote public JButton btnStart = new JButton("OFF"); public JButton btnChnl1 = new JButton("1"); public JButton btnChnl2 = new JButton("2"); public JButton btnChnl3 = new JButton("3"); public JButton btnChnl4 = new JButton("4"); public JButton btnChnl5 = new JButton("5"); public JButton btnChnl6 = new JButton("6"); public JButton btnChnl7 = new JButton("7"); public JButton btnChnl8 = new JButton("8"); public JButton btnChnl9 = new JButton("9"); public JButton btnChnlUp = new JButton("Ch+"); public JButton btnChnlDown = new JButton("Ch-"); public JButton btnVolUp = new JButton("Vol+"); public JButton btnVolDown = new JButton("Vol-"); //inisialisasi label untuk keterangan secara global public JLabel lbl = new JLabel("Remote Start",JLabel.CENTER); public TestRemote() { super("Test Remote"); //buat panel baru untuk tombol OFF JPanel jpnlExit = new JPanel(); jpnlExit.setLayout(new FlowLayout(FlowLayout.CENTER,10,5)); //atur posisi tombol di tengah jpnlExit.add(btnStart,BorderLayout.CENTER); //letakkan panel di bagian atas add("North",jpnlExit); //buat panel baru untuk tombol-tombol channel JPanel jpnlChnl = new JPanel(); //atur pengaturan tombol-tombol 3 * 3 secara rapi jpnlChnl.setLayout(new GridLayout(3,3)); jpnlChnl.setLayout(new FlowLayout(FlowLayout.CENTER,15,15)); //add tombol-tombol
jpnlChnl.add(btnChnl1); jpnlChnl.add(btnChnl2); jpnlChnl.add(btnChnl3); jpnlChnl.add(btnChnl4); jpnlChnl.add(btnChnl5); jpnlChnl.add(btnChnl6); jpnlChnl.add(btnChnl7); jpnlChnl.add(btnChnl8); jpnlChnl.add(btnChnl9); //letakkan panel di tengah add("Center",jpnlChnl); //buat panel untuk tombol tambah atau kurangi channel dan volume JPanel jpnlUpDown = new JPanel(); //atur posisi tombol 2 * 2 jpnlUpDown.setLayout(new GridLayout(2,2)); //add tombol-tombol jpnlUpDown.add(btnChnlUp); jpnlUpDown.add(btnChnlDown); jpnlUpDown.add(btnVolUp); jpnlUpDown.add(btnVolDown); add("South",jpnlUpDown); //letakkan frame di tengah layar dan tampilkan int width = 220; int height = 400; Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); int x = (screen.width-width)/2; int y = (screen.height-height)/2; setBounds(x,y,width,height); setVisible(true); //buat frame baru dengan label untuk menampilkan informasi JFrame f = new JFrame("Information"); f.getContentPane().add(lbl); f.setSize(400,300); //letakkan di sebelah frame utama dan tampilkan f.setLocation(100,300); f.setVisible(true); f.setDefaultCloseOperation(EXIT_ON_CLOSE); //panggil fungsi untuk cek channel dan volume untuk pertama kalinya CheckChannel(); CheckVolume(); btnStart.addActionListener(new ActionListener() { public void actionPerformed (ActionEvent a) { System.exit(0); } }); btnStart.addChangeListener(new ChangeListener() { public void stateChanged (ChangeEvent ch) { lbl.setText("State Changed : Exit"); } });
btnChnl1.addActionListener(new ActionListener() { public void actionPerformed (ActionEvent one) { Channel = 1; TuneChannel(); } }); btnChnl2.addActionListener(new ActionListener() { public void actionPerformed (ActionEvent two) { Channel = 2; TuneChannel(); } }); btnChnl3.addActionListener(new ActionListener() { public void actionPerformed (ActionEvent three) { Channel = 3; TuneChannel(); } }); btnChnl4.addActionListener(new ActionListener() { public void actionPerformed (ActionEvent four) { Channel = 4; TuneChannel(); } }); btnChnl5.addActionListener(new ActionListener() { public void actionPerformed (ActionEvent five) { Channel = 5; TuneChannel(); } }); btnChnl6.addActionListener(new ActionListener() { public void actionPerformed (ActionEvent six) { Channel = 6; TuneChannel(); } }); btnChnl7.addActionListener(new ActionListener() { public void actionPerformed (ActionEvent seven) { Channel = 7; TuneChannel(); } }); btnChnl8.addActionListener(new ActionListener() { public void actionPerformed (ActionEvent eight) { Channel = 8; TuneChannel(); } }); btnChnl9.addActionListener(new ActionListener() {
public void actionPerformed (ActionEvent nine) { Channel = 9; TuneChannel(); } }); btnChnlUp.addActionListener(new ActionListener() { public void actionPerformed (ActionEvent cnlup) { Channel = getIncrement(Channel); TuneChannel(); } }); btnChnlDown.addActionListener(new ActionListener() { public void actionPerformed (ActionEvent cnldown) { Channel = getDecrement(Channel); TuneChannel(); } }); btnVolUp.addActionListener(new ActionListener() { public void actionPerformed (ActionEvent volup) { Volume = getIncrement(Volume); lbl.setText("Volume Up : " +Volume); CheckVolume(); } }); btnVolDown.addActionListener(new ActionListener() { public void actionPerformed (ActionEvent voldown) { Volume = getDecrement(Volume); lbl.setText("Volume Down: " +Volume); CheckVolume(); } }); } public static void main(String[] args) { TestRemote test = new TestRemote(); } //fungsi untuk merubah channel void TuneChannel() { lbl.setText("Tune Channel : " +Channel); CheckChannel(); } //fungsi untuk mengecek channel void CheckChannel() { btnChnlDown.setEnabled(true); btnChnlUp.setEnabled(true); if(Channel<=1) { btnChnlDown.setEnabled(false);} else if(Channel ==9) { btnChnlUp.setEnabled(false);} }
static int getIncrement(int x) { x=x+1; return(x); } static int getDecrement(int x) { x=x-1; return(x); } //fungsi untuk mengecek volume void CheckVolume() { btnVolDown.setEnabled(true); btnVolUp.setEnabled(true); if(Volume<=1) { btnVolDown.setEnabled(false);} else if(Volume == 10) { btnVolUp.setEnabled(false);} } }