java编写一个字母大小写转换器?

Python011

java编写一个字母大小写转换器?,第1张

import java.awt.*

import java.awt.event.*

import javax.swing.*

public class CaseConverter extends JFrame {

private JTextField inputField

private JTextField outputField

private JButton capitalButton

private JButton lowercaseButton

public CaseConverter() {

// 设置窗口标题

setTitle("Case Converter")

// 设置窗口布局为FlowLayout

setLayout(new FlowLayout())

// 创建组件

inputField = new JTextField(20)

outputField = new JTextField(20)

capitalButton = new JButton("Capital")

lowercaseButton = new JButton("Lowercase")

// 将组件添加到窗口中

add(inputField)

add(outputField)

add(capitalButton)

add(lowercaseButton)

// 向按钮添加按压事件监听器

capitalButton.addActionListener(new CapitalListener())

lowercaseButton.addActionListener(new LowercaseListener())

}

// 定义Capital按钮的按压事件监听器

private class CapitalListener implements ActionListener {

public void actionPerformed(ActionEvent e) {

// 从输入文本框中获取输入字符串

String input = inputField.getText()

// 将字符串中的大写字母转换为小写字母,其它字符不变

String output = input.toLowerCase()

最主要的是用对方法,Integer中有个public static String toHexString(int i)方法,它的含义是以十六进制的整型形式返回一个整型的字符串表示形式;它是静态的,所以直接用Integer.toHexString(int i) import java.awt.*import java.awt.event.*class zhuan extends Frame implements ActionListener{ TextField t1,t2Label lButton bString szhuan(){ super("将十进制转换为十六进制")t1=new TextField(10)t2=new TextField(10)t2.setEditable(false)b=new Button("转换")l=new Label("请输入一位十进制数:")l.setForeground(Color.red)b.setForeground(Color.red)setLayout(new FlowLayout())add(l)add(t1)add(b)add(t2)b.addActionListener(this)setVisible(true)setBounds(300,300,400,80)addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent ee){ System.exit(0)} })} public void actionPerformed(ActionEvent e){ int a=Integer.parseInt(t1.getText())s=Integer.toHexString(a)t2.setText(s)} } public class zhuanhuan { public static void main(String [] age){ new zhuan()} }