The Artima Developer Community
Sponsored Link

Java Answers Forum
I need help with this problem...please

1 reply on 1 page. Most recent reply: Apr 11, 2005 12:24 AM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Rama

Posts: 4
Nickname: rush88
Registered: Apr, 2005

I need help with this problem...please Posted: Apr 10, 2005 7:57 PM
Reply to this message Reply
Advertisement
I am very new to java...i am to construct a frame similar to this diagram contains 2 text fields on left and 2 buttons on right.
uses the following 2 strings
originalCharacter = "abcde"
encryptionCharacter = "edcba"

when user clicks on Encrypt button your program should convert the content of original text-text field from originalCharacter to the encryptionCharacter.
result should be displayed in the encrypted text-text field.
when the user clicks on the Decrypt button it should reverse the process.

I hope this layout looks ok on posted page
_________________________________

Original Text________ _______
|________| |Encrypt|
|_______|

Encrypted Text _______ _______
|_______| |Decrypt|
|_______|

___________________________________


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.*;

public class encrypt extends JFrame {
private JPanel panel1, panel2;
//private JCheckBox checkBox1, checkBox2;
private JLabel label1, label2;
private JTextField textField1, textField2;
private JButton buttons[];
private Dimension dimension = new Dimension(100, 50);

public encrypt() {

super ("encrypt");

Container container = getContentPane();

//checkBox1 = new JCheckBox("Damn");
//checkBox2 = new JCheckBox("Damn");

panel1 = new JPanel();
panel1.setLayout(new GridLayout(2,1));
//panel1.add(checkBox1);
//panel1.add(checkBox2);

textField1 = new JTextField();
textField2 = new JTextField();
label1 = new JLabel ("Original Text:");
label2 = new JLabel ("Encrypted Text:");

panel2 = new JPanel();
panel2.setLayout(new GridLayout(2,2));
panel2.add(label1);
panel2.add(textField1);
panel2.add(label2);
panel2.add(textField2);

buttons = new JButton[2];

buttons[0] = new JButton("Encrypt");
buttons[1] = new JButton("Decrypt");

//panel3 = new JPanel();
//panel3.setLayout(new GridLayout(3,1));

ButtonHandler handler = new ButtonHandler ();
for (int i = 0; i < 2; i++) {

buttons.addActionListener(handler);
panel2.add(buttons);
}

container.setLayout(new GridLayout(1,2));
container.add(panel1);
container.add(panel2);
//container.add(panel3);

setSize(300,100);
setVisible(true);
}

public void paint(Graphics g) {
super.paint(g);

for (int i = 0; i < 2; i++) {
buttons.setSize(dimension);
}
}
//public void paint (Graphics g);
String originalCharacter = "abcdefghijklmnopqrstuvwxyz";
char reversedText[] = new char[text.length()];
for (int i = 0; i < text.length(); i++)
reversedText = text.charAt(text.length() - i - 1);
String reversedTextString = new String(reversedText);
g.drawString (reversedTextString, 20, 40);

public static void main(String args[]) {

encrypt application = new encrypt();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

private class ButtonHandler implements ActionListener {

public void actionPerformed(ActionEvent event) {

JOptionPane.showMessageDialog(encrypt.this, "You Pressed:");
event.getActionCommand();

}
}
}


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: I need help with this problem...please Posted: Apr 11, 2005 12:24 AM
Reply to this message Reply
Hint: Before and after the code put [ java ] and [ /java ] (without the empty spaces, and use other indizes than [ i ] or [ b ] because these are reserved, as you can see on the right side.

Now ... what is your question?
That someone of us creates the layout for you?
Or the events that occur when you press abutton?

If you want to go the easy way, then use a Null-Layout and set the components positions and size.

I normally use GridBagLayout for allmost everything. It is more complicated, but it makes the window resizable.


For the events:
You need 2 different action handlers for the buttons (or you use the ActionCommand String for determining what should be done): Give every button a unique actioncommand, then in the handler check what action command is stored in the event (use the equals method, == doesn't work for Strings.)
Where are your crypt / decrypt methods?

To get Text from a Textfield use the getText() method.
To set text to a textfield use setText(String).

Flat View: This topic has 1 reply on 1 page
Topic: Isolate/remove parts of a String Previous Topic   Next Topic Topic: running applets

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use