මේ තියෙන්නේ අපි type කරන ඕනෑම number එකක් text වලට convert කරන්න class එකක්. මෙහි ඇති convert() යන method එක භාවිතා කරමින් 0 සිට 999 999 999 999 ඕනෑම සංඛ්යාවක් වචන බවට හරවා ගන්න පුළුවන්. මෙහි ඇති convert method එකට අපට අවශ්ය අංකය String ආකාරයට හෝ long ආකාරයට ලබා දීමෙන් මෙම කාර්යය කරගත හැක.
Posted by
Unknown
at
4:43 AM
මේ තියෙන්නේ අපට අවශ්ය ඕනෑම වචනයක අපට අවශ්ය ඕනෑම තැනකින් කැබලි වලට කඩාගන්න විදියයි. ඊට පාවිච්චි කරන්නේ String class එකේ ඇති split() කියන method එකයි. මෙම method එකේ parameters ලෙස අපට වචනය කැබලි වලට කඩා ගත යුතු ස්ථානයට අදාළ text එක ලබා දිය යුතුයි. පහත ඇත්තේ ඊට අදාල code example එකයි.
public class SplitText {
public static void main(String[] args) {
String text = "split this text";
String[] split = text.split("");
for (int i = 0; i < split.length; i++) {
System.out.println(split[i]);
}
}
}
Output : s
p
l
i
t
t
h
i
s
t
e
x
t
public class SplitText {
public static void main(String[] args) {
String text = "split this text";
String[] split = text.split("");
for (int i = 0; i < split.length; i++) {
System.out.println(split[i]);
}
}
}
Output : s
p
l
i
t
t
h
i
s
t
e
x
t
Posted by
Unknown
at
4:34 AM
අද මම කියාදෙන්න යන්නේ ඕනෑම වචනයක් reverse එකට හරවන්නේ කොහොමද කියලයි. ඒ කියන්නේ වචනය අග සිට මුලට ලියන්නේ කොහොමද කියලයි. ඒකට අපට උවමනා වෙනවා StringBuilder කියන class එක. එහි තියෙන reverse() කියන method එකෙන් මේ වැඩේ පහසුවෙන්ම කරගන්න පුළුවන්. පහතින් තියෙන්නේ ඊට අදාල code example එකක්.
public class ReveseText {
public static void main(String[] args) {
String direct="Reverse this text";
StringBuilder builder=new StringBuilder(direct);
StringBuilder reversed = builder.reverse();
System.out.println(reversed);
}
}
Output : txet siht esreveR
public class ReveseText {
public static void main(String[] args) {
String direct="Reverse this text";
StringBuilder builder=new StringBuilder(direct);
StringBuilder reversed = builder.reverse();
System.out.println(reversed);
}
}
Output : txet siht esreveR
package lookandfeel;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class LookAndFeelDemo extends JFrame {
public LookAndFeelDemo() {
initComponents();
}
public void initComponents() {
setSize(200, 200);
setTitle("LAF Demo");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT));
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("Look and Feel");
final JFrame frame = this;
//
// Get all the available look and feel that we are going to use for
// creating the JMenuItem and assign the action listener to handle
// the selection of menu item to change the look and feel.
//
UIManager.LookAndFeelInfo[] lookAndFeelInfos = UIManager.getInstalledLookAndFeels();
for (int i = 0; i < lookAndFeelInfos.length; i++) {
final UIManager.LookAndFeelInfo lookAndFeelInfo = lookAndFeelInfos[i];
JMenuItem item = new JMenuItem(lookAndFeelInfo.getName());
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
//
// Set the look and feel for the frame and update the UI
// to use a new selected look and feel.
//
UIManager.setLookAndFeel(lookAndFeelInfo.getClassName());
SwingUtilities.updateComponentTreeUI(frame);
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
} catch (InstantiationException e1) {
e1.printStackTrace();
} catch (IllegalAccessException e1) {
e1.printStackTrace();
} catch (UnsupportedLookAndFeelException e1) {
e1.printStackTrace();
}
}
});
menu.add(item);
}
menuBar.add(menu);
getContentPane().add(menuBar);
getContentPane().add(new JButton("Hello"));
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new LookAndFeelDemo().setVisible(true);
}
});
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class LookAndFeelDemo extends JFrame {
public LookAndFeelDemo() {
initComponents();
}
public void initComponents() {
setSize(200, 200);
setTitle("LAF Demo");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT));
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("Look and Feel");
final JFrame frame = this;
//
// Get all the available look and feel that we are going to use for
// creating the JMenuItem and assign the action listener to handle
// the selection of menu item to change the look and feel.
//
UIManager.LookAndFeelInfo[] lookAndFeelInfos = UIManager.getInstalledLookAndFeels();
for (int i = 0; i < lookAndFeelInfos.length; i++) {
final UIManager.LookAndFeelInfo lookAndFeelInfo = lookAndFeelInfos[i];
JMenuItem item = new JMenuItem(lookAndFeelInfo.getName());
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
//
// Set the look and feel for the frame and update the UI
// to use a new selected look and feel.
//
UIManager.setLookAndFeel(lookAndFeelInfo.getClassName());
SwingUtilities.updateComponentTreeUI(frame);
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
} catch (InstantiationException e1) {
e1.printStackTrace();
} catch (IllegalAccessException e1) {
e1.printStackTrace();
} catch (UnsupportedLookAndFeelException e1) {
e1.printStackTrace();
}
}
});
menu.add(item);
}
menuBar.add(menu);
getContentPane().add(menuBar);
getContentPane().add(new JButton("Hello"));
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new LookAndFeelDemo().setVisible(true);
}
});
}
}
Posted by
Unknown
at
11:41 AM
Posted by
Unknown
at
10:59 AM
මේ තියෙන්නේ text විතරක් වදින textfield එකක් හදා ගන්න හැටියි. මේකෙදි textfield එකේ numbers type කරන විට බීප් ශබ්දයක් සහ නැගෙන අතර numbers type නොවේ..... try කරලා බලන්න.. :-)
private void jTextField1KeyTyped(java.awt.event.KeyEvent evt) {
try {
char keyChar = evt.getKeyChar();
if (Character.isDigit(keyChar)) {
Toolkit.getDefaultToolkit().beep();
evt.consume();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
private void jTextField1KeyTyped(java.awt.event.KeyEvent evt) {
try {
char keyChar = evt.getKeyChar();
if (Character.isDigit(keyChar)) {
Toolkit.getDefaultToolkit().beep();
evt.consume();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
Posted by
Unknown
at
2:13 AM
Subscribe to:
Posts (Atom)