Bài viết này cung cấp một mã nguồn giao diện ứng dụng (GUI) Java JApplet dùng để chuyển đổi văn bản từ chữ hoa sang chữ thường (convert text from lower case to upper case). Một bài mẫu đơn giản cho bạn mới học lập trình giao diện với Java. |
Java Source - Mã Nguồn Java:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
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 {
@Override
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
}
}
}
import java.awt.event.*;
import javax.swing.*;
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 {
@Override
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
}
}
}
Result - Kết Quả:
Source: Lee Point
No comments:
Post a Comment