I’m making a calculator GUI which is only a easy one with easy capabilities. Proper now I can not even implement operators and not using a bug. My JButtons can solely be seen when going into fullscreen mode. Right here is my code:
`bundle com.firm;
import javax.swing.*;
import java.awt.*;
import java.awt.occasion.KeyEvent;
import java.awt.occasion.KeyListener;
import java.awt.picture.BufferedImage;
import java.util.ArrayList;
public class Physique extends JPanel implements Runnable, KeyListener {
//FIELDS
non-public static int WIDTH = 450;
non-public static int HEIGHT = 700;
non-public Thread thread;
non-public BufferedImage picture;
non-public Graphics2D g;
non-public static ArrayList<JButton> numberButtons = new ArrayList<>();
non-public StringBuilder present = new StringBuilder();
Physique() {
tremendous();
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setFocusable(true);
setBackground(Coloration.GREEN);
requestFocus();
requestFocusInWindow();
}
public void addNotify() {
addKeyListener(this);
tremendous.addNotify();
if (thread == null) {
thread = new Thread(this);
thread.begin();
}
requestFocusInWindow();
}
non-public void initButtons(int buttonWidth, int buttonHeight, int x, int y) {
int n = 1;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
JButton b = new JButton();
b.setFont(new Font("Century Gothic", Font.BOLD, 18));
b.setText(String.valueOf(n));
b.setForeground(Coloration.GREEN);
b.setBackground(new Coloration(0, 60, 70));
b.setFocusPainted(false);
b.setBounds(x, y, buttonWidth, buttonHeight);
b.addActionListener(e -> {
present.append(b.getText());
});
add(b);
numberButtons.add(b);
n++;
x += buttonWidth;
}
x -= (buttonWidth * 3);
y += buttonHeight;
}
JButton button0 = new JButton("0");
button0.setFont(new Font("Century Gothic", Font.BOLD, 18));
button0.setForeground(Coloration.GREEN);
button0.setBackground(new Coloration(0, 60, 70));
button0.setFocusPainted(false);
button0.setBounds(x + buttonWidth, y, buttonWidth, buttonHeight);
add(button0);
numberButtons.add(button0);
}
public void run() {
present.append("0");
picture = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
g = (Graphics2D) picture.getGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
int width = 60, peak = 40, x = 135, y = 250;
initButtons(width, peak, x, y);
whereas (true) {
replace();
render();
draw();
}
}
non-public void replace() {
if (present.toString().size() > 1 && present.charAt(0) == '0') {
present.substitute(0, 1, "");
}
}
non-public void render() {
g.setPaint(new GradientPaint(100, 100, Coloration.WHITE, 450, 700, Coloration.GREEN));
g.fillRect(0, 0, 450, 700);
g.setPaint(new GradientPaint(0, 50, Coloration.MAGENTA, 450, 50, Coloration.MAGENTA));
g.setStroke(new BasicStroke(5));
g.drawRoundRect(4, 50, 440, 60, 20, 20);
g.setColor(Coloration.DARK_GRAY);
g.setFont(new Font("Courier New", Font.BOLD, 25));
if (present.toString().size() >= 28) {
String s = present.toString().substring(present.toString().size() - 28);
g.drawString(s, 440 - (s.size() * 15), 100);
} else {
g.drawString(present.toString(), 440 - (present.toString().size() * 15), 100);
}
}
non-public void draw() {
Graphics g2 = this.getGraphics();
g2.drawImage(picture, 0, 0, null);
g2.dispose();
}
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
change (e.getKeyCode()) {
case KeyEvent.VK_0:
present.append('0');
break;
case KeyEvent.VK_1:
present.append('1');
break;
case KeyEvent.VK_2:
present.append('2');
break;
case KeyEvent.VK_3:
present.append('3');
break;
case KeyEvent.VK_4:
present.append('4');
break;
case KeyEvent.VK_5:
present.append('5');
break;
case KeyEvent.VK_6:
present.append('6');
break;
case KeyEvent.VK_7:
present.append('7');
break;
case KeyEvent.VK_8:
present.append('8');
break;
case KeyEvent.VK_9:
present.append('9');
break;
}
}
public void keyReleased(KeyEvent e) {
}
}
`
And that is the principle class:
bundle com.firm;
import javax.swing.*;
public class Primary {
public static void predominant(String[] args) {
JFrame window = new JFrame("Calculator"); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setContentPane(new Physique());
window.pack();
window.setResizable(true);
window.setVisible(true);
}
}