|
Java GUI Discussions specific to programming Java GUI. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Java GUI section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
March 25th, 2008, 02:21 AM
|
Registered User
|
|
Join Date: Mar 2008
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Problem With RSA Interface
Program complies and the interface shows up but there is no output
after pressing run button. How come this is happening?
Thanks.
Code:
package newpackage4;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.io.*;
public class NewClass5 extends JFrame{
public NewClass5() {
initiateComponents();
}
private JLabel TitleLabel;
private JPanel innerPanel;
private JLabel StringInputLabel;
private JTextField InputStringTextField;
private JButton RunButton;
private JButton SaveButton;
private JButton ResetButton;
private JButton ExitButton;
private JTextArea OutputWindow;
private char[] plainText;
private void initiateComponents() {
getContentPane().setLayout(null);
setTitle("Simplified Simulation: RSA Security Algorithm");
setSize(new Dimension(1025, 703));
setLocation(0,0);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().setBackground(new Color(100,200,200));
getContentPane().add(getInnerPanel());
getContentPane().add(getTitleLabel());
populateInnerPanel();
}
private JLabel getTitleLabel() {
TitleLabel = new JLabel();
TitleLabel.setFont(new Font("Arial",1,44));
TitleLabel.setText(" RSA Algorithm");
TitleLabel.setBackground(new Color(255,255,255));
TitleLabel.setBorder(new EtchedBorder());
TitleLabel.setBounds(300,40,500,60);
return TitleLabel;
}
private JPanel getInnerPanel() {
innerPanel = new JPanel();
innerPanel.setLayout(null);
innerPanel.setBackground(new Color(190,255,255));
innerPanel.setBorder(new TitledBorder(
new TitledBorder(null, "",
TitledBorder.DEFAULT_JUSTIFICATION,
TitledBorder.DEFAULT_POSITION,
new Font("Times New Roman", 1, 12))));
innerPanel.setBounds(40,160,930,460);
return innerPanel;
}
private void populateInnerPanel() {
StringInputLabel = new JLabel();
StringInputLabel.setFont(new Font("Bookman Old Style",0,13));
StringInputLabel.setText("Enter in a word:");
innerPanel.add(StringInputLabel);
StringInputLabel.setBounds(65,20,110,20);
InputStringTextField = new JTextField();
InputStringTextField.setText(" ");
innerPanel.add(InputStringTextField);
InputStringTextField.setBounds(180,20,220,20);
RunButton = new JButton();
RunButton.setFont(new Font("Bookman Old Style",1,13));
RunButton.setText("Run");
innerPanel.add(RunButton);
RunButton.setBounds(65,420,70,20);
SaveButton = new JButton();
SaveButton.setFont(new Font("Bookman Old Style",1,13));
SaveButton.setText("Save");
innerPanel.add(SaveButton);
SaveButton.setBounds(305,420,70,20);
ResetButton = new JButton();
ResetButton.setFont(new Font("Bookman Old Style",1,13));
ResetButton.setText("Reset");
innerPanel.add(ResetButton);
ResetButton.setBounds(555,420,80,20);
ExitButton = new JButton();
ExitButton.setFont(new Font("Bookman Old Style",1,13));
ExitButton.setText("Exit");
innerPanel.add(ExitButton);
ExitButton.setBounds(794,420,70,20);
OutputWindow = new JTextArea();
Font equalSpacedFont = new Font("Monospaced",Font.PLAIN,14);
OutputWindow.setFont(equalSpacedFont);
OutputWindow.setEditable(false);
JScrollPane scrollPane = new JScrollPane(OutputWindow);
scrollPane.setBounds(65,70,800,330);
innerPanel.add(scrollPane);
}
public void actionPerformed(ActionEvent event) {
String arg = event.getActionCommand();
if(arg.equals("Run"))
{
try
{
int plainTextLength = plainText.length;
String inputString = InputStringTextField.getText();
int inputStringLength = inputString.length();
char[] result = new char[inputStringLength];
//char currentSource = inputString.charAt(i);
}
catch (Exception e) {
}
int [] cipherText = new int[plainText.length];
System.out.print("\nPlaintext:\t ");
for(int i = 0; i < plainText.length; i++)
{
int tmp = (int)plainText[i]-64;
cipherText[i] = (tmp*tmp*tmp)%33;
System.out.print("\t"+plainText[i]);
}
System.out.print("\nCiphertext:\t");
for(int i = 0; i < cipherText.length; i++)
{
System.out.print("\t"+cipherText[i]);
}
}
}
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame.setDefaultLookAndFeelDecorated(true);
NewClass5 nc5 = new NewClass5();
nc5.setVisible(true);
}
});
}
|
March 25th, 2008, 06:14 AM
|
Friend of Wrox
|
|
Join Date: Mar 2007
Posts: 373
Thanks: 0
Thanked 1 Time in 1 Post
|
|
You need process button click events and write your own code. You need to use EventListeners.
Try the following code.
Code:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
public class NewClass5 extends JFrame {
private static final long serialVersionUID = 1L;
public NewClass5() {
initiateComponents();
}
private JLabel TitleLabel;
private JPanel innerPanel;
private JLabel StringInputLabel;
private static JTextField InputStringTextField;
private JButton RunButton;
private JButton SaveButton;
private JButton ResetButton;
private JButton ExitButton;
private JTextArea OutputWindow;
private void initiateComponents() {
getContentPane().setLayout(null);
setTitle("Simplified Simulation: RSA Security Algorithm");
setSize(new Dimension(1025, 703));
setLocation(0, 0);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().setBackground(new Color(100, 200, 200));
getContentPane().add(getInnerPanel());
getContentPane().add(getTitleLabel());
populateInnerPanel();
}
private JLabel getTitleLabel() {
TitleLabel = new JLabel();
TitleLabel.setFont(new Font("Arial", 1, 44));
TitleLabel.setText(" RSA Algorithm");
TitleLabel.setBackground(new Color(255, 255, 255));
TitleLabel.setBorder(new EtchedBorder());
TitleLabel.setBounds(300, 40, 500, 60);
return TitleLabel;
}
private JPanel getInnerPanel() {
innerPanel = new JPanel();
innerPanel.setLayout(null);
innerPanel.setBackground(new Color(190, 255, 255));
innerPanel.setBorder(new TitledBorder(new TitledBorder(null, "",
TitledBorder.DEFAULT_JUSTIFICATION,
TitledBorder.DEFAULT_POSITION, new Font("Times New Roman", 1,
12))));
innerPanel.setBounds(40, 160, 930, 460);
return innerPanel;
}
private void populateInnerPanel() {
StringInputLabel = new JLabel();
StringInputLabel.setFont(new Font("Bookman Old Style", 0, 13));
StringInputLabel.setText("Enter in a word:");
innerPanel.add(StringInputLabel);
StringInputLabel.setBounds(65, 20, 110, 20);
InputStringTextField = new JTextField();
innerPanel.add(InputStringTextField);
InputStringTextField.setBounds(180, 20, 220, 20);
RunButton = new JButton();
RunButton.setFont(new Font("Bookman Old Style", 1, 13));
RunButton.setText("Run");
innerPanel.add(RunButton);
RunButton.setBounds(65, 420, 70, 20);
SaveButton = new JButton();
SaveButton.setFont(new Font("Bookman Old Style", 1, 13));
SaveButton.setText("Save");
innerPanel.add(SaveButton);
SaveButton.setBounds(305, 420, 70, 20);
ResetButton = new JButton();
ResetButton.setFont(new Font("Bookman Old Style", 1, 13));
ResetButton.setText("Reset");
innerPanel.add(ResetButton);
ResetButton.setBounds(555, 420, 80, 20);
ExitButton = new JButton();
ExitButton.setFont(new Font("Bookman Old Style", 1, 13));
ExitButton.setText("Exit");
innerPanel.add(ExitButton);
ExitButton.setBounds(794, 420, 70, 20);
OutputWindow = new JTextArea();
Font equalSpacedFont = new Font("Monospaced", Font.PLAIN, 14);
OutputWindow.setFont(equalSpacedFont);
OutputWindow.setEditable(false);
JScrollPane scrollPane = new JScrollPane(OutputWindow);
scrollPane.setBounds(65, 70, 800, 330);
innerPanel.add(scrollPane);
// add event listeners
ButtonListener aListener = new ButtonListener();
ResetButton.addActionListener(aListener);
ExitButton.addActionListener(aListener);
RunButton.addActionListener(aListener);
}
static int[] encryptText(char[] plainText) {
int[] encText = new int[plainText.length];
for (int i = 0; i < plainText.length; i++) {
int tmp = (int) plainText[i] - 64;
encText[i] = (tmp * tmp * tmp) % 33;
}
return encText;
}
private void printText(char[] plainText, int[] encText) {
if(plainText == null || encText == null) {
clearText();
return;
}
StringBuffer sb = new StringBuffer();
sb.append("\nPlain Text:\t ");
for (int i = 0; i < plainText.length; i++) {
sb.append("\t" + plainText[i]);
}
sb.append("\nEncripted Text:\t");
for (int i = 0; i < encText.length; i++) {
sb.append("\t" + encText[i]);
}
OutputWindow.setText(sb.toString());
}
private void clearText() {
InputStringTextField.setText("");
OutputWindow.setText("");
}
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame.setDefaultLookAndFeelDecorated(true);
NewClass5 nc5 = new NewClass5();
nc5.setVisible(true);
}
});
}
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == ExitButton) {
System.exit(0);
} else if (source == ResetButton) {
clearText();
} else if(source == RunButton) {
char[] plainText = InputStringTextField.getText().toUpperCase().toCharArray();
printText(plainText, encryptText(plainText));
}
}
}
}
- Rakesh
|
|
|