Skip to content

Commit f8ffc8b

Browse files
authored
Create Blue.java
1 parent 678621a commit f8ffc8b

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

Blue.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import javax.swing.*;
2+
import java.awt.*;
3+
import java.awt.event.*;
4+
5+
public class BlueDotApp {
6+
public static void main(String[] args) {
7+
JFrame frame = new JFrame("Blue Dot App");
8+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
9+
frame.setLayout(new FlowLayout());
10+
11+
JLabel label = new JLabel("Enter a number:");
12+
JTextField textField = new JTextField(10);
13+
JPanel dotPanel = new JPanel() {
14+
@Override
15+
protected void paintComponent(Graphics g) {
16+
super.paintComponent(g);
17+
g.setColor(Color.BLUE);
18+
g.fillOval(50, 50, 50, 50);
19+
}
20+
};
21+
22+
textField.addActionListener(new ActionListener() {
23+
@Override
24+
public void actionPerformed(ActionEvent e) {
25+
String input = textField.getText();
26+
try {
27+
Double.parseDouble(input);
28+
dotPanel.setBackground(Color.BLUE);
29+
} catch (NumberFormatException ex) {
30+
dotPanel.setBackground(Color.YELLOW);
31+
}
32+
}
33+
});
34+
35+
frame.add(label);
36+
frame.add(textField);
37+
frame.add(dotPanel);
38+
frame.setSize(200, 200);
39+
frame.setVisible(true);
40+
}
41+
}
42+
In this example, a JFrame contains a JLabel prompting the user to enter a number, a JTextField for input, and a custom JPanel named dotPanel that overrides its paintComponent method to draw a blue dot. When the user enters text and presses Enter, the program checks if the input is a valid number using Double.parseDouble(). If it's a number, the dot remains blue. If not, the dot changes to yellow.
43+
44+
Please make sure to run this code in a Java development environment with Swing support, as Swing is part of the Java standard library.
45+
46+
47+
48+
49+

0 commit comments

Comments
 (0)