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

public class Class4 {

    public static final long serialVersionUID = 1L;

    public void mainMethod(int event){
        JFrame f = new JFrame("Love Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(500,200);     
        f.setLayout(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();

        if(event == 0){




            JPanel p = new JPanel();
            p.setLayout(new BoxLayout(p,BoxLayout.Y_AXIS));
            p.setBounds(150, 0, 500, 75);
            p.setPreferredSize(new Dimension(150,75));
            JTextField boy = new JTextField();
            boy.setMaximumSize(new Dimension(200,40));
            JTextField girl = new JTextField();
            girl.setMaximumSize(new Dimension(200,40));
            p.add(boy);
            p.add(girl);
            gbc.insets = new Insets(-90,310,0,0);
            gbc.gridx = 0;
            gbc.gridy = 0;
            f.add(p,gbc);

            JPanel p3 = new JPanel(new BorderLayout());
            p3.setBounds(0, 0, 150, 75);
            p3.setPreferredSize(new Dimension(150,75));
            Class5 c5o = new Class5();
            c5o.setPreferredSize(new Dimension(150,75));
            p3.add(c5o);
            gbc.insets = new Insets(0,0,90,330);
            gbc.gridx = 0;
            gbc.gridy = 0;
            f.add(p3,gbc);

            JPanel p2 = new JPanel(new FlowLayout());
            Class7 c7o = new Class7();
            p2.add(c7o);
            p2.setPreferredSize(new Dimension(300,40));
            gbc.insets = new Insets(0,0,-20,0);
            gbc.gridx = 0;
            gbc.gridy = 0;
            f.add(p2,gbc);

            f.setVisible(true);

            //1st
            JOptionPane.showMessageDialog(null,f.isVisible());


        }

        if(event == 5){

            JPanel p4 = new JPanel(new BorderLayout());
            p4.setBounds(0,140,500,55);
            Class2 c2o = new Class2();
            Dimension d2 = new Dimension(500,55);
            c2o.setPreferredSize(d2);
            p4.setPreferredSize(d2);
            p4.add(c2o);
            gbc.insets = new Insets(0,0,-130,0);
            gbc.gridx = 0;
            gbc.gridy = 0;  
            f.add(p4,gbc);

            f.invalidate();
            f.validate();
            f.repaint();

            //2nd
            JOptionPane.showMessageDialog(null,f.isVisible());
        }

    }

}

重新验证和重新绘制不起作用。我有一个布局管理器,所以我怀疑问题出在 if 语句上。我第一次测试 f 的可见性时,它返回了 true。我第二次测试同样的东西,它返回错误。为什么我的重新验证和重新绘制不起作用?我需要怎么做才能让它工作?

4

3 回答 3

4

此代码存在一些问题,但主要问题是您的JFrame参考范围f

假设您正在以某种方式调用此main示例中的方法:

public static void main(String[] args) {
  Class4 c = new Class4();
  c.mainMethod(0);
  c.mainMethod(5);
}

然后,您的代码在第二次调用期间创建第二JFrame次。

将 JFrame 创建移到构造函数中并创建f一个成员变量:

public class Class4 {

    private JFrame f;

    public Class4() {
       f = new JFrame("Love Test");
       f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       f.setSize(500,200);     
       f.setLayout(new GridBagLayout());
    }

    // ...
}

然后,您可以在后续调用mainMethod().

一些附加说明:

  • 避免重复使用 GridBagConstraints。通过构造函数创建它们。这使得代码更加健壮。
  • 您不再需要contentPane从 the中获取JFrame(如其他答案中所建议的那样)。从 Java 1.5 开始,直接委托给内容窗格的add()方法。JFrame
  • 不需要做invalidate()repaint()在向JFrame. add()已经使组件层次结构无效。但是,您需要validate()在组件已经显示时调用(如您的情况)。有关更多信息,请参阅java.awt.Container.add()
于 2013-06-05T12:03:01.157 回答
3
 if(event == 5){

        JPanel p4 = new JPanel(new BorderLayout());
        p4.setBounds(0,140,500,55);
        Class2 c2o = new Class2();
        Dimension d2 = new Dimension(500,55);
        c2o.setPreferredSize(d2);
        p4.setPreferredSize(d2);
        p4.add(c2o);
        gbc.insets = new Insets(0,0,-130,0);
        gbc.gridx = 0;
        gbc.gridy = 0;  
        f.add(p4,gbc);

        f.invalidate();
        f.validate();
        f.repaint();

        //2nd
        JOptionPane.showMessageDialog(null,f.isVisible());
    }

f它不可见的框架(缺少f.setVisible(true);),因此在这种情况下不需要重新验证和重新绘制,用户将看不到它。还要使用记录器或断点进行测试f.isVisible(),因为这会阻止用户界面。

于 2013-06-05T11:41:03.903 回答
2

我无法准确查看您要查找的内容。SSCCE 真的很有帮助。这是你要找的吗?

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

class Canv extends JComponent {

    GridBagConstraints gbc;

    public Canv (int event) {

        gbc = new GridBagConstraints();

        switch(event)
        {
            case 0:
                this.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
                this.setBounds(150, 0, 500, 75);
                this.setPreferredSize(new Dimension(150,75));
                JTextField boy = new JTextField();
                boy.setMaximumSize(new Dimension(200,40));
                JTextField girl = new JTextField();
                girl.setMaximumSize(new Dimension(200,40));
                this.add(boy);
                this.add(girl);
                gbc.insets = new Insets(-90,310,0,0);
                gbc.gridx = 0;
                gbc.gridy = 0;
                this.add(this,gbc);

                JPanel p3 = new JPanel(new BorderLayout());
                p3.setBounds(0, 0, 150, 75);
                p3.setPreferredSize(new Dimension(150,75));
                //Class5 c5o = new Class5();
                //c5o.setPreferredSize(new Dimension(150,75));
                //p3.add(c5o);
                gbc.insets = new Insets(0,0,90,330);
                gbc.gridx = 0;
                gbc.gridy = 0;
                this.add(p3,gbc);

                JPanel p2 = new JPanel(new FlowLayout());
                //Class7 c7o = new Class7();
                //p2.add(c7o);
                p2.setPreferredSize(new Dimension(300,40));
                gbc.insets = new Insets(0,0,-20,0);
                gbc.gridx = 0;
                gbc.gridy = 0;
                this.add(p2,gbc);
                break;
            case 5:
                JPanel p4 = new JPanel(new BorderLayout());
                p4.setBounds(0,140,500,55);
                //Class2 c2o = new Class2();
                Dimension d2 = new Dimension(500,55);
                //c2o.setPreferredSize(d2);
                p4.setPreferredSize(d2);
                //p4.add(c2o);
                gbc.insets = new Insets(0,0,-130,0);
                gbc.gridx = 0;
                gbc.gridy = 0;  
                this.add(p4,gbc);

                //this.invalidate();
                this.validate();
                this.repaint();

                JOptionPane.showMessageDialog(null,this.isVisible());
                break;
            default:
                break;
        }
    }

    public void paintComponent(){

    }


}

public class Class4 {

    static int event;
    static JFrame frame;
    static Canv canvas;

    public static void main(String[] args)
    {
        frame = new JFrame("Love Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500,200);     
        frame.setLayout(new GridBagLayout());
        //event = Integer.parseInt(args[0]);
        event = 5;
        canvas = new Canv(event);
        frame.add(canvas);
        frame.setVisible(true);
    }
}

运行上述代码将在对话框中返回 true。为了在事件之间进行更改,只需在 Class4 中添加另一个方法来重置 Canv,方法是使用不同的事件编号调用其构造函数。

于 2013-06-05T12:26:24.543 回答