简体   繁体   English

单击按钮后JFrame无法打开

[英]JFrame not opening when a button is clicked

I have two JFrame . 我有两个JFrame

  1. public class Main extends JFrame
  2. public class ColourOption extends JPanel implements ActionListener which is then set up in a JFrame. public class ColourOption extends JPanel implements ActionListener ,然后在JFrame中对其进行设置。

I wanted to open the second JFrame when i click on button of first JFrame 我想在单击第一个JFrame的按钮时打开第二个JFrame
.setVisible() is not working. .setVisible()无法正常工作。 I also tried revalidate() , as well as invalidate() , validate() in the second JFrame. 我还在第二个JFrame中尝试了revalidate()以及invalidate()validate()

What could be the reason for it to not work? 它不起作用的原因可能是什么?

You will have to instantiate the 2nd class which has the 2nd Frame(to be shown)..and then if you call the setVisible(true) .. then it must show .. what you doing .. could you provide your button's event handler.. 您将必须实例化具有第二帧(要显示)的第二类。然后,如果调用setVisible(true)..那么它必须显示..您在做什么..您能否提供按钮的事件处理程序..

and this is not good practice 这不是一个好习惯

so personally i would recommend you to switch over to better alternatives like JTABBEDPANES or CARDLAYOUT 所以我个人建议您切换到更好的替代品,例如JTABBEDPANES或CARDLAYOUT

and consider the comments as well .. good comments guys :) .. especially using JDialog for this context :) 并考虑评论..好评论的人:) ..特别是在这种情况下使用JDialog :)

well if you still want help in your context: a sample: 如果您仍然需要帮助,可以使用以下示例:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class JFrame1 extends JFrame
{
    public JFrame1()
    {
        setLayout(new FlowLayout());
        JButton b=new JButton("Click");
        add(b);
        b.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                JFrame jf = new JFrame2();
                jf.setVisible(true);
                jf.setSize(200, 200);
                jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            }
        }
        );
}
    public static void main(String args[])
    {
        JFrame jf = new JFrame1();
        jf.setVisible(true);
        jf.setSize(200, 200);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

and the second class: 第二类:

import javax.swing.*;
import java.awt.*;
class JFrame2 extends JFrame
{
    public JFrame2()
    {
        setLayout(new FlowLayout());
        add(new JLabel("2nd Frame"));
    }
}    

But again i would still recommend to switch to other methods as i mentioned earlier: tabbedpanes, cardlayout etc.. Hope i helped :) 但是我仍然还是建议切换到其他方法,如我之前提到的:tabbedpanes,cardlayout等。希望我有所帮助:)

Since they are from 2 different classes, you just have to define/instantiate an object of the other class... and if within that 2nd class (ColourOption) it already contains setVisible(true) then there must be no problem loading the window. 因为它们来自2个不同的类,所以您只需要定义/实例化另一个类的对象...并且如果在第二个类(ColourOption)中它已经包含setVisible(true),则加载窗口一定没有问题。

 //this will be placed on your constructor
 yourButton.addActionListener(new ButtonListener());

 //listener class
 class ButtonListener implements ActionListener{
   public void actionPerformed(ActionEvent ae){
      if(ae.getSource() == yourButton){
         new ColourOption();
      }
   }
}

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

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