我正在用Swing GUI構建Java中的Tic Tac Toe遊戲,並且它在Ubuntu 10.4和Windows XP中正確渲染。這是怎麼看起來像在Ubuntu:Java - Swing GUI在Windows 7中渲染不正確
當我與所有的類文件複製的Bin文件夾,並試圖運行在Windows 7的程序它看起來像這個:
img413.imageshack.us/img413/6144/tictactoe1.gif http://img413.imageshack.us/img413/6144/tictactoe1.gif
img708.imageshack.us/img708/4387/tictactoe2.gif http://img708.imageshack.us/img708/4387/tictactoe2.gif
我只是不明白什麼是錯的。正如我所說的,它在Ubuntu 10.4和Windows XP中完美運行。
如果有人能幫助我,我會非常高興!我將發佈與GUI相關的代碼,以防萬一需要解決問題。
這裏是我使用初始化GUI的代碼:
//Initializing GUI.
frame = new JFrame(); //Creating the window.
frame.setTitle("Tic Tac Toe"); //Setting the title of the window.
frame.addMouseListener(this);
frame.getContentPane().add(BorderLayout.CENTER, grid.getPanel()); //Adding the grid panel.
info = new JLabel(" Initializing game..."); //Creating info text.
frame.getContentPane().add(BorderLayout.SOUTH, info); //Adding info text.
//Setting GUI properties.
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setVisible(true);
與本身是在我的GameGrid類創建的網格,其具有方法「的JPanel getPanel()」的面板。下面是面板的初始化(代碼所屬在GameGrid的構造函數):
GridBox temp;
layout = new GridLayout(getHeight(), getWidth());
panel = new JPanel(layout);
panel.setBorder(
BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("Click in a box to place a marker:"),
BorderFactory.createEmptyBorder(5,5,5,5)));
//Creating a GridBox for each cell, and adding them to the panel in the right order..
for(int i = 0; i < getHeight(); i++) {
for(int j = 0; j < getWidth(); j++) {
temp = new GridBox(j, i);
temp.addMouseListener(listener);
panel.add(temp);
}
}
GridBox是JPanel的一個子類,其餘修改以自動顯示在指定的座標網格的內容。
class GridBox extends JPanel {
private static final long serialVersionUID = 1L;
int fontsize, x, y, value, signHeight, signWidth;
char print;
FontMetrics fm;
LineMetrics lm;
public GridBox(int a, int b) {
x = a; //TODO - input control
y = b;
}
public Move getMove() {
Move m = new Move(x, y);
return m;
}
public void paintComponent(Graphics g) {
Border blackline = BorderFactory.createLineBorder(Color.black);
setBorder(blackline);
Dimension size = getSize();
Rectangle2D rect;
fontsize = (int)(size.getHeight()*0.75);
value = getGridValue(x, y);
if(value == EMPTY)
print = ' ';
else if(value == 0)
print = 'X';
else if(value == 1)
print = 'O';
else
print = (char)value;
Font font = new Font("Times New Roman", Font.PLAIN, fontsize);
g.setFont(font);
fm = g.getFontMetrics();
rect = fm.getStringBounds(Character.toString(print), g);
signHeight = (int)rect.getHeight();
signWidth = (int)rect.getWidth();
g.setColor(Color.black);
g.drawString(Character.toString(print), (size.width/2)-(signWidth/2), (size.height/2)-(signHeight/2)+fm.getAscent());
}
}
在此先感謝!
非常感謝!我移動了「Border blackline = BorderFactory.createLineBorder(Color.black); setBorder(blackline);」到構造函數,現在它的工作原理! 但是爲什麼在我做之前它沒有工作?我當然意識到,每次組件繪製時都沒有必要設置邊框,但我無法理解這會如何造成很大麻煩。 – Greensea 2010-08-12 00:06:32
@Greensea它會改變偏移量,並且可能會使佈局無效組件樹。我認爲可以肯定地說所有投注都是在那個時候關閉的。 – 2010-08-12 00:09:08
啊,我想我現在明白了。我認爲setBorder()用新的邊框取代了舊邊框,但顯然這不是該方法的工作原理。謝謝您的幫助! – Greensea 2010-08-12 00:16:11