저는 Swing GUI로 Java에서 Tic Tac Toe 게임을 만들고 있는데, 우분투 10.4 및 Windows XP에서 올바르게 렌더링됩니다. 나는 모든 클래스 파일로 빈 폴더를 복사하고 대신이처럼 보였다 윈도우 7에서 프로그램을 실행하려고 할 때Java 스윙 GUI가 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
무엇이 잘못되었는지를 이해할 수 없습니다. 내가 말했듯이, 그것은 우분투 10.4와 윈도우 XP에서 완벽하게 작동합니다.
누군가 나를 도울 수 있다면 매우 행복 할 것입니다! 문제 해결을 위해 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);
방법 "JPanel의 getPanel을()"이 자체가 내 GameGrid 클래스에서 생성 된 그리드와 패널 : 여기
내가 GUI를 초기화하는 데 사용하는 코드입니다. 여기에 패널의 초기화 (코드 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는 I 자동 지정된 좌표 그리드의 내용을 표시하도록 변경 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
@Greensea 오프셋을 변경하여 레이아웃을 무효화 할 것입니다 구성 요소 트리를 확장하십시오. 나는 모든 내기가 그 시점에서 꺼져 있다고 말하는 것이 안전하다고 생각합니다. –
아, 지금은 이해할 것 같습니다. setBorder()가 이전 테두리를 새 테두리로 바꿨다고 생각했지만 분명히 그 방법은 작동하지 않습니다. 도와 주셔서 감사합니다! – Greensea