3 Replies - 6212 Views - Last Post: 30 October 2011 - 12:21 PM Rate Topic: -----

#1 hk95101   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 11-October 11

GUI_ JAVA CAESAR CIPHER

Posted 30 October 2011 - 10:07 AM

This is all i've been able to do. i still need to make a new frame that asks the days of the week, with a working radio button that works out the shift. how do you add a new frame to come up when they press "Encrypt" or "Decrypt".
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class MyCipher extends JFrame implements ActionListener 
{
// extends JFrame = allows me to make a bigger window
    
	 	JFrame jf1 = new JFrame ("Encrypt / Decrypt ");	
		JFrame jf2 = new JFrame ("Select the days of the week.");

      	
		JLabel lbl1 = new JLabel("Select what you would like to do ");
      JLabel lbl2 = new JLabel("Select the days of the week ");
      JLabel lbl3 = new JLabel("Action: ");
      JLabel lbl4 = new JLabel("Result: ");
      JTextField tf1 = new JTextField ("Type in the plain text");
      JTextField tf2 = new JTextField ("Type in the encrypted text");
        
		  	JRadioButton  d1r = new JRadioButton ("Monday");
			JRadioButton  d2r = new JRadioButton("Tuesday");
			JRadioButton  d3r = new JRadioButton("Wednesday");
			JRadioButton  d4r = new JRadioButton("Thursday");
			JRadioButton  d5r = new JRadioButton("Friday");
			JRadioButton  d6r = new JRadioButton("Saturday");
			JRadioButton  d7r = new JRadioButton("Sunday");
			
  			
			JButton btn1 = new JButton ("Encrypt");
			JButton btn2 = new JButton ("Decrypt");
			JButton btn3 = new JButton ("Next");
	 
    public MyCipher () 
	 {
   

	jf1.setSize(450,200);
		
	   lbl1.setBounds(150,50,300,50);
		btn1.setBounds(100,100,100,50);
		btn1.addActionListener(this);
		btn2.setBounds(250,100,100,50);
		btn2.addActionListener(this);
		
		   Container con = jf1.getContentPane();
         con.setLayout(null);
         con.add(lbl1);
   //       con.add(lbl2);
//          con.add(lbl3);
// 			con.add(lbl4);
//          con.add(tf1);
// 			con.add(tf2);
// 			con.add(d1r);
// 			con.add(d2r);
// 			con.add(d3r);
// 			con.add(d4r);
// 			con.add(d5r);
// 		   con.add(d6r);
// 			con.add(d7r);
			con.add(btn1);
			con.add(btn2);

		jf1.setVisible(true);
		}
		
  public void actionPerformed(ActionEvent e)
   {
	    	}
 
}

This post has been edited by smohd: 30 October 2011 - 10:12 AM
Reason for edit:: fixed code tags


Is This A Good Question/Topic? 0
  • +

Replies To: GUI_ JAVA CAESAR CIPHER

#2 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: GUI_ JAVA CAESAR CIPHER

Posted 30 October 2011 - 10:19 AM

Depending on how complicated your dialog with the user has to be when they press the button, you could use a modal JDialog (the more complicated, but it gives you more options) or a simple JOptionPane in one of its many forms.
Was This Post Helpful? 0
  • +
  • -

#3 smohd   User is offline

  • Critical Section
  • member icon


Reputation: 1825
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: GUI_ JAVA CAESAR CIPHER

Posted 30 October 2011 - 10:20 AM

Look at GUI part of pbl tutorial about Caesar Cipher it may be helpful.

Quote

how do you add a new frame to come up when they press "Encrypt" or "Decrypt".

Have you tried showing it using setVisible()? How did you show the first one?
Was This Post Helpful? 1
  • +
  • -

#4 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: GUI_ JAVA CAESAR CIPHER

Posted 30 October 2011 - 12:21 PM

A simple JOptionDialog.showInputDiagog("Message"); would save you a lot of problems (modal, visible, ...)
Something like that
public class Cypher extends JFrame implements ActionListener {

	JButton encode = new JButton("Encode");
	JButton decode = new JButton("Decode");
	JButton exit = new JButton("Exit");
	JLabel type = new JLabel();
	JLabel translation = new JLabel();

	Cypher() {
		super("Ceasar cypher");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

		JPanel pb = new JPanel(new GridLayout(1,3));  // PanellButton
		encode.addActionListener(this);
		decode.addActionListener(this);
		exit.addActionListener(this);
		pb.add(encode);
		pb.add(decode);
		pb.add(exit);
		
		JPanel p = new JPanel(new GridLayout(3,1));
		p.add(pb);
		type.setHorizontalAlignment(SwingConstants.CENTER);
		type.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		p.add(type);
		p.add(translation);
		
		add(p);
		setVisible(true);
		pack();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		Object o = e.getSource();
		if(o == exit) {
			dispose();
			System.exit(0);
		}
		if(o == encode) {
			String str = JOptionPane.showInputDialog("Message to encode:");
			type.setText("The encoded message is");
			// process encoding of str based on radio button
			translation.setText("this is the encoded String");
		}
		else {
			String str = JOptionPane.showInputDialog("Message to decode:");
			type.setText("The decoded message is");
			// process decoding of str based on radio button
			translation.setText("this is the decoded String");
			
		}
	}
	
	public static void main(String[] args) {
		new Cypher();
	}
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1