从long到int可能有损耗的转换?
我有这个程序几乎是一个计算器,但有一个移动的JLabel,它应该在每次点击标签时都会改变颜色,但是我在代码的最底部有3个错误,我标注了一条评论。 所有三个都是:错误:不兼容的类型:从long到int的可能有损转换
public class TestCalculator {
private ResultPane resultPane;
public static void main(String[] args) {
new TestCalculator();
}
public TestCalculator() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
resultPane = new ResultPane();
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setGlassPane(resultPane);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new CalculatorPane(resultPane));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setBounds(200,200,500,400);
}
});
}
public class ResultPane extends JPanel {
private JLabel result;
private Timer timer;
private int xDelta = (Math.random() > 0.5) ? 1 : -1;
private int yDelta = (Math.random() > 0.5) ? 1 : -1;
public void setLabelForeground(Color color) {
result.setForeground(color);
}
public ResultPane() {
setOpaque(false);
setLayout(null);
result = new JLabel();
Font font = result.getFont();
font = font.deriveFont(Font.BOLD, 26f);
result.setFont(font);
add(result);
HandlerClass handler = new HandlerClass();
result.addMouseListener(handler);
result.addMouseMotionListener(handler);
timer = new Timer(40, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Point point = result.getLocation();
point.x += xDelta;
point.y += yDelta;
if (point.x < 0) {
point.x = 0;
xDelta *= -1;
} else if (point.x + result.getWidth() > getWidth()) {
point.x = getWidth() - result.getWidth();
xDelta *= -1;
}
if (point.y < 0) {
point.y = 0;
yDelta *= -1;
} else if (point.y + result.getHeight() > getHeight()) {
point.y = getHeight() - result.getHeight();
yDelta *= -1;
}
result.setLocation(point);
repaint();
}
});
timer.start();
}
public void setResult(Number number) {
result.setText(NumberFormat.getNumberInstance().format(number));
result.setSize(result.getPreferredSize());
setVisible(true);
}
public String getResultText() {
return result.getText();
}
}
public class CalculatorPane extends JPanel {
private final ResultPane resultPane;
private final JLabel firstNumberLabel = new JLabel("First Number:");
private final JLabel secondNumberLabel = new JLabel("Second Number:");
private final JTextField firstNumberField = new JTextField(5);
private final JTextField secondNumberField = new JTextField(5);
public double result = 0.0;
public CalculatorPane(ResultPane resultPane) {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
this.resultPane = resultPane;
JPanel fields = new JPanel();
fields.add(firstNumberLabel);
fields.add(firstNumberField);
fields.add(secondNumberLabel);
fields.add(secondNumberField);
add(fields, gbc);
JPanel buttons = new JPanel();
buttons.add(new JButton(new AddAction()));
buttons.add(new JButton(new SubtractAction()));
buttons.add(new JButton(new MultiplyAction()));
buttons.add(new JButton(new DivideAction()));
add(buttons, gbc);
}
public class AddAction extends AbstractAction {
public AddAction() {
putValue(NAME, "+");
}
@Override
public void actionPerformed(ActionEvent e) {
try {
double num1 = Double.parseDouble(firstNumberField.getText());
double num2 = Double.parseDouble(secondNumberField.getText());
double result = num1 + num2;
resultPane.setResult(result);
String resultText = resultPane.getResultText();
firstNumberField.setText(resultText);
} catch (NumberFormatException exp) {
}
}
}
public class SubtractAction extends AbstractAction {
public SubtractAction() {
putValue(NAME, "-");
}
@Override
public void actionPerformed(ActionEvent e) {
try {
double num1 = Double.parseDouble(firstNumberField.getText());
double num2 = Double.parseDouble(secondNumberField.getText());
double result = num1 - num2;
resultPane.setResult(result);
String resultText = resultPane.getResultText();
firstNumberField.setText(resultText);
} catch (NumberFormatException exp) {
}
}
}
public class MultiplyAction extends AbstractAction {
public MultiplyAction() {
putValue(NAME, "x");
}
@Override
public void actionPerformed(ActionEvent e) {
try {
double num1 = Double.parseDouble(firstNumberField.getText());
double num2 = Double.parseDouble(secondNumberField.getText());
double result = num1 * num2;
resultPane.setResult(result);
String resultText = resultPane.getResultText();
firstNumberField.setText(resultText);
} catch (NumberFormatException exp) {
}
}
}
public class DivideAction extends AbstractAction {
public DivideAction() {
putValue(NAME, "/");
}
@Override
public void actionPerformed(ActionEvent e) {
try {
double num1 = Double.parseDouble(firstNumberField.getText());
double num2 = Double.parseDouble(secondNumberField.getText());
double result = num1 / num2;
resultPane.setResult(result);
String resultText = resultPane.getResultText();
firstNumberField.setText(resultText);
} catch (NumberFormatException exp) {
}
}
}
}
private class HandlerClass implements MouseListener, MouseMotionListener {
public void mouseClicked(MouseEvent event) {
int r = Math.round(Math.random() * 255);//error here
int g = Math.round(Math.random() * 255);//here
int b = Math.round(Math.random() * 255);//and here
Color col = new Color(r, g, b);
resultPane.setLabelForeground(col);
}
public void mouseReleased(MouseEvent event) {
}
public void mousePressed(MouseEvent event) {
}
public void mouseExited(MouseEvent event) {
}
public void mouseEntered(MouseEvent event) {
}
public void mouseMoved(MouseEvent event) {
}
public void mouseDragged(MouseEvent event){
}
}
}
这是因为long
是64位, int
是32位,更不用说你正在从浮点数到整数。 从long
到int
,你将不得不放弃一些信息,编译器不能/不会自动执行。 你将不得不通过演员明确地说出这样的话:
int g = (int) Math.round(Math.random() * 255);
^^^^^
或者,您可以使用java.util.Random
:
Random r = new Random();
int g = r.nextInt(256);
通过将long
( Math.round
的结果)分配给int
,您试图让编译器提供隐式的原始缩小转换。 它不能隐含地完成; 用演员明确地做。
int r = (int) Math.round(Math.random() * 255);
Math.random()给你一个很长的时间,它可以远远超过一个整数可以表示的值的范围。 然后,您将该值分配给一个整数。
这是编译器警告你的。
http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html
链接地址: http://www.djcxy.com/p/73641.html