Reputation: 60
I'm a beginner in Java, and I'm using book to learn, now I'm in the first GUI topics, and I've got a problem. I don't know why but I can't see text that I'm trying to put in the frame, this is the code:
import java.awt.*;
import javax.swing.*;
public class Ramka {
public static void main(String[] args)
{
JFrame ramka = new JFrame();
ramka.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ramka.setTitle("Frame");
ramka.setVisible(true);
Toolkit zestaw = Toolkit.getDefaultToolkit();
Dimension rozmiar = zestaw.getScreenSize();
int szerokosc = rozmiar.width;
int wysokosc = rozmiar.height;
ramka.setSize(szerokosc, wysokosc);
}
}
class Ramkatekst extends JComponent{
Toolkit zestaw = Toolkit.getDefaultToolkit();
Dimension rozmiar = zestaw.getScreenSize();
int szerokosc = rozmiar.width/2;
int wysokosc = rozmiar.height/2;
public void paintComponent(Graphics g)
{
g.drawString(" Sample text",szerokosc, wysokosc );
}
}
Upvotes: 0
Views: 233
Reputation: 889
As @pshemo points out, Ramkatekst is never used. You need to create an instance of it and add it to your JFrame "ramka". So simply add this line at the end of your main method:
ramka.add(new Ramkatekst());
Upvotes: 1