[英]How to make a button take you to another frame in GUI java?
I would like to go to another frame of Connect Four by clicking the "PLAY ME" button, but I am very confused on how to do so. 我想通过单击“ PLAY ME”按钮转到“连接四人”的另一个框架,但是我对如何做到这一点感到非常困惑。 Here, I have the code for the opening page of connect four and labels and buttons set up on the page. 在这里,我有连接四的开始页面的代码以及在页面上设置的标签和按钮。 Here is my code: 这是我的代码:
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);
}
}
Hide Your First Frame And Set Visiblity of second frame to true 隐藏您的第一帧并将第二帧的可见性设置为true
btnplay.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
second_add second = new second_add();
setVisible(false); // Hide current frame
second.setVisible(true);
}
});
Rather than changing the frame, change the panel. 与其更改框架,不如更改面板。 Instead of using JFrame
's add()
method, use setContentPane(Container container) 代替使用JFrame
的add()
方法,使用setContentPane(Container container)
Instead of adding buttons and what not to the frame, create another wrapper panel and use the BorderLayout on that, and then add that panel to the frame: 除了在框上添加按钮和不添加按钮外,还可以创建另一个包装器面板并在其上使用BorderLayout,然后将该面板添加到框架中:
Example: 例:
JPanel wrapper = new JPanel(new BorderLayout());
wrapper.add(play, BorderLayout.SOUTH);
//etc.
frame.setContentPane(wrapper);
Then, to handle the button click, implement the ActionListener
interface. 然后,要处理按钮单击,请实现ActionListener
接口。 Don't do it with the main class - you'll need more than one. 不要在主要班级上这样做-您将需要多个。 Use an anonymous inner class, or a lambda expression. 使用匿名内部类或lambda表达式。 Example: 例:
play.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.setContentPane(someOtherJPanel);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.