繁体   English   中英

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

[英]How to make a button take you to another frame in GUI java?

我想通过单击“ PLAY ME”按钮转到“连接四人”的另一个框架,但是我对如何做到这一点感到非常困惑。 在这里,我有连接四的开始页面的代码以及在页面上设置的标签和按钮。 这是我的代码:

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

隐藏您的第一帧并将第二帧的可见性设置为true

        btnplay.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
       second_add second = new second_add();   
       setVisible(false); // Hide current frame
       second.setVisible(true);
        }
    });

与其更改框架,不如更改面板。 代替使用JFrameadd()方法,使用setContentPane(Container container)

除了在框上添加按钮和不添加按钮外,还可以创建另一个包装器面板并在其上使用BorderLayout,然后将该面板添加到框架中:

例:

JPanel wrapper = new JPanel(new BorderLayout());
wrapper.add(play, BorderLayout.SOUTH);
//etc.   
frame.setContentPane(wrapper);

然后,要处理按钮单击,请实现ActionListener接口。 不要在主要班级上这样做-您将需要多个。 使用匿名内部类或lambda表达式。 例:

play.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        frame.setContentPane(someOtherJPanel);
    }
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM