我已经在这个程序上添加了一段时间,现在从一个简单的计算器慢慢开发它,但现在程序没有错误但是没有给我想要的输出。它应该改变颜色为蓝色,但不会。我添加了resultPane.setForeground(Color.BLUE);但不会更新JLabel
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);
}
});
}
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 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) {
resultPane.setForeground(Color.BLUE);
}
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){
}
}
}
答案 0 :(得分:3)
在MouseListener
{}设置JPanel
的前景色,而不是JLabel
&#39>:
public void mouseClicked(MouseEvent event) {
resultPane.setForeground(Color.BLUE); // This is the problem
}
resultPane
是您的自定义JPanel。您需要更改它设置此自定义面板中包含的JLabel的前景色。我只想向ResultPane
添加一个方法,如:
public void setLabelForeground(Color color) {
result.setForeground(color);
}
然后在resultPane.setLabelForeground(Color.BLUE);
中调用MouseListener
。
另外,不确定这是否是你想要做的,但是当你点击JLabel时,这仍然只会改变前景色......?