|
It's called OOP for a reason. Please, the constructor never executes code. Define some methods for that.
You are adding the JLabel to the ContentPane - which you first set with a JLabel. Adding a JLabel to a JLabel does not work.
Also please just refer to the JFrame by adding to "this".
Suggestion:
1. OOP that.
2. Set up a JPanel, that forms the playground for the rat.
You probably want a second Panel for the Game controls - which are now to be entered in the console before.
3. Use a null-Layout for the game-Panel. You want to move the rat by pixel / absolute position, not by relative position to some other control.
FlowLayout is also default, does not need to be initalized.
import javax.swing.*;
class CatchTheRat extends JFrame {
JLabel lb;
Random r;
public CatchTheRat(int k) {
createGUI(k);
}
private void createGUI(int k) {
setTitle("Catch The Rat");
setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(new Dimension(800,600));
this.add(createContent(k));
setVisible(true);
}
private JPanel createContent(int k) {
JPanel oPanel = new JPanel();
lb = new JLabel(new ImageIcon("rat.jpg"));
oPanel.add(lb);
r = new Random();
lb.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent me) {
Toolkit.getDefaultToolkit().beep();
System.out.println("Caught!");
}
});
return oPanel;
}
public static void main(String args[]) {
new CatchTheRat(5);
}
}
|
|
|
|
|
Thanks for the suggestion. But that worked in my system. We can add JLabel to another JLabel because JComponent itself is a container and we can add any component to any component. There is no limitation. The add method does it.
Gowtham Gutha (http://java-demos.blogspot.com)
|
|
|
|
|
Technically - yes. Until someone wants to place place a text in the JLabel or plays around.
So it's pretty much safer to use a JPanel, that is known as a container.
|
|
|
|
|
Ok. By the way, could i also know why it is not recommended to write code in the constructor and only in a separate method and then call that method?
Gowtham Gutha (http://java-demos.blogspot.com)
|
|
|
|
|
|
What really is the error in my program? I couldn't understand. You'll need to have those image files (the background and the rat) to see them. That may be the possible error because it has worked in my development environment.
Gowtham Gutha (http://java-demos.blogspot.com)
|
|
|
|
|
Hi
i am new in android development, my question is how to obtain data from Mysql(questions) server PHP to the tablet, i know that with web service but how, and display automatic in the activity (texview?????) questions for example 5 cuestions for screen and other 5 for the next screen automatic.please give me a page or site where i find this information
sorry for my bad english, i hope understand, and i beginning in this develop of apps
thank you for your answers
see you
|
|
|
|
|
I‘ve used C# for one and half years.
Recently,for job reason,I need to use Java.
Actually,I've been learning WP Development,but it is hard to find a job,so I turn to android.
But they are lots of Java and Android books,so any recommend.
Beginner , Advanced Books.
|
|
|
|
|
Java in a Nutshell, published by O'Reilly.
The nutshell series are always good value.
A bit of introductory tutorial (more refresher, really) and then comprehensive reference.
So not a beginner's book but a working developer's constant companion.
Cheers,
Peter
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
|
|
|
|
|
|
Herbert Schildt's Java The Complete Reference
Updated and Expanded for Java SE 7 will be of good choice.
|
|
|
|
|
Is it need to learn Java 7 just for android development?
|
|
|
|
|
You might not need if you have a grip on the basics of Java. If you have then you can go ahead referring android books.
Gowtham Gutha (http://java-demos.blogspot.com)
|
|
|
|
|
|
|
I've been using "Sams Teaches you Java in 24 hours" and I found it to be amazing, have a look! (I know it's a late answer, but still)
|
|
|
|
|
Hello,
I have a code like this
public class Doc extends DefaultStyledDocument{
}
Doc doc= new Doc();
doc.insertString(0, "This is a working document", null);
AbstractDocument.AbstractElement rootElement = doc.createDefaultRoot();
Once I create a root element how do I assign or set its value from the doc string.Currently the element is blank.
thnaks
Pritha
|
|
|
|
|
Am not adding full code .. jus sample
import com.lowagie.text.*;
Paragraph pH = new Paragraph("Title",FontFactory.getFont(FontFactory.TIMES_ROMAN, 8, Font.BOLD, new Color(0, 0, 255)));
Paragraph pF1 = new Paragraph("Conditions Apply",FontFactory.getFont(FontFactory.HELVETICA, 7, Font.BOLD, new Color(0, 0, 255)));
Paragraph pF2 = new Paragraph("*Coupon Valid till 1 month of date issued",FontFactory.getFont(FontFactory.HELVETICA, 7, Font.BOLD, new Color(0, 0, 255)));
Paragraph pF3 = new Paragraph("*tamil translation");
Paragraph pNEXT = new Paragraph("\n");
Paragraph p3 = new Paragraph();
p3.add(new Chunk(""));
Chunk separator = new Chunk(new DottedLineSeparator());
p3.add(separator);
pH.setAlignment(Paragraph.ALIGN_CENTER);
document.add(pH);
document.add(pNEXT);
document.add(tab);
document.add(pNEXT);
document.add(pF1);
document.add(pF2);
document.add(p3);
//document;
document.add(pNEXT);
|
|
|
|
|
hello
thanks for your reply
I am using swing libraries and there i am able to create a leaf or a branch of a document but the leafelement doesnt seem to get added to the default root .
public class MyDoc extends DefaultStyledDocument {
protected void insertUpdate(AbstractDocument.DefaultDocumentEvent e,
AttributeSet attr){
Element root = getDefaultRootElement();
Element branchroot = new BranchElement(root, null);
branchroot = createBranchElement(root,null);
Element leafElement = new LeafElement(branchroot, null, 0,0);
leafElement = createLeafElement(branchroot,null,"p");
}
public Element createBranchElement(Element parent,AttributeSet a)
{
return super.createBranchElement(parent, a);
}
protected Element createLeafElement(Element parent,AttributeSet a, String arg) throws BadLocationException
{
return super.createLeafElement(parent, null, startOffset(arg),startOffset(arg)+5);
}
}
|
|
|
|
|
Hi all!
I want to get the database info( eg.instance;username)by using only IP.I don't know how to start.
thank you!
modified 5-Nov-12 10:27am.
|
|
|
|
|
XMafei wrote: I don't know how to start. You could start by giving some more details about your problem. You do not get information from a database just by giving the IP address, but by creating a connection to a database server and making requests, the IP address is just part of the connection string.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
eg
input IP,UserName,UserPwd
output database info such as database instance,database user
|
|
|
|
|
XMafei wrote: eg
input IP,UserName,UserPwd
output database info such as database instance,database user
I have no idea what that is supposed to mean. However, I suggest you spend some time reading about Java Database Access[^].
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
XMafei wrote: I don't know how to start.
You are referring to information that is database specific. So you research database specific protocols.
As an example of one such possibility you can look at the following
http://www.sqlmag.com/article/active-directory-service-interfaces-adsi/publishing-sql-server-in-active-directory[^]
And keep in mind that it is possible that one cannot get that information for some databases either because the database doesn't support it or because it is not published. That of course suggests that the best way and certainly the easiest is to just require that the user must provide that information.
|
|
|
|
|
Please develop a simple console base application, which reads some persons records from a “.txt” file and displays each person’s information in form of table. The program should also count and display the number of alphabets, spaces, integers, words, characters and lines from the file in tabular form.
please make this application file (.java) and driver file (.java) separately.
(All three files will be in one folder for execution through driver file)
text file is containing the follwing text.
David, 51, IT, 40006, Bachelor
Tomb, 85, Computer, 40502, Master
Stephen, 97, Maths, 40652, Phd
..............................
it is infact
name, rollNo, grade, phoneNo, program
variables.
|
|
|
|
|