//就是简单实现一个进制转换,然后一旦输入任何数,不管哪个框,都是错。然后我刚才才去学了下正则表达式,我一开始以为是这里的错,注释掉后发现还是错。求好心人帮忙看下问题在哪,还有就是注释掉那部分有错吗(毕竟刚学)?
package TransNumSys;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class TransNumSys extends JFrame{
private static String dValue;
private static String hValue;
private static String bValue;
private static String oValue;
private JTextField tf_d = new JTextField();
private JTextField tf_h = new JTextField();
private JTextField tf_b = new JTextField();
private JTextField tf_o = new JTextField();
public static void main(String[] args){
// TODO Auto-generated method stub
JOptionPane.showMessageDialog(null,"在任一文本框中输入数字后,按回车","提示",JOptionPane.INFORMATION_MESSAGE);
TransNumSys frame = new TransNumSys();
frame.setSize(400,300);
frame.setTitle("进制转换器");
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public TransNumSys(){
JPanel panel = new JPanel(new GridLayout(4, 2));
JLabel lb1 = new JLabel("十进制",SwingConstants.CENTER);
JLabel lb2 = new JLabel("十六进制",SwingConstants.CENTER);
JLabel lb3 = new JLabel("二进制",SwingConstants.CENTER);
JLabel lb4 = new JLabel("八进制",SwingConstants.CENTER);
tf_d.addActionListener(new TransAction());
tf_h.addActionListener(new TransAction());
tf_b.addActionListener(new TransAction());
tf_o.addActionListener(new TransAction());
panel.add(lb1);
panel.add(tf_d);
panel.add(lb2);
panel.add(tf_h);
panel.add(lb3);
panel.add(tf_b);
panel.add(lb4);
panel.add(tf_o);
add(panel);
}
class TransAction implements ActionListener{
public void actionPerformed(ActionEvent e){
String src = e.getSource().toString();
switch (src) {
case "tf_d":
dValue = tf_d.getText();
break;
case "tf_h":
hValue = tf_h.getText();
break;
case "tf_b":
bValue = tf_b.getText();
break;
case "tf_o":
oValue = tf_o.getText();
break;
}
// Pattern patternB = Pattern.compile("[01]+");
// Matcher matcherB = patternB.matcher(hValue);
// if(!matcherB.matches()){
// JOptionPane.showMessageDialog(null, "你输入越界了吧?");
// return;
// }
// Pattern patternO = Pattern.compile("[0-7]+");
// Matcher matcherO = patternO.matcher(hValue);
// if(!matcherO.matches()){
// JOptionPane.showMessageDialog(null, "你输入越界了吧?");
// return;
// }
dValue = Integer.valueOf(dValue, 10).toString();
hValue = Integer.toHexString(Integer.parseInt(dValue));
bValue = Integer.toBinaryString(Integer.parseInt(dValue));
oValue = Integer.toOctalString(Integer.parseInt(dValue));
tf_d.setText(dValue);
tf_h.setText(hValue);
tf_b.setText(bValue);
tf_o.setText(oValue);
}
}
}
package TransNumSys;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class TransNumSys extends JFrame{
private static String dValue;
private static String hValue;
private static String bValue;
private static String oValue;
private JTextField tf_d = new JTextField();
private JTextField tf_h = new JTextField();
private JTextField tf_b = new JTextField();
private JTextField tf_o = new JTextField();
public static void main(String[] args){
// TODO Auto-generated method stub
JOptionPane.showMessageDialog(null,"在任一文本框中输入数字后,按回车","提示",JOptionPane.INFORMATION_MESSAGE);
TransNumSys frame = new TransNumSys();
frame.setSize(400,300);
frame.setTitle("进制转换器");
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public TransNumSys(){
JPanel panel = new JPanel(new GridLayout(4, 2));
JLabel lb1 = new JLabel("十进制",SwingConstants.CENTER);
JLabel lb2 = new JLabel("十六进制",SwingConstants.CENTER);
JLabel lb3 = new JLabel("二进制",SwingConstants.CENTER);
JLabel lb4 = new JLabel("八进制",SwingConstants.CENTER);
tf_d.addActionListener(new TransAction());
tf_h.addActionListener(new TransAction());
tf_b.addActionListener(new TransAction());
tf_o.addActionListener(new TransAction());
panel.add(lb1);
panel.add(tf_d);
panel.add(lb2);
panel.add(tf_h);
panel.add(lb3);
panel.add(tf_b);
panel.add(lb4);
panel.add(tf_o);
add(panel);
}
class TransAction implements ActionListener{
public void actionPerformed(ActionEvent e){
String src = e.getSource().toString();
switch (src) {
case "tf_d":
dValue = tf_d.getText();
break;
case "tf_h":
hValue = tf_h.getText();
break;
case "tf_b":
bValue = tf_b.getText();
break;
case "tf_o":
oValue = tf_o.getText();
break;
}
// Pattern patternB = Pattern.compile("[01]+");
// Matcher matcherB = patternB.matcher(hValue);
// if(!matcherB.matches()){
// JOptionPane.showMessageDialog(null, "你输入越界了吧?");
// return;
// }
// Pattern patternO = Pattern.compile("[0-7]+");
// Matcher matcherO = patternO.matcher(hValue);
// if(!matcherO.matches()){
// JOptionPane.showMessageDialog(null, "你输入越界了吧?");
// return;
// }
dValue = Integer.valueOf(dValue, 10).toString();
hValue = Integer.toHexString(Integer.parseInt(dValue));
bValue = Integer.toBinaryString(Integer.parseInt(dValue));
oValue = Integer.toOctalString(Integer.parseInt(dValue));
tf_d.setText(dValue);
tf_h.setText(hValue);
tf_b.setText(bValue);
tf_o.setText(oValue);
}
}
}