Du verwendest einen veralteten Browser. Es ist möglich, dass diese oder andere Websites nicht korrekt angezeigt werden. Du solltest ein Upgrade durchführen oder ein alternativer Browser verwenden.
Wo ist bei denen beiden Quelltexten der Unterschied?
Die Frage hab ich auch grammatikalisch nicht so ganz verstanden... ich vermute, du willst den Text innerhalb des Fensters zentrieren und nicht nur innerhalb des JLabels?
Java:
hallo.setHorizontalTextPosition(JLabel.CENTER);
Mit dieser Zeile hast du nur festgelegt, dass der Text innerhalb des JLabels zentriert werden soll.
Damit der Text innerhalb des Fenster zentriert wird, musst du auch ein Layout benutzen, welches das JLabel entweder zentriert oder es genauso breit zieht, wie die Zeichenfläche breit ist.
Zum Beispiel könntest du ein FlowLayout benutzen, um das JLabel zentriert zu positionieren:
Java:
import java.awt.*;
import javax.swing.*;
public class Game
{
public static void main(String args[])
{
JFrame oMainWindow=new JFrame("World of War");
oMainWindow.setSize(1280,768);
oMainWindow.setVisible(true);
oMainWindow.setLayout(new FlowLayout(FlowLayout.CENTER));
JLabel hallo=new JLabel("Hallo");
hallo.setHorizontalTextPosition(JLabel.CENTER);
hallo.setVisible(true);
oMainWindow.add(hallo);
oMainWindow.validate();
}
}
Dazu brauchst Du die Font-Klasse.
Erst erstellst Du Dir einen Font, der, den Du halt benutzen willst, und dann ordnest Du diesen Font der betreffenden Komponente zu. Also z.B.
Java:
/*
Programm: game.java
Version: 1
*/
import java.awt.Font;
import javax.swing.*;
public class game {
public static void main(String args[]) {
JFrame oMainWindow = new JFrame("World of War");
oMainWindow.setSize(1280, 768);
// folgende Zeile mal, damit der auch das Programm beendet, wenn
// das Fenster geschlossen wird
oMainWindow.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
oMainWindow.setVisible(true);
JLabel hallo = new JLabel("Hallo");
hallo.setHorizontalAlignment(JLabel.CENTER);
// Font erstellen, hier einen Font mit Serifen, in Fett und kursiv
// in 32 Pixeln Groesse
Font font = new Font("Serif", Font.BOLD + Font.ITALIC, 32);
hallo.setFont(font);
oMainWindow.add(hallo);
hallo.setVisible(true);
}
}
game.java:18: cannot find symbol
symbol : class Font
location: class game
Font font = new Font("Serif", Font.BOLD + Font.ITALIC, 32);
^
game.java:18: cannot find symbol
symbol : class Font
location: class game
Font font = new Font("Serif", Font.BOLD + Font.ITALIC, 32);
^
game.java:18: cannot find symbol
symbol : variable Font
location: class game
Font font = new Font("Serif", Font.BOLD + Font.ITALIC, 32);
^
game.java:18: cannot find symbol
symbol : variable Font
location: class game
Font font = new Font("Serif", Font.BOLD + Font.ITALIC, 32);
^
4 errors
und sieht so aus
Java:
/*
Programm: game.java
Version: 1
*/
import javax.swing.*;
public class game
{
public static void main(String args[])
{
JFrame oMainWindow=new JFrame("World of War");
oMainWindow.setSize(1280,768);
oMainWindow.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
oMainWindow.setVisible(true);
JLabel hallo=new JLabel("World of War");
hallo.setHorizontalAlignment(JLabel.CENTER);
hallo.setVerticalAlignment(JLabel.TOP);
Font font = new Font("Serif", Font.BOLD + Font.ITALIC, 32);
hallo.setFont(font);
hallo.setVisible(true);
oMainWindow.add(hallo);
}
}
Auch in Anbetracht anderer Fragen: Es wäre vielleicht ganz gut, wenn Du mal ein Buch zu Java von vorne beginnen würdest, um erstmal die Grundlagen zu wissen.
Was Modellbahner damit in bildhafter Form andeuten möchte: es macht kaum einen Unterschied ob Java 1.5 oder 1.6. Generell "verfällt" so schnell nichts und wird höchstens als veraltet angemosert. Blätter in der Insel doch einfach mal ein bißchen im Kapitel GUI-Programmierung, damit erklären sich die meisten Sachen schon von selbst.
Und bei Deinem momentanen Problem ist es auch wieder nur ein Import der fehlt (ich sage jetzt nicht welcher, das Konzept der Imports und wie man herausfindet, welche man benötigt ist auch in der erwähnten Literatur erklärt).