简体   繁体   English

如何使按钮将您带到GUI Java中的另一个框架?

[英]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) 代替使用JFrameadd()方法,使用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.

相关问题 单击按钮后如何使JTextField出现并且仅接受与另一个字符串相同长度的字符-Java GUI - How to make JTextField appear after a button click and only accept the same length of characters as another string -Java GUI 如何在GUI中为一个按钮创建多个侦听器? - How do you make multiple listeners for one button in GUI? 如何用Java中的另一个框架制作开始菜单? - How to make a start menu with another frame in Java? 如何在 Java 中创建一个创建文本字段的 GUI 按钮? - How do you create a GUI button in Java that creates a text field? Java-GUI:将值从框架传递到另一个 - Java - GUI: Passing values from frame to another Java GUI构建器-如何制作不规则形状的按钮 - Java GUI builder - How to make an irregular shaped button 如果在java swing中选择了另一个jbutton,如何在执行的按钮操作中创建一个案例? - How can you make a case inside a button action performed, based off if another jbutton is chosen in java swing? 当我点击打开另一个框架的按钮时,如何将此框架与另一个框架链接? - Java - How to link this frame with another frame when I click a button that opens another frame?- Java 如何为java.awt.Frame创建键绑定? - How do you make key bindings for a java.awt.Frame? Java GUI - JButton从另一个类打开另一个框架 - Java GUI - JButton opens another frame from another class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM