繁体   English   中英

如何从另一个框架打开框架 Java Swing

[英]how to open frame from another frame Java Swing

我正在制作一个有多个子程序的程序,但是当我试图打开一个新框架时,它什么也没做。 调用框架的程序:

import javax.swing.*;
import java.awt.*;

public class Main {
  public static void main(String[] args) {
    JFrame frame = new JFrame("Options");
    JButton notepad = new JButton("Notepad");
    JButton todo = new JButton("To-Do List");
    NoteListe noteListener = new NoteListe();
    
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500,500);
    frame.setLayout(new GridLayout(1,2));

    frame.add(notepad);
    frame.add(todo);

    notepad.addActionListener(noteListener);
    
  }
}

记事本按钮的动作监听器:

import java.awt.event.*;

public class NoteListe implements ActionListener{
  public void actionPerformed(ActionEvent e){
    new MainNote();
  }
}

记事本:

import javax.swing.*;
import java.awt.*;

public class MainNote {
  public static void main(String[] args) {
    JFrame frame = new JFrame("Text Editor");

    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500,500);
    frame.setLayout(new GridLayout(2,1));
  }
}

我真的认为你需要仔细看看:

这些是真正的基本概念,在您冒险进入 GUI 开发的狂野和无情的世界之前,您应该牢牢掌握它们。

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;

public class Main {

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

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new MenuPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class MenuPane extends JPanel {

        public MenuPane() {
            setLayout(new GridBagLayout());
            setBorder(new EmptyBorder(32, 32, 32, 32));

            JButton notepad = new JButton("Notepad");
            JButton todo = new JButton("To-Do List");

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.fill = GridBagConstraints.BOTH;
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.insets = new Insets(8, 8, 8, 8);

            add(notepad, gbc);
            add(todo, gbc);

            notepad.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    MainNote note = MainNote.show(MenuPane.this);
                    note.setText("This is where your text goes");
                }
            });
        }

    }

    public static class MainNote extends JPanel {

        private JTextArea ta;

        public MainNote() {
            setLayout(new BorderLayout());
            ta = new JTextArea(20, 40);
            add(new JScrollPane(ta));
        }

        public void setText(String text) {
            ta.setText(text);
        }

        public String getText() {
            return ta.getText();
        }

        public static MainNote show(Component parent) {
            MainNote mainNote = new MainNote();
            JFrame frame = new JFrame("Text Editor");
            frame.add(mainNote);
            frame.pack();
            frame.setLocationRelativeTo(parent);
            frame.setVisible(true);
            return mainNote;
        }
    }
}

注意:我使用了static方法 ( show ) 来简化另一个show的构造。 这只是一个委托工作流程,因为我将创建 window 的责任委托给该方法,但我仍在取回MainNote的一个实例。

因为MainNote只是一个JPanel ,所以它可以添加到我想要的任何容器中。 因为我已将功能“封装”到 class 本身中,所以它更易于管理。

您需要使框架可见。

import java.awt.event.*;

public class NoteListe implements ActionListener{
  public void actionPerformed(ActionEvent e){
    new MainNote().setVisible(true);  <==== set it to visible.
  }
}

暂无
暂无

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

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