import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
//////////////////////////////////////////////////////////////// CombineName
public class CombineName extends JFrame {
//=================================================== instance variables
private JTextField _fNameTf = new JTextField(8);
private JTextField _lNameTf = new JTextField(8);
private JTextField _combinedNameTf = new JTextField(14);
//================================================================= main
public static void main(String[] args) {
JFrame window = new CombineName(); // Create window.
}
//========================================================== constructor
public CombineName() {
//... 1. Create or set attributes of components.
_combinedNameTf.setEditable(false); // Don't let user change output.
JButton combineBtn = new JButton("Combine");
//... 2. Add listener(s).
combineBtn.addActionListener(new CombineAction());
//... 3. Create a panel, set layout, and add components to it.
JPanel content = new JPanel();
content.setLayout(new FlowLayout());
content.add(new JLabel("First"));
content.add(_fNameTf);
content.add(new JLabel("Last"));
content.add(_lNameTf);
content.add(combineBtn);
content.add(new JLabel("Combined Name"));
content.add(_combinedNameTf);
//... 4. Set the content panel of window and perform layout.
this.setContentPane(content);
this.setTitle("CombineName Example");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack(); // Do layout.
this.setLocationRelativeTo(null); // Center window.
this.setVisible(true);
}
///////////////////////////////////// inner listener class CombineAction
class CombineAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
//... Get text from the text fields, combine, set text.
// Please make this program do something interesting.
String first = _fNameTf.getText();
String last = _lNameTf.getText();
String combined = last + ", " + first; // Trivial logic!!!
_combinedNameTf.setText(combined);
}
}
}Tuesday, 31 May 2011
combining two strings using java
Lower case to Upper case conversion in java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//////////////////////////////////////////////////////////////// ToUpperCase
public class ToUpperCase extends JApplet {
//=================================================== instance variables
private JTextField _inField = new JTextField(20);
private JTextField _outField = new JTextField(20);
//================================================================= main
public static void main(String[] args) {
JFrame window = new JFrame();
window.setTitle("ToUpperCase Example");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//... JApplet works fine as content pane in a window!
window.setContentPane(new ToUpperCase());
window.pack(); // Layout components.
window.setLocationRelativeTo(null); // Center window.
window.setVisible(true);
}
//================================================== applet constructor
public ToUpperCase() {
//... Create or set attributes of components.
_outField.setEditable(false); // Don't let user change output.
JButton toUpperButton = new JButton("To Uppercase");
//... Add listener to button.
toUpperButton.addActionListener(new UpperCaseAction());
//... Add components directly to applet. Don't need content pane.
setLayout(new FlowLayout());
add(_inField);
add(toUpperButton);
add(_outField);
}
/////////////////////////////////// inner listener class UpperCaseAction
class UpperCaseAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
//... Convert text in one textfield to uppercase in another.
String data = _inField.getText(); // Get the text
String out = data.toUpperCase(); // Create uppercase version.
_outField.setText(out); // Set output field
}
}
}Detecting Extension Filename in JAVA
import javax.swing.*;
public class FileExt {
public static void main(String[] args) {
//... Declare local variables.
String fileName; // The file name the user entered.
String extension; // The extension.
//... Input a file name and remove whitespace.
fileName = JOptionPane.showInputDialog(null, "Enter file name.");
fileName = fileName.trim();
//... Find the position of the last dot. Get extension.
int dotPos = fileName.lastIndexOf(".");
extension = fileName.substring(dotPos);
//... Output extension.
JOptionPane.showMessageDialog(null, "Extension is " + extension);
}
}Capitalizae charecters using java
import javax.swing.*;
public class Capitalize2 {
public static void main(String[] args) {
//.. Input a word
String inputWord = JOptionPane.showInputDialog(null, "Enter a word");
//.. Process - Separate word into parts, change case, put together.
String firstLetter = inputWord.substring(0,1); // Get first letter
String remainder = inputWord.substring(1); // Get remainder of word.
String capitalized = firstLetter.toUpperCase() + remainder.toLowerCase();
//.. Output the result.
JOptionPane.showMessageDialog(null, capitalized);
}
}Km to Miles conversion using swing
import javax.swing.*;
public class KmToMiles {
//============================================================ constants
private static final double MILES_PER_KILOMETER = 0.621;
//================================================================= main
public static void main(String[] args) { //Note 1
//... Local variables
String kmStr; // String km before conversion to double.
double km; // Number of kilometers.
double mi; // Number of miles.
//... Input
kmStr = JOptionPane.showInputDialog(null, "Enter kilometers.");
km = Double.parseDouble(kmStr);
//... Computation
mi = km * MILES_PER_KILOMETER;
//... Output
JOptionPane.showMessageDialog(null, km + " kilometers is " + mi + " miles.");
}
}
public class KmToMiles {
//============================================================ constants
private static final double MILES_PER_KILOMETER = 0.621;
//================================================================= main
public static void main(String[] args) { //Note 1
//... Local variables
String kmStr; // String km before conversion to double.
double km; // Number of kilometers.
double mi; // Number of miles.
//... Input
kmStr = JOptionPane.showInputDialog(null, "Enter kilometers.");
km = Double.parseDouble(kmStr);
//... Computation
mi = km * MILES_PER_KILOMETER;
//... Output
JOptionPane.showMessageDialog(null, km + " kilometers is " + mi + " miles.");
}
}
Subscribe to:
Comments (Atom)



