运行jpanel& Jframe实例(Mac M1 Pro)
我是编码的初学者。我正在MacBook M1 Pro上使用Intellij。到目前为止,大多数事情变得很棒,而且编译时间很快。应用程序在终端中运行的应用程序完美工作。
不幸的是,当我使用Jpanel& Jframe似乎有一个延迟或完整的错误。 我正在观看有关Tic Tac Toe游戏的本教程。我已经完成了完全相同的所有操作,但是在此时,当我运行它时,只显示一个带有白色顶部面板的灰色正方形面板。没有文字。
奇怪的是,当我调整窗户大小时,窗户的冻结很短,然后是旋转的载荷轮,然后带有正确的背面颜色的文字“ tic-tac-toe”出现在顶部。
当我从视频中复制整个最终代码时,出于某种原因有效。我已经检查了所有内容,到目前为止,它相同,但由于某种原因无法正常工作。
项目中有一个tictactoe.java和main.java文件。
到目前为止,我的代码在tictactoe.java:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class TicTacToe implements ActionListener {
Random random = new Random();
JFrame frame = new JFrame();
JPanel title_panel = new JPanel();
JPanel button_panel = new JPanel();
JLabel textfield = new JLabel();
JButton[] buttons = new JButton[9];
boolean player1_turn;
TicTacToe() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 800);
frame.getContentPane().setBackground(new Color(50,50,50));
frame.setLocationRelativeTo(null);
frame.setLayout(new BorderLayout());
frame.setVisible(true);
textfield.setBackground(new Color(9, 21, 227));
textfield.setForeground(new Color(25, 255, 0));
textfield.setFont(new Font("Ink Free", Font.BOLD, 75));
textfield.setHorizontalAlignment(JLabel.CENTER);
textfield.setText("Tic-Tac-Toe");
textfield.setOpaque(true);
title_panel.setLayout(new BorderLayout());
title_panel.setBounds(0,0,800,100);
title_panel.add(textfield);
frame.add(title_panel,BorderLayout.NORTH);
}
@Override
public void actionPerformed(ActionEvent e) {
}
public void firstTurn() {
}
public void check() {
}
public void xWins(int a, int b, int c) {
}
public void oWins(int a, int b, int c) {
}
}
视频教程的代码:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class TicTacToe implements ActionListener{
Random random = new Random();
JFrame frame = new JFrame();
JPanel title_panel = new JPanel();
JPanel button_panel = new JPanel();
JLabel textfield = new JLabel();
JButton[] buttons = new JButton[9];
boolean player1_turn;
TicTacToe(){
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800,800);
frame.getContentPane().setBackground(new Color(50,50,50));
frame.setLayout(new BorderLayout());
frame.setVisible(true);
textfield.setBackground(new Color(25,25,25));
textfield.setForeground(new Color(25,255,0));
textfield.setFont(new Font("Ink Free",Font.BOLD,75));
textfield.setHorizontalAlignment(JLabel.CENTER);
textfield.setText("Tic-Tac-Toe");
textfield.setOpaque(true);
title_panel.setLayout(new BorderLayout());
title_panel.setBounds(0,0,800,100);
button_panel.setLayout(new GridLayout(3,3));
button_panel.setBackground(new Color(150,150,150));
for(int i=0;i<9;i++) {
buttons[i] = new JButton();
button_panel.add(buttons[i]);
buttons[i].setFont(new Font("MV Boli",Font.BOLD,120));
buttons[i].setFocusable(false);
buttons[i].addActionListener(this);
}
title_panel.add(textfield);
frame.add(title_panel,BorderLayout.NORTH);
frame.add(button_panel);
firstTurn();
}
@Override
public void actionPerformed(ActionEvent e) {
for(int i=0;i<9;i++) {
if(e.getSource()==buttons[i]) {
if(player1_turn) {
if(buttons[i].getText()=="") {
buttons[i].setForeground(new Color(255,0,0));
buttons[i].setText("X");
player1_turn=false;
textfield.setText("O turn");
check();
}
}
else {
if(buttons[i].getText()=="") {
buttons[i].setForeground(new Color(0,0,255));
buttons[i].setText("O");
player1_turn=true;
textfield.setText("X turn");
check();
}
}
}
}
}
public void firstTurn() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(random.nextInt(2)==0) {
player1_turn=true;
textfield.setText("X turn");
}
else {
player1_turn=false;
textfield.setText("O turn");
}
}
public void check() {
//check X win conditions
if(
(buttons[0].getText()=="X") &&
(buttons[1].getText()=="X") &&
(buttons[2].getText()=="X")
) {
xWins(0,1,2);
}
if(
(buttons[3].getText()=="X") &&
(buttons[4].getText()=="X") &&
(buttons[5].getText()=="X")
) {
xWins(3,4,5);
}
if(
(buttons[6].getText()=="X") &&
(buttons[7].getText()=="X") &&
(buttons[8].getText()=="X")
) {
xWins(6,7,8);
}
if(
(buttons[0].getText()=="X") &&
(buttons[3].getText()=="X") &&
(buttons[6].getText()=="X")
) {
xWins(0,3,6);
}
if(
(buttons[1].getText()=="X") &&
(buttons[4].getText()=="X") &&
(buttons[7].getText()=="X")
) {
xWins(1,4,7);
}
if(
(buttons[2].getText()=="X") &&
(buttons[5].getText()=="X") &&
(buttons[8].getText()=="X")
) {
xWins(2,5,8);
}
if(
(buttons[0].getText()=="X") &&
(buttons[4].getText()=="X") &&
(buttons[8].getText()=="X")
) {
xWins(0,4,8);
}
if(
(buttons[2].getText()=="X") &&
(buttons[4].getText()=="X") &&
(buttons[6].getText()=="X")
) {
xWins(2,4,6);
}
//check O win conditions
if(
(buttons[0].getText()=="O") &&
(buttons[1].getText()=="O") &&
(buttons[2].getText()=="O")
) {
oWins(0,1,2);
}
if(
(buttons[3].getText()=="O") &&
(buttons[4].getText()=="O") &&
(buttons[5].getText()=="O")
) {
oWins(3,4,5);
}
if(
(buttons[6].getText()=="O") &&
(buttons[7].getText()=="O") &&
(buttons[8].getText()=="O")
) {
oWins(6,7,8);
}
if(
(buttons[0].getText()=="O") &&
(buttons[3].getText()=="O") &&
(buttons[6].getText()=="O")
) {
oWins(0,3,6);
}
if(
(buttons[1].getText()=="O") &&
(buttons[4].getText()=="O") &&
(buttons[7].getText()=="O")
) {
oWins(1,4,7);
}
if(
(buttons[2].getText()=="O") &&
(buttons[5].getText()=="O") &&
(buttons[8].getText()=="O")
) {
oWins(2,5,8);
}
if(
(buttons[0].getText()=="O") &&
(buttons[4].getText()=="O") &&
(buttons[8].getText()=="O")
) {
oWins(0,4,8);
}
if(
(buttons[2].getText()=="O") &&
(buttons[4].getText()=="O") &&
(buttons[6].getText()=="O")
) {
oWins(2,4,6);
}
}
public void xWins(int a,int b,int c) {
buttons[a].setBackground(Color.GREEN);
buttons[b].setBackground(Color.GREEN);
buttons[c].setBackground(Color.GREEN);
for(int i=0;i<9;i++) {
buttons[i].setEnabled(false);
}
textfield.setText("X wins");
}
public void oWins(int a,int b,int c) {
buttons[a].setBackground(Color.GREEN);
buttons[b].setBackground(Color.GREEN);
buttons[c].setBackground(Color.GREEN);
for(int i=0;i<9;i++) {
buttons[i].setEnabled(false);
}
textfield.setText("O wins");
}
}
我认为这可能是JDK或与Mac和Intellij的不兼容,我不知道。我已经重新安装了Intellij并将其重置为出厂设置。
如果有人有任何想法,请分享。我已经谷歌搜索了很多,找不到任何解决方案。
I'm a beginner to coding. I'm using IntelliJ on a MacBook M1 Pro. So far most things went great and the compiling time is fast. Apps where everything runs in the Terminal work perfectly.
Unfortunately, when I use Swing instances like JPanel & JFrame there seems to be a delay or complete bug.
I'm watching this tutorial on a Tic Tac Toe game. I've done everything the exact same but at this point when I run it only a gray square panel with a white top panel is being displayed. No text.
Weirdly, when I resize the window, there is a short freeze of the window, followed by the spinning loading wheel and then the text "Tic-Tac-Toe" appears on the top with the correct back-&foreground colors.
When I copy the whole final code from the video it works for some reason. I've triple checked everything, so far it's identical but it's not working correctly for some reason.
There's a TicTacToe.java and Main.java file in the project.
My code so far in TicTacToe.java :
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class TicTacToe implements ActionListener {
Random random = new Random();
JFrame frame = new JFrame();
JPanel title_panel = new JPanel();
JPanel button_panel = new JPanel();
JLabel textfield = new JLabel();
JButton[] buttons = new JButton[9];
boolean player1_turn;
TicTacToe() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 800);
frame.getContentPane().setBackground(new Color(50,50,50));
frame.setLocationRelativeTo(null);
frame.setLayout(new BorderLayout());
frame.setVisible(true);
textfield.setBackground(new Color(9, 21, 227));
textfield.setForeground(new Color(25, 255, 0));
textfield.setFont(new Font("Ink Free", Font.BOLD, 75));
textfield.setHorizontalAlignment(JLabel.CENTER);
textfield.setText("Tic-Tac-Toe");
textfield.setOpaque(true);
title_panel.setLayout(new BorderLayout());
title_panel.setBounds(0,0,800,100);
title_panel.add(textfield);
frame.add(title_panel,BorderLayout.NORTH);
}
@Override
public void actionPerformed(ActionEvent e) {
}
public void firstTurn() {
}
public void check() {
}
public void xWins(int a, int b, int c) {
}
public void oWins(int a, int b, int c) {
}
}
The video tutorial's code:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class TicTacToe implements ActionListener{
Random random = new Random();
JFrame frame = new JFrame();
JPanel title_panel = new JPanel();
JPanel button_panel = new JPanel();
JLabel textfield = new JLabel();
JButton[] buttons = new JButton[9];
boolean player1_turn;
TicTacToe(){
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800,800);
frame.getContentPane().setBackground(new Color(50,50,50));
frame.setLayout(new BorderLayout());
frame.setVisible(true);
textfield.setBackground(new Color(25,25,25));
textfield.setForeground(new Color(25,255,0));
textfield.setFont(new Font("Ink Free",Font.BOLD,75));
textfield.setHorizontalAlignment(JLabel.CENTER);
textfield.setText("Tic-Tac-Toe");
textfield.setOpaque(true);
title_panel.setLayout(new BorderLayout());
title_panel.setBounds(0,0,800,100);
button_panel.setLayout(new GridLayout(3,3));
button_panel.setBackground(new Color(150,150,150));
for(int i=0;i<9;i++) {
buttons[i] = new JButton();
button_panel.add(buttons[i]);
buttons[i].setFont(new Font("MV Boli",Font.BOLD,120));
buttons[i].setFocusable(false);
buttons[i].addActionListener(this);
}
title_panel.add(textfield);
frame.add(title_panel,BorderLayout.NORTH);
frame.add(button_panel);
firstTurn();
}
@Override
public void actionPerformed(ActionEvent e) {
for(int i=0;i<9;i++) {
if(e.getSource()==buttons[i]) {
if(player1_turn) {
if(buttons[i].getText()=="") {
buttons[i].setForeground(new Color(255,0,0));
buttons[i].setText("X");
player1_turn=false;
textfield.setText("O turn");
check();
}
}
else {
if(buttons[i].getText()=="") {
buttons[i].setForeground(new Color(0,0,255));
buttons[i].setText("O");
player1_turn=true;
textfield.setText("X turn");
check();
}
}
}
}
}
public void firstTurn() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(random.nextInt(2)==0) {
player1_turn=true;
textfield.setText("X turn");
}
else {
player1_turn=false;
textfield.setText("O turn");
}
}
public void check() {
//check X win conditions
if(
(buttons[0].getText()=="X") &&
(buttons[1].getText()=="X") &&
(buttons[2].getText()=="X")
) {
xWins(0,1,2);
}
if(
(buttons[3].getText()=="X") &&
(buttons[4].getText()=="X") &&
(buttons[5].getText()=="X")
) {
xWins(3,4,5);
}
if(
(buttons[6].getText()=="X") &&
(buttons[7].getText()=="X") &&
(buttons[8].getText()=="X")
) {
xWins(6,7,8);
}
if(
(buttons[0].getText()=="X") &&
(buttons[3].getText()=="X") &&
(buttons[6].getText()=="X")
) {
xWins(0,3,6);
}
if(
(buttons[1].getText()=="X") &&
(buttons[4].getText()=="X") &&
(buttons[7].getText()=="X")
) {
xWins(1,4,7);
}
if(
(buttons[2].getText()=="X") &&
(buttons[5].getText()=="X") &&
(buttons[8].getText()=="X")
) {
xWins(2,5,8);
}
if(
(buttons[0].getText()=="X") &&
(buttons[4].getText()=="X") &&
(buttons[8].getText()=="X")
) {
xWins(0,4,8);
}
if(
(buttons[2].getText()=="X") &&
(buttons[4].getText()=="X") &&
(buttons[6].getText()=="X")
) {
xWins(2,4,6);
}
//check O win conditions
if(
(buttons[0].getText()=="O") &&
(buttons[1].getText()=="O") &&
(buttons[2].getText()=="O")
) {
oWins(0,1,2);
}
if(
(buttons[3].getText()=="O") &&
(buttons[4].getText()=="O") &&
(buttons[5].getText()=="O")
) {
oWins(3,4,5);
}
if(
(buttons[6].getText()=="O") &&
(buttons[7].getText()=="O") &&
(buttons[8].getText()=="O")
) {
oWins(6,7,8);
}
if(
(buttons[0].getText()=="O") &&
(buttons[3].getText()=="O") &&
(buttons[6].getText()=="O")
) {
oWins(0,3,6);
}
if(
(buttons[1].getText()=="O") &&
(buttons[4].getText()=="O") &&
(buttons[7].getText()=="O")
) {
oWins(1,4,7);
}
if(
(buttons[2].getText()=="O") &&
(buttons[5].getText()=="O") &&
(buttons[8].getText()=="O")
) {
oWins(2,5,8);
}
if(
(buttons[0].getText()=="O") &&
(buttons[4].getText()=="O") &&
(buttons[8].getText()=="O")
) {
oWins(0,4,8);
}
if(
(buttons[2].getText()=="O") &&
(buttons[4].getText()=="O") &&
(buttons[6].getText()=="O")
) {
oWins(2,4,6);
}
}
public void xWins(int a,int b,int c) {
buttons[a].setBackground(Color.GREEN);
buttons[b].setBackground(Color.GREEN);
buttons[c].setBackground(Color.GREEN);
for(int i=0;i<9;i++) {
buttons[i].setEnabled(false);
}
textfield.setText("X wins");
}
public void oWins(int a,int b,int c) {
buttons[a].setBackground(Color.GREEN);
buttons[b].setBackground(Color.GREEN);
buttons[c].setBackground(Color.GREEN);
for(int i=0;i<9;i++) {
buttons[i].setEnabled(false);
}
textfield.setText("O wins");
}
}
I'm thinking it's something with perhaps the JDK or an incompatibility with mac and IntelliJ, I dont know. I've already reinstalled IntelliJ and reset it to factory settings.
If anyone has any ideas please share. I've googled a lot and couldn't find any solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论