簡體   English   中英

打開一個JFrame到另一個JFrame

[英]Open a JFrame into another JFrame

我正在創建一個程序,我的主類可以與所有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();
        }
    });
}

}

我相信這就是所有代碼,而且我不太習慣如何在堆棧溢出中格式化代碼,因此我可能會弄亂一些東西。 我可能只是搞砸了,所以對此感到抱歉,但請先感謝。

主要的問題是在你的createAndShowGUIInfoBook類...

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和我假設你想顯示在屏幕上,所以從類中獲取信息將是不可能的

我懷疑,您實際上想使用某種JDialog代替,它將允許您向用戶展示一個窗口,但是它將阻止代碼的執行,直到用戶關閉該窗口,然后您才可以進行查詢提供信息的對象。

有關更多詳細信息,請參見如何制作對話框

  • 通常,您不希望從JFrameJDialog類的頂級容器擴展,而是希望使用JPanel作為基本組件,並將其添加到所需的任何容器中。
  • 避免使用null布局,像素完美布局是現代ui設計中的一種幻覺。 有太多因素會影響組件的單個大小,您無法控制。 Swing旨在與布局經理為核心一起工作,舍棄這些問題不會導致問題和問題的終結,您將花費越來越多的時間來嘗試糾正
  • 您可能還想看看“使用多個JFrame,良好/不良做法”?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM