【问题标题】:GUI Layout Problems [STILL require help]GUI 布局问题 [仍然需要帮助]
【发布时间】:2012-05-22 11:22:35
【问题描述】:

我不知道如何进行如下布局:

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

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

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

这是我的代码:

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

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

【问题讨论】:

    标签: user-interface layout jpanel grid-layout


    【解决方案1】:

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

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

    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.
    

    【讨论】:

    • 如果您能制作标题图像(带有面板,对吗?),我将不胜感激,只是想看看我会怎么做。呃,所以我不使用 GridLayout ......?我有点困惑。
    • 你不使用任何布局,我是说直接在屏幕上以编程方式绘制。 link 制作完成后我会添加图片。
    • 那么我是否必须在绘制的网格中手动排列按钮?我想这可能有效,但老实说我更喜欢 GridLayout。 D:非常感谢您的帮助!你碰巧知道如何解决我的问题吗?保持原样,但也只显示标题?
    • 去掉按钮,使用 MouseListener。
    • 天哪,我以前试过 MouseListener。它和我不完全相处。只是因为一些奇怪的原因,我发现它很难使用。
    【解决方案2】:

    您发布的代码存在几个问题:

    • Swing 组件只能从事件调度线程访问。见Initial ThreadsThe Event Dispatch Thead
    • frame 上没有设置布局,所以默认为 BorderLayout。您应该将bgpanelbgpanel2 添加到add(bgpanel, BorderLayout.CENTER)add(bgpanel2, BorderLayout.NORTH)add(component) 默认为 CENTER,所以 bgpanel 替换 bgpanel2 并且第二个面板永远不会呈现。
    • actionPerformed 中,您正在为每个按钮重复相同的操作序列。这通常表明您应该引入一个函数来完成重复性工作。

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

    【讨论】:

      猜你喜欢
      • 2019-01-25
      • 1970-01-01
      • 1970-01-01
      • 2012-01-26
      • 2014-09-12
      • 1970-01-01
      • 1970-01-01
      • 2013-09-25
      • 1970-01-01
      相关资源
      最近更新 更多