简体   繁体   English

Java-Swing GUI在Windows 7中无法正确呈现

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

I'm building a Tic Tac Toe game in Java with a Swing GUI, and it renders correctly in Ubuntu 10.4 and Windows XP. 我正在使用带有Swing GUI的Java构建Tic Tac Toe游戏,并且该游戏可以在Ubuntu 10.4和Windows XP中正确呈现。 This is how it looks like in Ubuntu: 这是在Ubuntu中的样子:

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

When I copied the bin-folder with all the class files and tried to run the program in Windows 7 it looked like this instead: 当我复制带有所有类文件的bin文件夹并尝试在Windows 7中运行该程序时,它看起来像这样:

img413.imageshack.us/img413/6144/tictactoe1.gif
img708.imageshack.us/img708/4387/tictactoe2.gif

I just can't understand what's wrong. 我只是不明白怎么了。 As I said, it works perfectly in Ubuntu 10.4 and Windows XP. 就像我说的那样,它在Ubuntu 10.4和Windows XP中可以完美运行。

I would be very happy if someone could help me out! 如果有人可以帮助我,我将非常高兴! I'll post the code related to the GUI, just in case it is needed to solve the problem. 我将发布与GUI相关的代码,以防万一需要解决该问题。

Here is the code I use to initialize the 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);

The panel with the grid itself is created in my GameGrid class, which have a method "JPanel getPanel()". 带有网格本身的面板在我的GameGrid类中创建,该类具有方法“ JPanel getPanel()”。 Here is the initialization of that panel (the code belongs in the constructor of GameGrid): 这是该面板的初始化(代码属于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 is a subclass of JPanel, which I modified to automatically show the contents of the grid at the coordinates specified. 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());
    }
}

Thanks in advance! 提前致谢!

There's an obvious problem in the you change the border whilst repainting the component. 在重新绘制组件时更改边框时存在一个明显的问题。 That's going to cause all sorts of problems. 那会引起各种各样的问题。

Also, I don't see where you paint the background of the panel. 另外,我看不到您在哪里绘制面板的背景。 You should have 你应该有

super.paintComponent(g);

at the top of the method. 在方法的顶部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM