0

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();
    }
}
jose gabriel
  • 754
  • 2
  • 8
  • 21
  • no exception in console? Even at runtime? – Mukul Goel Nov 11 '12 at 17:25
  • no there are no exceptions at all. hope you can spot the problem. thank you. – jose gabriel Nov 11 '12 at 17:31
  • 3
    most likey your `JPanel` does not have focus, call `requestFocusInWindow()` on your `JPanel` instance after setting `JFrame` to visible and see if that works, if not double click on the `JPanel` and try again. If it works for any of the above your JPanel lost focus when it was added to `JFrame`. Rather use KeyBidnings and look at KeyEvent class the whole `case 127` is bad. Also override `paintComponent` of `JPanel` and not `paint()` – David Kroukamp Nov 11 '12 at 18:11
  • 2
    As @David notes, focus is _required_ for `KeyListener`; +1 for `KeyAdapter` (rather than `KeyListener`) and [sscce](http://sscce.org/). – trashgod Nov 11 '12 at 18:56

2 Answers2

3

As shown here, "drawString() expects the coordinates to represent the baseline of the String."

FontMetrics fm = g.getFontMetrics();
g.drawString(texto, 0, fm.getAscent());

ventana

In addition,

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
1

I've tried running your code. If you ask why your window does not show up, try setting bounds (size and location) on your JFrame:

ventana.setBounds(0, 0, 200, 200);

It seems the window will not show up, when using the decorated look and feel, if no bounds are set.

CleanUp
  • 410
  • 4
  • 12
  • 1
    Absent an intrinsic preferred size, `setBounds()` may be appropriate, as suggested among these [caveats](http://stackoverflow.com/q/7229226/230513). The essential problem is the text baseline, mentioned [here](http://stackoverflow.com/a/13333956/230513). – trashgod Nov 11 '12 at 19:00