我想通过点击“PLAY ME”按钮进入Connect Four的另一个框架,但我对如何操作非常困惑。在这里,我有连接四的开始页面的代码以及页面上设置的标签和按钮。 这是我的代码:
import java.awt.*;
import javax.swing.event.*;
import java.awt.Color.*;
import javax.swing.*;
import java.util.*;
public class Game implements ActionListener{
public Game(){
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setSize(new Dimension(1000,1000));
frame.setTitle("Connect Four");
frame.setLayout(new BorderLayout());
JButton play = new JButton();
play.setPreferredSize(new Dimension(75,150));
play.setBackground(Color.RED);
play.setFont(new Font("Arial", Font.PLAIN, 40));
play.setForeground(Color.BLACK);
play.setText("CLICK ME TO PLAY");
frame.add(play, BorderLayout.SOUTH);
JPanel north = new JPanel(new BorderLayout());
JLabel title = new JLabel("Connect Four");
north.setBackground(Color.WHITE);
title.setFont(new Font("Arial", Font.PLAIN, 100));
title.setForeground(Color.RED);
title.setHorizontalAlignment(0);
title.setVerticalAlignment(1);
frame.add(north, BorderLayout.NORTH);
north.add(title);
JPanel intro = new JPanel(new GridLayout(10,1));
intro.setBackground(Color.WHITE);
JLabel instructions = new JLabel("Instructions");
instructions.setFont(new Font("Ariel", Font.PLAIN, 70));
JLabel instructionsPart1 = new JLabel("Both players will be assigned a color, either red or black.");
JLabel instructionsPart2 = new JLabel("Players will take turns placing the colored discs on to the board.");
JLabel instructionsPart3 = new JLabel("The OBJECTIVE is to get four of one colored discs in a row.");
instructionsPart1.setFont(new Font("Ariel", Font.PLAIN, 35));
instructionsPart2.setFont(new Font("Ariel", Font.PLAIN, 35));
instructionsPart3.setFont(new Font("Ariel", Font.PLAIN, 35));
intro.add(instructions);
intro.add(new JLabel(""));
intro.add(instructionsPart1);
intro.add(new JLabel(""));
intro.add(instructionsPart2);
intro.add(new JLabel(""));
intro.add(instructionsPart3);
intro.add(new JLabel(""));
frame.add(intro, BorderLayout.CENTER);
frame.add(intro);
}
}
答案 0 :(得分:0)
隐藏您的第一帧并将第二帧的Visiblity设置为true
btnplay.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
second_add second = new second_add();
setVisible(false); // Hide current frame
second.setVisible(true);
}
});
答案 1 :(得分:0)
不要更改框架,而是更改面板。不使用std::vector<unsigned char>
JFrame
方法,而是使用setContentPane(Container container)
不是向框架添加按钮和不添加按钮,而是创建另一个包装器面板并在其上使用BorderLayout,然后将该面板添加到框架中:
示例:
add()
然后,要处理按钮单击,请实现JPanel wrapper = new JPanel(new BorderLayout());
wrapper.add(play, BorderLayout.SOUTH);
//etc.
frame.setContentPane(wrapper);
界面。不要在主要班级做这件事 - 你需要不止一件。使用匿名内部类或lambda表达式。例如:
ActionListener