1. 程式人生 > >java點選按鈕,出現另一個視窗

java點選按鈕,出現另一個視窗

第一個窗體LoginFrame.java:
package winRelation;

import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class LoginFrame extends JFrame {
	JButton button = new JButton("點選我");
	
	class LoginOKAction implements ActionListener {
		
		public void actionPerformed(ActionEvent e) {
			JOptionPane.showMessageDialog(null, "將進入另一個窗體!");
			new MainFrame();
			setVisible(false);
		}
	}
	public LoginFrame(){
		super();
		this.setResizable(false);
		this.setSize(new Dimension(300, 205));
		this.setTitle("第一個窗體");
		this.setLayout(null);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setLocation(300, 200);
		this.setVisible(true);
		
		this.getContentPane().add(button, null);
		button.setBounds(new Rectangle(111, 70, 78, 27));
		button.addActionListener(new LoginOKAction());//給按鈕加事件監聽器
	}
	public static void main(String[] args) {
		new LoginFrame();
	}
}

第二個窗體MainFrame.java:
package winRelation;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JToolBar;

public class MainFrame extends JFrame{

    private static final JDesktopPane DESKTOP_PANE = new JDesktopPane();

    public MainFrame() {
        super("這是主窗體");
        setSize(640, 480);

        //選單設定
        JMenuBar menuBar = new JMenuBar();
        this.setJMenuBar(menuBar);
        JMenu menu1 = new JMenu("選單1");
        JMenu menu101 = new JMenu("選單101");
        JMenuItem menu10101 = new JMenuItem("選單10101");
        JMenuItem menu102 = new JMenuItem("選單102");
        menu102.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                addIFame(new InternalFrame());
            }
        });
        menu101.add(menu10101);
        menu1.add(menu101);
        menu1.add(menu102);
        JMenu menu2 = new JMenu("選單2");
        menuBar.add(menu1);
        menuBar.add(menu2);

        this.getContentPane().add(DESKTOP_PANE);
        this.setVisible(true);
    }

    public static void addIFame(JInternalFrame iframe) { // 新增子窗體的方法
		DESKTOP_PANE.add(iframe);
	}

    public static void main(String[] args) {
        new MainFrame();
    }
}

第三個窗體(MainFrame中的窗體)InternalFrame.java:
package winRelation;

import javax.swing.JButton;
import javax.swing.JInternalFrame;

public class InternalFrame extends JInternalFrame{

    public InternalFrame() {
        super();
        setClosable(true);
        setIconifiable(true);
        setTitle("內部窗體");
        setBounds(50,50,400,300);
        setVisible(true);
    }
}