java - 将一个 JFrame 打开到另一个 JFrame 中

标签 java eclipse swing

我正在创建一个程序,并且我的主类可以与所有 JButton 一起使用,但是我无法获得使用按钮调用第一类启动的第二类。 JFrame 将启动,但按钮不会启动。

我使用 eclipse 只是为了获取信息。

这是我的主类的代码:

    public class Unescapable extends JPanel implements ActionListener
{

private static final long serialVersionUID = 1L;
private static final Font font1 = new Font("FONT", Font.BOLD, 75);
protected JButton b1;
protected JButton b2;
protected JButton b3;
protected JButton b5;
protected JTextField t1;

    public Unescapable() 
    {
        t1 = new JTextField("Unescapable");
        t1.setText("Unescapable");
        t1.setBounds(225, 50, 750, 100);
        t1.setForeground(Color.LIGHT_GRAY);
        t1.setOpaque(true);
        t1.setVisible(true);
        t1.setEditable(false);
        t1.setBackground(Color.BLACK);
        t1.setFont(font1);

        b1 = new JButton("Open Window");
        b1.setActionCommand("open");
        b1.setBackground(Color.GRAY);
        b1.setForeground(Color.white);
        b1.setOpaque(true);
        b1.setBorderPainted(false);
        b1.setBounds(280, 200, 390, 40);
        b1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                b1.setBackground(Color.BLUE);
            }

            public void mouseExited(java.awt.event.MouseEvent evt) {
                b1.setBackground(Color.GRAY);
            }
        });

        b2 = new JButton("Delete File");
        b2.setActionCommand("delete");
        b2.setBackground(Color.GRAY);
        b2.setForeground(Color.white);
        b2.setOpaque(true);
        b2.setBorderPainted(false);
        b2.setBounds(280, 255, 390, 40);
        b2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                b2.setBackground(Color.BLUE);
            }

            public void mouseExited(java.awt.event.MouseEvent evt) {
                b2.setBackground(Color.GRAY);
            }
        });

        b3 = new JButton("Info...");
        b3.setActionCommand("info");
        b3.setBackground(Color.GRAY);
        b3.setForeground(Color.white);
        b3.setOpaque(true);
        b3.setBorderPainted(false);
        b3.setBounds(278, 330, 185, 40);
        b3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                b3.setBackground(Color.BLUE);
            }

            public void mouseExited(java.awt.event.MouseEvent evt) {
                b3.setBackground(Color.GRAY);
            }
        });

        b5 = new JButton("Quit Game");
        b5.setActionCommand("close");
        b5.setBackground(Color.GRAY);
        b5.setForeground(Color.white);
        b5.setOpaque(true);
        b5.setBorderPainted(false);
        b5.setBounds(485, 330, 185, 40);
        b5.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                b5.setBackground(Color.BLUE);
            }

            public void mouseExited(java.awt.event.MouseEvent evt) {
                b5.setBackground(Color.GRAY);
            }
        });

        b1.addActionListener(this);
        b2.addActionListener(this);
        b3.addActionListener(this);
        b5.addActionListener(this);

        b1.setToolTipText("Opens Another JWindow");
        b2.setToolTipText("Deletes \"text.txt\"");
        b3.setToolTipText("Give's some information.");
        add(b1);
        add(b2);
        add(b3);
        add(b5);
        add(t1);
        System.out.println("Main Window Is Running");
    }

    public void actionPerformed(ActionEvent e) 
    {
        if ("open".equals(e.getActionCommand()))
        {
            File f = new File("text.txt");
            try
            {
                PrintWriter out = new PrintWriter(f);
                out.println("TheBestMacTutorials");
                out.close();
            }
            catch (Exception e2)
            {

            }
        } 
        else if ("delete".equals(e.getActionCommand()))
        {
            File f = new File("text.txt");
            f.delete();
        }
        else if ("info".equals(e.getActionCommand()))
        {
            InfoBook add = new InfoBook();
            add.call();
        }
        else
        {
            System.out.println("Window Is Now Closing");
            System.exit(0);
        }
    }

    private static void createAndShowGUI() 
    {
        JFrame program = new JFrame("My Program");
        program.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Unescapable newContentPane = new Unescapable();
        program.setContentPane(newContentPane);

        program.setLayout(null);
        program.setVisible(true);
        program.setLocation(850, 445);
        program.setSize(900, 580);
        program.setTitle("Unescapable 1.0");
        program.setBackground(Color.GREEN);
        program.isOpaque();
        program.isForegroundSet();
        program.getContentPane().setBackground(Color.BLACK);

    }

    public static void main(String[] args) 
    {   

        javax.swing.SwingUtilities.invokeLater(new Runnable() 
        {
                public void run() 
                {
                    createAndShowGUI(); 
            }
      });
 }
    }

