1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 通过点击按钮打开新的窗口

通过点击按钮打开新的窗口

时间:2023-04-18 10:58:24

相关推荐

通过点击按钮打开新的窗口

通过点击按钮打开新的窗口

通过点击某个按键打开新的窗口,原来的窗口关闭或者不关闭;

主要语句

//创建新的界面A a=new A();//将新的界面展示出来,此参数设置为trueA.setVisible(true);//将旧的界面隐藏,此参数设置为falseB.setVisible(false);

完整代码

界面A

import java.awt.BorderLayout;import java.awt.EventQueue;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.border.EmptyBorder;import javax.swing.JButton;import java.awt.Color;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;public class A extends JFrame {/*** */private static final long serialVersionUID = 1L;private JPanel contentPane;/*** Launch the application.*/public static void main(String[] args) {EventQueue.invokeLater(new Runnable() {public void run() {try {A frame = new A();frame.setVisible(true);} catch (Exception e) {e.printStackTrace();}}});}/*** Create the frame.*/public A() {setTitle("\u8FD9\u662F\u754C\u9762A");setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(100, 100, 450, 300);contentPane = new JPanel();contentPane.setBackground(Color.ORANGE);contentPane.setForeground(new Color(255, 0, 0));contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));setContentPane(contentPane);contentPane.setLayout(null);JButton btna = new JButton("\u8FD4\u56DE\u754C\u9762B");btna.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {B b=new B();b.setVisible(true);A.this.setVisible(false);}});btna.setBounds(168, 113, 97, 23);contentPane.add(btna);}}

界面B

import java.awt.BorderLayout;import java.awt.EventQueue;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.border.EmptyBorder;import java.awt.Color;import javax.swing.JButton;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;public class B extends JFrame {/*** */private static final long serialVersionUID = 1L;private JPanel contentPane;/*** Launch the application.*/public static void main(String[] args) {EventQueue.invokeLater(new Runnable() {public void run() {try {B frame = new B();frame.setVisible(true);} catch (Exception e) {e.printStackTrace();}}});}/*** Create the frame.*/public B() {setTitle("\u8FD9\u662F\u754C\u9762B");setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(100, 100, 450, 300);contentPane = new JPanel();contentPane.setBackground(Color.PINK);contentPane.setToolTipText("");contentPane.setForeground(Color.CYAN);contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));setContentPane(contentPane);contentPane.setLayout(null);JButton btna = new JButton("\u8FD4\u56DE\u754C\u9762A");btna.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {A a=new A();a.setVisible(true);B.this.setVisible(false);}});btna.setBounds(139, 112, 97, 23);contentPane.add(btna);}}

结果展示

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。