单击一个窗口按钮设置另一个窗口可见

/通过一个MyFrame1234 窗口上的按钮true设置MyFrame123这个窗口为可见

//通过一个MyFrame1234 窗口上的按钮true设置MyFrame123这个窗口为可见
package com.zeng.GUI;

import java.awt.*;
import java.awt.event.*;

public class TextFrameListener {
    public static void main(String[] args) {
        MyFrame123 myFrame = new MyFrame123();
        new MyFrame1234(myFrame);
    }
}

//窗口1
class MyFrame1234 extends Frame {
    MyFrame123 myFrame123 = null;//用来接收要设置为可见的窗口

    public MyFrame1234(MyFrame123 myFrame123) {//构造函数参数为一个要显示的窗口类型
        this.myFrame123 = myFrame123;
        setBounds(100, 100, 400, 400);
        setLayout(new FlowLayout(FlowLayout.RIGHT));
        setVisible(true);
        Button button3 = new Button("True");
        add(button3);
        button3.addActionListener(new MyButton123Listener(myFrame123));
    }
}

//窗口二
class MyFrame123 extends Frame {
    public MyFrame123() {
        setBounds(400, 100, 800, 800);
        setLayout(new FlowLayout(FlowLayout.RIGHT));
        setVisible(true);
        Button button = new Button("Goodbye");
        Button button1 = new Button("Welcome");
        add(button);
        add(button1);
        button.addActionListener(new MyButton123Listener(this));
        button1.addActionListener(new MyButton123Listener(this));
        addWindowListener(new FrameWindowListener());
    }
}

//按钮监听类
class MyButton123Listener implements ActionListener {
    MyFrame123 frame;

    public MyButton123Listener(MyFrame123 frame) {
        this.frame = frame;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if ("Welcome".equals(e.getActionCommand())) {//窗口按钮名和串welcome相等就把当前窗口设置为可见
            System.out.println("设置窗口可见");
            frame.setVisible(true);

        }
        if ("Goodbye".equals(e.getActionCommand())) {//窗口按钮名和串Goodbye相等就把当前窗口设置为不可见
            System.out.println("设置窗口不可见");
            frame.setVisible(false);
        }
        if ("True".equals(e.getActionCommand())) {//窗口按钮名和串true相等就把当前窗口设置为可见
            System.out.println("设置窗口可见");
            frame.setVisible(true);
        }
    }
}

//windowListener里的方法
class FrameWindowListener implements WindowListener {

    @Override
    public void windowOpened(WindowEvent e) {
        System.out.println("windowOpened");
    }

    @Override
    public void windowClosing(WindowEvent e) {
        System.out.println(" windowClosing");
        System.exit(0);
    }

    @Override
    public void windowClosed(WindowEvent e) {
        System.out.println("windowClosed");
    }

    @Override
    public void windowIconified(WindowEvent e) {
        System.out.println("windowIconified");
    }

    @Override
    public void windowDeiconified(WindowEvent e) {
        System.out.println("windowDeiconified ");
    }

    @Override
    public void windowActivated(WindowEvent e) {

    }

    @Override
    public void windowDeactivated(WindowEvent e) {

    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值