Etiquetas » GUI

Tic Tac Toe Java GUI

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
public class tttGui{
	static int count=0;
	static String val=" "; static String val2=" "; static String val3=" "; static String val4=" "; static String val5=" ";
	static String val6=" "; static String val7=" "; static String val8=" "; static String val9=" "; static String n1=" "; static String n2=" ";


	public static void result(int count,String val,String val2,String val3,String val4,String val5, String val6, String val7,String val8,String val9,String name1,String name2){
		if((val!=" ") && (val.equals(val2)) && (val2.equals(val3))){		// horizontal
			if(count%2==0 && val=="O")
				JOptionPane.showMessageDialog(null,name2+"player 2 wins");
			else
				JOptionPane.showMessageDialog(null,name1+"player 1 wins");
		}
		else if((val4!=" ") && (val4.equals(val5)) && (val5.equals(val6))){
			if(count%2==0 && val4=="O")
				JOptionPane.showMessageDialog(null,name2+"player 2 wins");
			else
				JOptionPane.showMessageDialog(null,name1+"player 1 wins");
		}
		else if((val7!=" ") && (val7.equals(val8)) && (val2.equals(val9))){
			if(count%2==0 && val7=="O")
				JOptionPane.showMessageDialog(null,name2+"player 2 wins");
			else
				JOptionPane.showMessageDialog(null,name1+"player 1 wins");
		}
		else if((val!=" ") && (val.equals(val4)) && (val4.equals(val7))){    //vertical
			if(count%2==0 && val=="O")
				JOptionPane.showMessageDialog(null,name2+"player 2 wins");
			else
				JOptionPane.showMessageDialog(null,"player 1 wins");
		}
		else if((val2!=" ") && (val2.equals(val5)) && (val5.equals(val8))){
			if(count%2==0 && val2=="O")
				JOptionPane.showMessageDialog(null,name2+"player 2 wins");
			else
				JOptionPane.showMessageDialog(null,name1+"player 1 wins");
		}
		else if((val3!=" ") && (val3.equals(val6)) && (val3.equals(val9))){
			if(count%2==0 && val3=="O")
				JOptionPane.showMessageDialog(null,name2+"player 2 wins");
			else
				JOptionPane.showMessageDialog(null,name1+"player 1 wins");
		}
		else if((val!=" ") && (val.equals(val5)) && (val5.equals(val9))){		//slant
			if(count%2==0 && val=="O")
				JOptionPane.showMessageDialog(null,name2+"player 2 wins");
			else
				JOptionPane.showMessageDialog(null,name1+"player 1 wins");
		}
		else if((val3!=" ") && (val3.equals(val5)) && (val5.equals(val7))){
			if(count%2==0 && val3=="O")
				JOptionPane.showMessageDialog(null,name2+"player 2 wins");
			else
				JOptionPane.showMessageDialog(null,"player 1 wins");
		}

	}
	public static void main(String[] args) {
		JFrame f = new JFrame("Tic Tac Toe");
		JButton b1 = new JButton(" ");
		JButton b2 = new JButton(" ");	
		JButton b3 = new JButton(" ");
		JButton b4 = new JButton(" ");
		JButton b5 = new JButton(" ");
		JButton b6 = new JButton(" ");
		JButton b7 = new JButton(" ");
		JButton b8 = new JButton(" ");
		JButton b9 = new JButton(" ");
		JButton reset = new JButton("Reset");
		JTextField name1 = new JTextField();
		JTextField name2 = new JTextField();


		f.add(b1); f.add(b2); f.add(b3); 
		f.add(b4); f.add(b5); f.add(b6); 
		f.add(b7); f.add(b8); f.add(b9);
		f.add(reset); f.add(name1); f.add(name2);

		f.setLayout(new GridLayout(4,3));

		b1.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e){
				count++;
				val=b1.getText(); val2=b2.getText(); val3=b3.getText();
				val4=b4.getText(); val5=b5.getText(); val6=b6.getText();
				val7=b7.getText(); val8=b8.getText(); val9=b9.getText();
				n1=name1.getText(); n2=name2.getText();
				if(val!="X" && val!="O"){
					if(count%2==0){
						b1.setText("O");
						val=b1.getText();
						//System.out.println("1 "+val+" 2 "+val2+" 3 "+val3+" 4 "+val4+" 5 "+val5+" 6 "+val6+" 7 "+val7+" 8 "+val8+" 9 "+val9);
					}
					else{
						b1.setText("X");
						val=b1.getText();
						//System.out.println("1 "+val+" 2 "+val2+" 3 "+val3+" 4 "+val4+" 5 "+val5+" 6 "+val6+" 7 "+val7+" 8 "+val8+" 9 "+val9);					
					}
					result(count,val,val2,val3,val4,val5,val6,val7,val8,val9,n1,n2);
				}
				else{
					JOptionPane.showMessageDialog(null,"Already Clicked");
				}
			}
		});
		b2.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e){
				count++;
				val=b1.getText(); val2=b2.getText(); val3=b3.getText();
				val4=b4.getText(); val5=b5.getText(); val6=b6.getText();
				val7=b7.getText(); val8=b8.getText(); val9=b9.getText();
				n1=name1.getText(); n2=name2.getText();
				if(val2!="X" && val2!="O"){
					if(count%2==0){
						b2.setText("O");
						val2=b2.getText();
						//System.out.println("1 "+val+" 2 "+val2+" 3 "+val3+" 4 "+val4+" 5 "+val5+" 6 "+val6+" 7 "+val7+" 8 "+val8+" 9 "+val9);
					}
					else{
						b2.setText("X");
						val2=b2.getText();
						//System.out.println("1 "+val+" 2 "+val2+" 3 "+val3+" 4 "+val4+" 5 "+val5+" 6 "+val6+" 7 "+val7+" 8 "+val8+" 9 "+val9);
					}
					result(count,val,val2,val3,val4,val5,val6,val7,val8,val9,n1,n2);
				}
				else{
					JOptionPane.showMessageDialog(null,"Already Clicked");
				}
			}
		});
		b3.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e){
				count++;
				val=b1.getText(); val2=b2.getText(); val3=b3.getText();
				val4=b4.getText(); val5=b5.getText(); val6=b6.getText();
				val7=b7.getText(); val8=b8.getText(); val9=b9.getText();
				n1=name1.getText(); n2=name2.getText();
				if(val3!="X" && val3!="O"){
					if(count%2==0){
						b3.setText("O");
						val3=b3.getText();
						//System.out.println("1 "+val+" 2 "+val2+" 3 "+val3+" 4 "+val4+" 5 "+val5+" 6 "+val6+" 7 "+val7+" 8 "+val8+" 9 "+val9);
					}
					else{
						b3.setText("X");
						val3=b3.getText();
						//System.out.println("1 "+val+" 2 "+val2+" 3 "+val3+" 4 "+val4+" 5 "+val5+" 6 "+val6+" 7 "+val7+" 8 "+val8+" 9 "+val9);
					}
					result(count,val,val2,val3,val4,val5,val6,val7,val8,val9,n1,n2);
				}
				else
					JOptionPane.showMessageDialog(null,"Already Clicked");
			}
		});
		b4.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e){
				count++;
				val=b1.getText(); val2=b2.getText(); val3=b3.getText();
				val4=b4.getText(); val5=b5.getText(); val6=b6.getText();
				val7=b7.getText(); val8=b8.getText(); val9=b9.getText();
				n1=name1.getText(); n2=name2.getText();
				if(val4!="X" && val4!="O"){
					if(count%2==0){
						b4.setText("O");
						val4=b4.getText();
						//System.out.println("1 "+val+" 2 "+val2+" 3 "+val3+" 4 "+val4+" 5 "+val5+" 6 "+val6+" 7 "+val7+" 8 "+val8+" 9 "+val9);
					}
					else{
						b4.setText("X");
						val4=b4.getText();
						//System.out.println("1 "+val+" 2 "+val2+" 3 "+val3+" 4 "+val4+" 5 "+val5+" 6 "+val6+" 7 "+val7+" 8 "+val8+" 9 "+val9);
					}
					result(count,val,val2,val3,val4,val5,val6,val7,val8,val9,n1,n2);
				}
				else{
					JOptionPane.showMessageDialog(null,"Already Clicked");
				}
			}
		});
		b5.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e){
				count++;
				val=b1.getText(); val2=b2.getText(); val3=b3.getText();
				val4=b4.getText(); val5=b5.getText(); val6=b6.getText();
				val7=b7.getText(); val8=b8.getText(); val9=b9.getText();
				n1=name1.getText(); n2=name2.getText();
				if(val5!="X" && val5!="O"){
					if(count%2==0){
						b5.setText("O");
						val5=b5.getText();
						//System.out.println("1 "+val+" 2 "+val2+" 3 "+val3+" 4 "+val4+" 5 "+val5+" 6 "+val6+" 7 "+val7+" 8 "+val8+" 9 "+val9);
					}
					else{
						b5.setText("X");
						val5=b5.getText();
						//System.out.println("1 "+val+" 2 "+val2+" 3 "+val3+" 4 "+val4+" 5 "+val5+" 6 "+val6+" 7 "+val7+" 8 "+val8+" 9 "+val9);
					}
					result(count,val,val2,val3,val4,val5,val6,val7,val8,val9,n1,n2);
				}
				else{
					JOptionPane.showMessageDialog(null,"Already Clicked");				}
			}
		});
		b6.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e){
				count++;
				val=b1.getText(); val2=b2.getText(); val3=b3.getText();
				val4=b4.getText(); val5=b5.getText(); val6=b6.getText();
				val7=b7.getText(); val8=b8.getText(); val9=b9.getText();
				n1=name1.getText(); n2=name2.getText();
				if(val6!="X" && val6!="O"){
					if(count%2==0){
						b6.setText("O");
						val6=b6.getText();
						//System.out.println("1 "+val+" 2 "+val2+" 3 "+val3+" 4 "+val4+" 5 "+val5+" 6 "+val6+" 7 "+val7+" 8 "+val8+" 9 "+val9);
					}
					else{
						b6.setText("X");
						val6=b6.getText();
						//System.out.println("1 "+val+" 2 "+val2+" 3 "+val3+" 4 "+val4+" 5 "+val5+" 6 "+val6+" 7 "+val7+" 8 "+val8+" 9 "+val9);
					}
					result(count,val,val2,val3,val4,val5,val6,val7,val8,val9,n1,n2);
				}
				else{
					JOptionPane.showMessageDialog(null,"Already Clicked");
				}
			}
		});
		b7.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e){
				count++;
				val=b1.getText(); val2=b2.getText(); val3=b3.getText();
				val4=b4.getText(); val5=b5.getText(); val6=b6.getText();
				val7=b7.getText(); val8=b8.getText(); val9=b9.getText();
				n1=name1.getText(); n2=name2.getText();
				if(val7!="X" && val7!="O"){
					if(count%2==0){
						b7.setText("O");
						val7=b7.getText();
						//System.out.println("1 "+val+" 2 "+val2+" 3 "+val3+" 4 "+val4+" 5 "+val5+" 6 "+val6+" 7 "+val7+" 8 "+val8+" 9 "+val9);
					}
					else{
						b7.setText("X");
						val7=b7.getText();
						//System.out.println("1 "+val+" 2 "+val2+" 3 "+val3+" 4 "+val4+" 5 "+val5+" 6 "+val6+" 7 "+val7+" 8 "+val8+" 9 "+val9);
					}
					result(count,val,val2,val3,val4,val5,val6,val7,val8,val9,n1,n2);
				}
				else{
					JOptionPane.showMessageDialog(null,"Already Clicked");
				}
			}
		});
		b8.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e){
				count++;
				val=b1.getText(); val2=b2.getText(); val3=b3.getText();
				val4=b4.getText(); val5=b5.getText(); val6=b6.getText();
				val7=b7.getText(); val8=b8.getText(); val9=b9.getText();
				n1=name1.getText(); n2=name2.getText();
				if(val8!="X" && val8!="O"){
					if(count%2==0){
						b8.setText("O");
						val8=b8.getText();
						//System.out.println("1 "+val+" 2 "+val2+" 3 "+val3+" 4 "+val4+" 5 "+val5+" 6 "+val6+" 7 "+val7+" 8 "+val8+" 9 "+val9);
					}
					else{
						b8.setText("X");
						val8=b8.getText();
						//System.out.println("1 "+val+" 2 "+val2+" 3 "+val3+" 4 "+val4+" 5 "+val5+" 6 "+val6+" 7 "+val7+" 8 "+val8+" 9 "+val9);
					}
					result(count,val,val2,val3,val4,val5,val6,val7,val8,val9,n1,n2);
				}
				else{
					JOptionPane.showMessageDialog(null,"Already Clicked");
				}
			}
		});
		b9.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e){
				count++;
				val=b1.getText(); val2=b2.getText(); val3=b3.getText();
				val4=b4.getText(); val5=b5.getText(); val6=b6.getText();
				val7=b7.getText(); val8=b8.getText(); val9=b9.getText();
				n1=name1.getText(); n2=name2.getText();
				if(val9!="X" && val9!="O"){
					if(count%2==0){
						b9.setText("O");
						val9=b9.getText();
						//System.out.println("1 "+val+" 2 "+val2+" 3 "+val3+" 4 "+val4+" 5 "+val5+" 6 "+val6+" 7 "+val7+" 8 "+val8+" 9 "+val9);
					}
					else{
						b9.setText("X");
						val9=b9.getText();
						//System.out.println("1 "+val+" 2 "+val2+" 3 "+val3+" 4 "+val4+" 5 "+val5+" 6 "+val6+" 7 "+val7+" 8 "+val8+" 9 "+val9);
					}
					result(count,val,val2,val3,val4,val5,val6,val7,val8,val9,n1,n2);
				}
				else{
					JOptionPane.showMessageDialog(null,"Already Clicked");
				}
			}
		});

		reset.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e){
				b1.setText(" "); b2.setText(" "); b3.setText(" "); 
				b4.setText(" "); b5.setText(" "); b6.setText(" "); 
				b7.setText(" "); b8.setText(" "); b9.setText(" ");
				name1.setText(""); name2.setText("");
				count=0;
			}
		});

		f.setSize(300,300);
		f.setVisible(true);

	}
}

