簡體   English   中英

Java-Swing GUI在Windows 7中無法正確呈現

[英]Java - Swing GUI renders incorrectly in Windows 7

我正在使用帶有Swing GUI的Java構建Tic Tac Toe游戲,並且該游戲可以在Ubuntu 10.4和Windows XP中正確呈現。 這是在Ubuntu中的樣子:

http://img266.imageshack.us/img266/2432/tictactoe2.png

當我復制帶有所有類文件的bin文件夾並嘗試在Windows 7中運行該程序時,它看起來像這樣:

img413.imageshack.us/img413/6144/tictactoe1.gif
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());
    }
}

提前致謝!

在重新繪制組件時更改邊框時存在一個明顯的問題。 那會引起各種各樣的問題。

另外,我看不到您在哪里繪制面板的背景。 你應該有

super.paintComponent(g);

在方法的頂部。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM