请问这段代码哪里错了??java 谢谢

2025-01-01 15:22:11
推荐回答(2个)
回答1:

import javax.swing.*;
import java.awt.event.*;

public class Ktext {
public static class tj extends JFrame{
public tj(){
setResizable(false);
JPanel p=new JPanel();
JLabel l1=new JLabel("单价:");
p.add(l1);
final JTextField l11=new JTextField(10);
p.add(l11);
JLabel l2=new JLabel("数量:");
p.add(l2);
final JTextField l22=new JTextField(10);
p.add(l22);
JLabel l3=new JLabel("总价:");
p.add(l3);
final JTextField l33=new JTextField(10);
l33.setEditable(false);
p.add(l33);

l11.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
l11.requestFocus();
l11.selectAll();
}
});
l22.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
l22.requestFocus();
l22.selectAll();
}
});
try {
double s=Double.parseDouble(l11.getText());
int d=Integer.parseInt(l22.getText());
l33.setText(""+s*d);
l33.requestFocus();
l33.selectAll();
}catch(Exception ex){
ex.printStackTrace();
l33.setText("Error");
l33.requestFocus();
l33.selectAll();
}
getContentPane().add(p);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public static void main(String agrs[]){
tj t=new tj();
t.setSize(300,100);
t.setVisible(true);
}
}}

回答2:

package flushUIInFor;

import javax.swing.*;
import java.awt.event.*;

public class Ktext {

public static void main(String agrs[]){
tj t=new tj();
t.setSize(300,100);
t.setVisible(true);
}
}

class tj extends JFrame{
public tj(){
setResizable(false);
JPanel p=new JPanel();
JLabel l1=new JLabel("单价:");
p.add(l1);
final JTextField l11=new JTextField(10);
p.add(l11);
JLabel l2=new JLabel("数量:");
p.add(l2);
final JTextField l22=new JTextField(10);
p.add(l22);
JLabel l3=new JLabel("总价:");
p.add(l3);
final JTextField l33=new JTextField(10);
l33.setEditable(false);
p.add(l33);

l11.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
l11.requestFocus();
l11.selectAll();
}
});
l22.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
l22.requestFocus();
l22.selectAll();
}
});
try {
double s=Double.parseDouble(l11.getText());
int d=Integer.parseInt(l22.getText());
l33.setText(""+s*d);
l33.requestFocus();
l33.selectAll();
}catch(Exception ex){
ex.printStackTrace();
l33.setText("Error");
l33.requestFocus();
l33.selectAll();
}
getContentPane().add(p);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}