For my chemistry class my teacher told me i could earn extra credit by making some sort of chemistry related program. I thought that this was a good idea, because i am in AP computer science after all. This is my code so far, but i cant get it to display the string i want it to, even after i press the enter key. Any help would be great. Thanks!
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;
public class window extends JPanel implements KeyListener {
private static final long serialVersionUID = 1L;
private JTextField textBox;
private String textInput;
public window() {
textInput = "";
textBox = new JTextField("Enter symbol here:", 30);
this.add(textBox);
}
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.CYAN);
g.setFont(new Font("", Font.BOLD, 20));
g.drawString("Enter element symbol in text box above.", 110, 50);
g.drawString(textInput, 100, 100);
}
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
if (code == KeyEvent.VK_ENTER) {
textInput = textBox.getText();
}
repaint();
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
}
import java.awt.Color;
import javax.swing.JFrame;
public class elementRunner {
public static void main(String[] args) {
window panel = new window();
JFrame frame = new JFrame("Element project ~ By: Harsh Patel");
frame.setBounds(100, 100, 600, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
panel.setBackground(Color.RED);
frame.setVisible(true);
frame.addKeyListener(panel);
frame.add(panel);
}
}
The problem is you are setting the listener to the JPanel
, if you set it to the JTextField
it works. I attach the code below, however, maybe JLabels would be helpful. I mean, it's easier, more structured etc. But you will have to learn how layouts work. Anyway, this is how it looks to me:
ElementRunner.java (uppercase Element).
public class ElementRunner {
public static void main(String[] args) {
MyWindow panel = new MyWindow();
panel.setBackground(Color.RED);
JFrame frame = new JFrame("Element project ~ By: Harsh Patel");
frame.setBounds(100, 100, 600, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(panel);
frame.setVisible(true);
}
}
MyWindow.java (Window was too generic, this way we know it's yours, uppercase like Element)
public class MyWindow extends JPanel implements KeyListener {
private static final long serialVersionUID = 1L;
private JTextField textBox;
private String textInput;
public MyWindow() {
textInput = "";
textBox = new JTextField("Enter symbol here:", 30);
textBox.addKeyListener(this); // This line adds the listener to the TextField
this.add(textBox);
}
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.CYAN);
g.drawString("Enter element symbol in text box above.", 110, 50);
g.drawString(textInput, 100, 100);
}
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
if (code == KeyEvent.VK_ENTER) {
textInput = textBox.getText();
}
repaint();
}
public void keyReleased(KeyEvent e) { }
public void keyTyped(KeyEvent e) { }
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.