0

我不知道如何进行如下布局: 在此处输入图像描述

原谅我写的很烂。我的平板电脑无处可寻,所以我不得不使用鼠标。无论如何,我只是想知道如何用我的井字游戏正确地做类似的事情。

我已经得到了应该在的网格,但我这辈子也无法在屏幕上获得标题。我也想知道如何做一个简单的纯色条,如图所示。我也尝试过,但它不会总是出现。有时它会闪烁,有时它会闪烁然后消失,或者根本不出现。

我会说这可能与我设置布局的方式有关,但我不知道如何做网格。哦!我还想,如果我弄清楚整个面板添加到框架的事情出了什么问题,我可能会弄清楚为什么当您尝试单击已占用的按钮时应该显示的红色错误文本没有出现。我很确定这是同样的问题。

这是我的代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Code implements ActionListener {

JFrame frame = new JFrame ("Tic-Tac-Toe");

JLabel title = new JLabel ("Tic-Tac-Toe"); //displayed title of the program
JLabel error = new JLabel (""); //label that says error if you make a move on a
//non-blank button

JPanel titlestrip = new JPanel (); //the strip behind the title
JPanel bgpanel = new JPanel (); //the background panel that fills up the window
JPanel bgpanel2 = new JPanel (); //second bg panel with no layout 
JPanel buttonpanel = new JPanel(); //the panel that holds the nine buttons

JButton one = new JButton ("");
JButton two = new JButton ("");
JButton three = new JButton ("");
JButton four = new JButton ("");
JButton five = new JButton (""); 
JButton six = new JButton ("");
JButton seven = new JButton ("");
JButton eight = new JButton ("");
JButton nine = new JButton ("");

GridBagConstraints x = new GridBagConstraints ();

static String symbol = ""; //stores either an X or an O when the game begins
static int count = 0; //hidden counter; even for one player & odd for the other

public Code() {
    Code();
}

private void Code(){
    titlestrip.setLayout(null);
    titlestrip.setBackground(new Color (0x553EA5)); //color of the strip behind                                        title
    titlestrip.setLocation (98,5);
    titlestrip.setSize (400, 50);

    title.setFont(new Font("Rockwell Extra Bold", Font.PLAIN, 48)); //font settings
    title.setForeground(new Color (0x10CDC6)); //title color

    bgpanel.setBackground(new Color(0x433F3F)); //background color
    bgpanel.setLayout(FlowLayout());

    bgpanel2.setBackground(new Color(0x433F3F)); 

    frame.setVisible (true);
    frame.setSize (500,500);
    frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

    buttonpanel.setLayout (new GridLayout(3,3));
    buttonpanel.add(one);
    buttonpanel.add(two);
    buttonpanel.add(three);
    buttonpanel.add(four);
    buttonpanel.add(five);
    buttonpanel.add(six);
    buttonpanel.add(seven);
    buttonpanel.add(eight);
    buttonpanel.add(nine);
    buttonpanel.setSize (200,200);
    buttonpanel.setLocation(150, 150);

    one.addActionListener(this);
    two.addActionListener(this);
    three.addActionListener(this);
    four.addActionListener(this);
    five.addActionListener(this);
    six.addActionListener(this);
    seven.addActionListener(this);
    eight.addActionListener(this);
    nine.addActionListener(this);

    bgpanel.add(buttonpanel);
    bgpanel2.add(title);

    x.gridx = 150;
    x.gridy = 400;
    bgpanel2.add(error, x);

    frame.add(bgpanel2);
    frame.add(bgpanel); 
}
private LayoutManager FlowLayout() {
    // TODO Auto-generated method stub
    return null;
}
//- - - - - - - - - - - - - - - - - - [ END ] - - - - - - - - - - - - - - - - - - - //
//- - - - - - - - - - - - - - - - - -[ LAYOUT ] - - - - - - - - - - - - - - - - - - - //
public static void main(String[] args) {
    new SummativeCode();
}
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
public void actionPerformed(ActionEvent e){
    count = count + 1;
    String text = (String)e.getActionCommand(); //stores the kind of text in the button pressed
    //Checks which player is making a move
    if (count %2 == 0){
        symbol = "X";
    }
    else {
        symbol = "O";
    }

    //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
    //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
    //sets the text in the button with an X or an O depending on whose turn it is
    if (e.getSource() == one){
        if (text.equals("")){ //if text is blank, do the following
            one.setText(symbol);
        }
        else { //if it's not blank, display error
            error.setText("This is an occupied button. Please choose again.");
            error.setForeground(Color.red);
        }
    }
    if (e.getSource() == two){
        if (text.equals("")){
            two.setText(symbol);
        }
        else {
            error.setText("This is an occupied button. Please choose again.");
            error.setForeground(Color.red);
        }
    }
    if (e.getSource() == three){
        if (text.equals("")){
            three.setText(symbol);
        }
        else {
            error.setText("This is an occupied button. Please choose again.");
            error.setForeground(Color.red);
        }
    }
    if (e.getSource() == four){
        if (text.equals("")){
            four.setText(symbol);
        }
        else {
            error.setText("This is an occupied button. Please choose again.");
            error.setForeground(Color.red);
        }
    }
    if (e.getSource() == five){
        if (text.equals("")){
            five.setText(symbol);
        }
        else {
            error.setText("This is an occupied button. Please choose again.");
            error.setForeground(Color.red);
        }
    }
    if (e.getSource() == six){
        if (text.equals("")){
            six.setText(symbol);
        }
        else {
            error.setText("This is an occupied button. Please choose again.");
            error.setForeground(Color.red);
        }
    }
    if (e.getSource() == seven){
        if (text.equals("")){
            seven.setText(symbol);
        }
        else {
            error.setText("This is an occupied button. Please choose again.");
            error.setForeground(Color.red);
        }
    }
    if (e.getSource() == eight){
        if (text.equals("")){
            eight.setText(symbol);
        }
        else {
            error.setText("This is an occupied button. Please choose again.");
            error.setForeground(Color.red);
        }
    }
    if (e.getSource() == nine){
        if (text.equals("")){
            nine.setText(symbol);
        }
        else {
            error.setText("This is an occupied button. Please choose again.");
            error.setForeground(Color.red);
        }
    }
}   
} 

感谢您的帮助并原谅我可能糟糕的编码逻辑。

4

2 回答 2

0

您发布的代码有几个问题:

  • Swing 组件只能从 Event Dispatch Thread 访问。请参阅初始线程事件调度 Thead
  • 没有设置布局frame,所以默认为 BorderLayout。您应该添加bgpanelandbgpanel2add(bgpanel, BorderLayout.CENTER)and add(bgpanel2, BorderLayout.NORTH)add(component)默认为CENTERsobgpanel替换,bgpanel2第二个面板永远不会渲染。
  • actionPerformed您为每个按钮重复相同的操作序列时。这通常表明您应该引入一个功能来完成重复性工作。

话虽如此,你在这里所拥有的对于新手代码来说实际上是相当不错的。继续努力,欢迎来到 Java!

于 2012-05-15T02:43:13.340 回答
0

对于游戏,通常最好覆盖绘画(图形)。

在方法中使用以下伪代码:

setColor(backColor);
drawRect(0,0,width,height);
setColor(foreColor);
drawRect(0,0,width,150); // I'm just guessing that your top is 150 pixels
drawImage(bufferedTTT,Op,width/2-bufferedTTT.width/2, 15) // to center the heading, you can use drawText, but it would be a lot harder for the same effect
setColor(black)
drawLine(...)
... // draw Lines for TTT board.

图片加载失败。

于 2012-05-14T23:54:53.987 回答