“信息手册”的代码:

    public class InfoBook extends JFrame
{
private static final long serialVersionUID = 1L;
private static final Font font1 = new Font("FONT", Font.BOLD, 75);
protected JButton b1;
protected JButton b2;
protected JTextField t1;

public InfoBook()
{
    b1 = new JButton("Cancel");
    b1.setActionCommand("cancel");
    b1.setBackground(Color.GRAY);
    b1.setForeground(Color.white);
    b1.setOpaque(true);
    b1.setBorderPainted(false);
    b1.setBounds(280, 200, 390, 40);
    b1.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseEntered(java.awt.event.MouseEvent evt) {
            b1.setBackground(Color.BLUE);
        }

        public void mouseExited(java.awt.event.MouseEvent evt) {
            b1.setBackground(Color.GRAY);
        }
    });

    b2 = new JButton("More Info");
    b2.setBackground(Color.GRAY);
    b2.setForeground(Color.WHITE);
    b2.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseEntered(java.awt.event.MouseEvent evt) {
            b2.setBackground(Color.BLUE);
        }

        public void mouseExited(java.awt.event.MouseEvent evt) {
            b2.setBackground(Color.GRAY);
        }
    });

    t1 = new JTextField("Info");
    t1.setText("Info...");
    t1.setBounds(225, 50, 750, 100);
    t1.setForeground(Color.LIGHT_GRAY);
    t1.setOpaque(true);
    t1.setVisible(true);
    t1.setEditable(false);
    t1.setBackground(Color.BLACK);
    t1.setFont(font1);

    b1.setToolTipText("Opens Another JWindow");
    b2.setToolTipText("Gives More Infomation");

    add(b1);
    add(b2);
    add(t1);
    System.out.println("Info is now running");
}

public static void creatAndShowGUI()
{
    JFrame frame = new JFrame("Info Panel");
    frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
    InfoBook newContentPane = new InfoBook();

    frame.setLayout(null);
    frame.setVisible(true);
    frame.setLocation(850, 445);
    frame.setSize(900, 580);
    frame.setTitle("Unescapable 1.0");
    frame.setBackground(Color.GREEN);
    frame.isOpaque();
    frame.isForegroundSet();
    frame.getContentPane().setBackground(Color.BLACK);

}
public static void call()
{

    javax.swing.SwingUtilities.invokeLater(new Runnable()
    {
        public void run()
        {
            creatAndShowGUI();
        }
    });
}

}

我相信这就是所有的代码,而且我不太习惯如何在堆栈溢出中格式化代码,所以我可能会搞砸一些东西。我可能只是把一些事情搞砸了,所以我对此感到抱歉,但提前致谢。

最佳答案

主要问题出在 InfoBook 类中的 createAndShowGUI...

public static void creatAndShowGUI() {
    JFrame frame = new JFrame("Info Panel");
    frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
    InfoBook newContentPane = new InfoBook();

    frame.setLayout(null);
    frame.setVisible(true);
    frame.setLocation(850, 445);
    frame.setSize(900, 580);
    frame.setTitle("Unescapable 1.0");
    frame.setBackground(Color.GREEN);
    frame.isOpaque();
    frame.isForegroundSet();
    frame.getContentPane().setBackground(Color.BLACK);

}

本质上,您创建了一个 newContentPane 实例,但您从不使用它。

但是,您即将遇到第二个问题......

} else if ("info".equals(e.getActionCommand())) {
    InfoBook add = new InfoBook();
    add.call();

在这里,您创建了一个 InfoBook 实例,但该实例与您的 creatAndShowGUI 中创建的实例没有任何关系,我假设您想要在其上显示屏幕,因此无法从类(class)获取信息

我怀疑,您实际上想使用某种JDialog,这将允许您向用户呈现一个窗口,但它将阻止代码的执行,直到用户关闭窗口,此时您可以询问该对象以获取其信息。

参见How to Make Dialogs了解更多详情

  • 根据一般经验,您不希望从 JFrameJDialog 等顶级容器进行扩展,而是希望使用 JPanel 作为基本组件,并将它们添加到您需要的任何容器中。
  • 避免使用 null 布局,像素完美布局是现代 UI 设计中的一种幻觉。影响组件个体尺寸的因素太多,您无法控制其中任何一个。 Swing 的设计目的是与核心的布局管理器一起工作,放弃这些将导致无休止的问题和问题,您将花费越来越多的时间来尝试纠正
  • 您可能还想查看The Use of Multiple JFrames, Good/Bad Practice?

关于java - 将一个 JFrame 打开到另一个 JFrame 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33930312/

相关文章:

java - 使用 BorderLayout 从 JPanel 中删除 CENTER 元素

java - JFrame 中的 JPanel

java - 使用 JMS 监听器自动刷新 Wicket 组件

java - ILazyContentProvider 在每个 viewer.setItemCount() 更新所有内容

java - 如何修改第三方代码

java.text.ParseException : Unparseable date: "11/11/2014"

java - Android 设备监视器未在 MAC 上的 Android Studio 中启动

java - 在 Eclipse 中,如何在过滤器上键入结束时刷新 View ?

java - 如何将图像高度调整为 JFrame 高度?

java - 联系许可服务器