【问题标题】:Java - Swing GUI renders incorrectly in Windows 7Java - Swing GUI 在 Windows 7 中呈现不正确
【发布时间】:2019-08-06 07:06:33
【问题描述】:

我正在用 Java 构建一个带有 Swing GUI 的井字游戏,它可以在 Ubuntu 10.4 和 Windows XP 中正确呈现。这就是它在 Ubuntu 中的样子:

当我复制包含所有类文件的 bin 文件夹并尝试在 Windows 7 中运行该程序时,它看起来像这样:


我就是不明白出了什么问题。正如我所说,它在 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());
    }
}

提前致谢!

【问题讨论】:

    标签: java windows user-interface swing


    【解决方案1】:

    在重新绘制组件时更改边框存在明显问题。这会导致各种各样的问题。

    【讨论】:

    • 非常感谢!我移动了“Border blackline = BorderFactory.createLineBorder(Color.black); setBorder(blackline);”到构造函数,现在它可以工作了!但是为什么在我这样做之前它不起作用?当然,我意识到每次绘制组件时都没有必要设置边框,但我不明白这怎么会造成这么多麻烦..
    • @Greensea 它会改变偏移量,并且可能会使组件树上的布局无效。我认为可以肯定地说,那时所有的赌注都结束了。
    • 啊,我想我现在明白了。我认为 setBorder() 用新边框替换了旧边框,但显然这不是该方法的工作原理。感谢您的帮助!
    【解决方案2】:

    另外,我看不到您在哪里绘制面板的背景。你应该有

    super.paintComponent(g);
    

    在方法的顶部。

    【讨论】:

      猜你喜欢
      • 2016-05-15
      • 2016-01-18
      • 2021-12-07
      • 2016-06-21
      • 2014-09-28
      • 2021-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多