我不知道如何进行如下布局:
原谅我写的很烂。我的平板电脑无处可寻,所以我不得不使用鼠标。无论如何,我只是想知道如何用我的井字游戏正确地做类似的事情。
我已经得到了应该在的网格,但我这辈子也无法在屏幕上获得标题。我也想知道如何做一个简单的纯色条,如图所示。我也尝试过,但它不会总是出现。有时它会闪烁,有时它会闪烁然后消失,或者根本不出现。
我会说这可能与我设置布局的方式有关,但我不知道如何做网格。哦!我还想,如果我弄清楚整个面板添加到框架的事情出了什么问题,我可能会弄清楚为什么当您尝试单击已占用的按钮时应该显示的红色错误文本没有出现。我很确定这是同样的问题。
这是我的代码:
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);
}
}
}
}
感谢您的帮助并原谅我可能糟糕的编码逻辑。