发布网友 发布时间:2022-04-23 21:26
共2个回答
热心网友 时间:2023-10-10 03:00
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class Test extends JApplet implements ActionListener {
private static final long serialVersionUID = 1L;
private JTextField jtfD;
private JTextField jtfU;
private JButton btnUpper;
public void init() {
Container con = this.getContentPane();
jtfD = new JTextField();
jtfU = new JTextField();
btnUpper = new JButton("Upper");
Box b1 = Box.createHorizontalBox();
b1.add(Box.createHorizontalStrut(10));
b1.add(new JLabel("小写"));
b1.add(jtfD);
b1.add(Box.createHorizontalStrut(10));
Box b2 = Box.createHorizontalBox();
b2.add(Box.createHorizontalStrut(10));
b2.add(new JLabel("大写"));
b2.add(jtfU);
b2.add(Box.createHorizontalStrut(10));
Box b3 = Box.createHorizontalBox();
b3.add(Box.createHorizontalStrut(10));
b3.add(btnUpper);
Box b = Box.createVerticalBox();
b.add(Box.createVerticalStrut(30));
b.add(b1);
b.add(Box.createVerticalStrut(30));
b.add(b2);
b.add(Box.createVerticalStrut(30));
b.add(b3);
con.add(b);
btnUpper.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
try {
String str = jtfD.getText();
str = str.toUpperCase();
jtfU.setText(str);
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex);
}
}
}
热心网友 时间:2023-10-10 03:00
添加一个textfield
取得它的值,赋给String 类型的str
然后str=str.toUpperCase();
就把小写转换成大写咯