本文介绍了与KeyAdapter,AWT运行时错误,摆了Java。帮助发现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 您好IM写了一些$ C C I虽然它去上班,但它编译和它does not甚至抛出异常或任何东西。它还会创建图标,像它的openned但我点击它,它没有做任何事情,请需要帮助知道我做错了。继承人之类的code: 包practicagraficos8;进口java.awt中的*。java.awt.event中导入*。进口的javax.swing *。公共类ventanatexto {   公众的JFrame塔纳;   公共字符串texto;    ventanatexto(){    JFrame.setDefaultLookAndFeelDecorated(真);    texto =;    Ventana公司=新的JFrame(teclado);    PANEL1面板=新PANEL1();    ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    ventana.add(面板);     ventana.setVisible(真);    ventana.addKeyListener(新的处理程序());    }    公共类PANEL1继承JPanel {        @覆盖    公共无效漆(图形G){        super.paint(G);    DIM维=的getSize();    g.clearRect(0,0,dim.width,dim.height);    g.drawString(texto,宽,Width);    };}    类处理程序扩展KeyAdapter {        @覆盖    公共无效键pressed(KeyEvent的K){    焦炭tecla = k.getKeyChar();    开关(tecla){        壳体127:文本=;    打破;        壳体8:如果(texto.length()大于0){texto = texto.substring(0,texto.length() - 1);}            打破;        默认:            如果(texto.length()&LT; 15){texto + = tecla;}    }    ventana.repaint();    }    }} 和这里是我的主: 包practicagraficos8;公共类Practicagraficos8 {    公共静态无效的主要(字串[] args){        ventanatexto prueba =新ventanatexto();    }} 解决方案 如图所示 href=\"http://stackoverflow.com/a/11924118/230513\">, drawString之()预计坐标重新present的的基线的字符串。 的FontMetrics FM = g.getFontMetrics();g.drawString(texto,0,fm.getAscent()); 在此外, 使用的JTextComponent 为可编辑的文本。 使用键绑定,而不是的KeyListener 。 使用包()在封闭窗口。 Swing程序应该重写的paintComponent()而不是覆盖的paint()。 - 绘画在AWT和Swing:色漆方法的 Swing GUI的对象应当建立和操纵的只有的上的事件调度线程。 使用适当的访问控制。 hello im wrote some code i though it was going to work but it compiles and it doesnt even throws an exception or anything. it also creates the icon like its openned but i click it and it doesnt do anything please need help to know what am i doing wrong . heres the code of the class:package practicagraficos8;import java.awt.*;import java.awt.event.*;import javax.swing.*;public class ventanatexto { public JFrame ventana; public String texto; ventanatexto(){ JFrame.setDefaultLookAndFeelDecorated(true); texto=""; ventana= new JFrame("teclado"); panel1 panel= new panel1(); ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ventana.add(panel); ventana.setVisible(true); ventana.addKeyListener(new handler()); } public class panel1 extends JPanel { @Override public void paint(Graphics g){ super.paint(g); Dimension dim= getSize(); g.clearRect(0, 0, dim.width, dim.height); g.drawString(texto, WIDTH, WIDTH); };} class handler extends KeyAdapter{ @Override public void keyPressed(KeyEvent k){ char tecla= k.getKeyChar(); switch(tecla){ case 127:texto=""; break; case 8: if(texto.length()>0){texto=texto.substring(0, texto.length()-1);} break; default: if (texto.length()<15){texto+=tecla;} } ventana.repaint(); } }}and here is my main:package practicagraficos8;public class Practicagraficos8 { public static void main(String[] args) { ventanatexto prueba= new ventanatexto(); }} 解决方案 As shown here, "drawString() expects the coordinates to represent the baseline of the String."FontMetrics fm = g.getFontMetrics();g.drawString(texto, 0, fm.getAscent());In addition,Use a JTextComponent for editable text.Use Key Bindings, rather than KeyListener.Use pack() on the enclosing Window."Swing programs should override paintComponent() instead of overriding paint()."—Painting in AWT and Swing: The Paint Methods.Swing GUI objects should be constructed and manipulated only on the event dispatch thread.Use appropriate access control. 这篇关于与KeyAdapter,AWT运行时错误,摆了Java。帮助发现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
06-11 20:21