GUI布局问题[仍然需要帮助]

时间:2012-05-14 23:40:21

标签: user-interface layout jpanel grid-layout

我无法弄清楚如何进行如下布局: enter image description here

请原谅我糟糕的写作。我的平板电脑无处可寻,所以我不得不使用鼠标。 无论如何,我只想知道如何用我的Tic-Tac-Toe游戏正确地做这样的事情。

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

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

这是我的代码:

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);
        }
    }
}   
} 

感谢您的帮助和借口我可能很糟糕的编码逻辑。

2 个答案:

答案 0 :(得分:0)

对于游戏,通常最好覆盖paint(Graphics)。

在方法中使用以下psuedocode:

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.

Image failed to load.

答案 1 :(得分:0)

您发布的代码存在一些问题:

  • 只能从Event Dispatch Thread访问Swing组件。请参阅Initial ThreadsThe Event Dispatch Thead
  • frame未设置任何布局,因此默认为BorderLayout。您应该使用bgpanelbgpanel2添加add(bgpanel, BorderLayout.CENTER)add(bgpanel2, BorderLayout.NORTH)add(component)默认为CENTER,因此bgpanel会替换bgpanel2,而第二个面板永远不会呈现。
  • actionPerformed中,您为每个按钮重复相同的操作序列。这通常是指示你应该引入一个功能来重复工作。

所有这一切,你在这里拥有的新手代码实际上相当不错。保持良好的工作,欢迎来到Java!