Morse Code in Java

package morseCode;
import java.awt.Color;
import java.awt.Font;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class morseCode {
	static String convert = "", converted = "";
	static String str = "Morse Translator by Nitesh Pant Note:Morse code words are separated by: / Press English>>Morse button to generate morse code";
	public static void main(String[] args) {
		final LinkedHashMap<Character,String> map = new LinkedHashMap<Character,String>();
			map.put('a',".-");
			map.put('b',"-...");
			map.put('c',"-.-.");
			map.put('d',"-..");
			map.put('e',".");
			map.put('f',"..-.");
			map.put('g',"--.");
			map.put('h',"....");
			map.put('i',"..");
			map.put('j',".---");
			map.put('k',"-.-");
			map.put('l',".-..");
			map.put('m',"--");
			map.put('n',"-.");
			map.put('o',"---");
			map.put('p',".--.");
			map.put('q',"--.-");
			map.put('r',".-.");
			map.put('s',"...");
			map.put('t',"-");
			map.put('u',"..-");
			map.put('v',"...-");
			map.put('w',".--");
			map.put('x',"-..-");
			map.put('y',"-.--");
			map.put('z',"--..");
			map.put('1',".----");
			map.put('2',"..---");
			map.put('3',"...--");
			map.put('4',"....-");
			map.put('5',".....");
			map.put('6',"-....");
			map.put('7',"--...");
			map.put('8',"---..");
			map.put('9',"----.");
			map.put('0',"-----");
			
			JFrame frame=new JFrame("Morse Code");
			frame.setSize(600,700);
			frame.setVisible(true);
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			frame.setLocationRelativeTo(null);
			frame.getContentPane().setLayout(null);
			
			final JLabel jl = new JLabel("English Text");
			jl.setBounds(10,110,100,10);
			
			final JLabel jl2 = new JLabel("Morse Code");
			jl2.setBounds(320,110,100,10);
			
			final JLabel jl3 = new JLabel(str);
			jl3.setBounds(10,10,500,100);
			
			final TextArea ta = new TextArea();
			ta.setBounds(10,150,270,400);
			
		/*	Font font = new Font("Segoe Script", Font.BOLD, 20);
	        ta.setFont(font);
	        ta.setForeground(Color.BLUE);
	      */  
			final TextArea ta2 = new TextArea();
			ta2.setBounds(300,150,270,400);
			
			JButton jb = new JButton("English>>Morse");
			jb.setBounds(10,580,130,50);
			
			JButton jb2 = new JButton("Clear Text");
			jb2.setBounds(200,580,130,50);
			
			JButton jb3 = new JButton("English<<Morse");
			jb3.setBounds(390,580,130,50);
			
			frame.add(ta);
			frame.add(ta2);
			frame.add(jb);
			frame.add(jb2);
			frame.add(jb3);
			frame.add(jl);
			frame.add(jl2);
			frame.add(jl3);
			
			jb.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					convert = ta.getText();
					convert = convert.toLowerCase();
					for(int i=0;i<convert.length();i++){
						if(convert.charAt(i)==' '){
							converted +=" / ";
						}
						else{
							converted += (map.get(convert.charAt(i))+" ");
						}
						
					}
					Font font = new Font("Segoe Script", Font.BOLD, 20);
			        ta2.setFont(font);
			        ta2.setForeground(Color.BLUE);
			        
					ta2.append(converted);
			
				}
			});
			
			jb2.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					converted = "";
					convert = "";
					ta.setText(null);
					ta2.setText(null);
				}
			});
		
			
			jb3.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					convert = ta2.getText();
					List lst = new ArrayList();
					
					for(int i=0;i<=convert.length();i++){
						if(i==convert.length() || convert.charAt(i)==' '){
							StringBuilder sb = new StringBuilder();
							for(Character ch : lst){
								sb.append(ch);
							}
							lst.removeAll(lst);
							String s = sb.toString();
							for(java.util.Map.Entry<Character, String> entry : map.entrySet()){
								if(entry.getValue().equals(s)){
									converted += entry.getKey();
								}
							}
						}
						else{
							lst.add(convert.charAt(i));
						}
					}
					Font font = new Font("Segoe Script", Font.BOLD, 20);
			        ta.setFont(font);
			        ta.setForeground(Color.BLUE);
					ta.setText(converted);
				}
			});

	}
	
}
… 6 palabras más

Session 22-25

Connecting GUI’s to Visual studio and our DATABASE and DataGrids

The last things we did for milestone 3 was connecting everything up and inputing a datagridview.

262 palabras más

Pemrograman Pertemuan 15

Latihan Program Cetak CheckBox Menggunakan NetBeans 8.0

  1. Buat terlebih dahulu desain seperti di bawah ini dengan memasukkan swing controls :
    1. Label
    2. Text Field…
  2. 110 palabras más
GUI

Cross-compiling QT 15.2 for Raspberry PI

I lost visible amount of hair over the course of last couple of weeks. All because I had this Idée fixe to develop a cute little GUI app for my Raspberry PI Zero (More about it in the future posts). 546 palabras más

Gcc

Create a Text Box to Accept User Input for PowerShell GUI

Do you have a PowerShell GUI that needs to accept text input? Maybe you want to be able to copy and paste a list of computers, ip addresses, user names or some other data. 1.335 palabras más

Powershell

Configuring the simplest service chain step-by-step

In a previous post I introduced the concept of Service Chain in Contrail.
Today, I’m going to show how a chain can be configured via GUI. 340 palabras más