/通过一个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) {
}
}
此篇博客展示了如何通过MyFrame1234窗口上的按钮,实现对MyFrame123窗口的可见性控制,通过ActionListener监听并响应不同按钮的点击事件来设置窗口状态。
8057

被折叠的 条评论
为什么被折叠?



