JTextFields和我的学校项目

时间:2016-05-17 22:57:57

标签: java graphics

对于我的化学课,我的老师告诉我,通过制作某种与化学相关的课程,我可以获得额外的学分。我认为这是一个好主意,因为我毕竟是A.P.计算机科学。到目前为止这是我的代码,但即使我按下回车键,我也无法显示我想要的字符串。任何帮助都会很棒。谢谢!

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);
    }
}

1 个答案:

答案 0 :(得分:0)

问题是您是否正在将监听器设置为JPanel,如果您将其设置为JTextField它的工作原理。我附上下面的代码,但是,JLabel可能会有所帮助。我的意思是,它更容易,更有条理等等。但您必须了解布局的工作原理。无论如何,这就是我的看法:

ElementRunner.java (大写元素)。

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 (窗口过于通用,这种方式我们知道它是你的,像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) { }
}