JFrame& JDialog链接

时间:2012-05-22 03:46:09

标签: java swing

我正在使用视觉摇摆插件进行日食 - 这就是问题所在。在我点击按钮时,它应该显示对话框(或其他jForm,但我读过应用程序应该只使用一个jfrom)并隐藏第一个没有问题。但是在第二种形式上我需要发生同样的事情,当点击按钮时,我需要先显示背面和第二种形式。 这是代码(它可能没有很大的排序,因为我找不到这种视觉摆动的方式)

import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.WindowConstants;

import org.dyno.visual.swing.layouts.Bilateral;
import org.dyno.visual.swing.layouts.Constraints;
import org.dyno.visual.swing.layouts.GroupLayout;
import org.dyno.visual.swing.layouts.Leading;
import org.dyno.visual.swing.layouts.Trailing;


//VS4E -- DO NOT REMOVE THIS LINE!
public class Intervencije extends JFrame {

    private static final long serialVersionUID = 1L;
    private JPanel jPanel1;
    private JButton btnObjekat;
    private JButton btnEkipa;
    private JButton btnIzlaz;
    private JLabel jLabel0;
    private JPanel jPanel0;
    private JTextField txtBrojIntervencije;
    private JButton btnUcitaj;
    private JLabel jLabel3;
    private JPanel jPanel2;
    private JLabel jLabel1;
    private JLabel jLabel2;
    private JTextField txtDatum;
    private JTextField txtBroj;
    private JTextArea txtOpisi;
    private JScrollPane jScrollPane0;
    private static final String PREFERRED_LOOK_AND_FEEL = "javax.swing.plaf.metal.MetalLookAndFeel";
    public Intervencije() {


        this.pack();
        this.setDefaultCloseOperation(Intervencije.EXIT_ON_CLOSE);
        this.setVisible(true);
        //this.setLocationRelativeTo(null);
        initComponents();
    }

    private void initComponents() {
        setTitle("Intervencije");
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        setLayout(new GroupLayout());
        add(getJPanel2(), new Constraints(new Leading(17, 422, 10, 10), new Leading(226, 71, 10, 10)));
        add(getJPanel0(), new Constraints(new Leading(12, 189, 12, 12), new Leading(18, 100, 10, 10)));
        add(getJPanel1(), new Constraints(new Leading(205, 227, 12, 12), new Leading(15, 202, 10, 10)));
        setSize(454, 334);
    }

    private JTextArea getTxtOpisi() {
        if (txtOpisi == null) {
            txtOpisi = new JTextArea();
            txtOpisi.setEditable(false);
            txtOpisi.setText("txtOpisi");
            txtOpisi.setMinimumSize(new Dimension(80, 16));
            txtOpisi.setPreferredSize(new Dimension(80, 16));
        }
        return txtOpisi;
    }

    private JScrollPane getJScrollPane0() {
        if (jScrollPane0 == null) {
            jScrollPane0 = new JScrollPane();
            jScrollPane0.setViewportView(getJTextArea0());
        }
        return jScrollPane0;
    }

    private JTextArea getJTextArea0() {
        if (txtOpisi == null) {
            txtOpisi = new JTextArea();
        }
        return txtOpisi;
    }

    private JTextField getJTextField2() {
        if (txtBroj == null) {
            txtBroj = new JTextField();
        }
        return txtBroj;
    }

    private JTextField getJTextField1() {
        if (txtDatum == null) {
            txtDatum = new JTextField();
        }
        return txtDatum;
    }




    private JLabel getJLabel2() {
        if (jLabel2 == null) {
            jLabel2 = new JLabel();
            jLabel2.setText("Opis");
        }
        return jLabel2;
    }

    private JLabel getJLabel1() {
        if (jLabel1 == null) {
            jLabel1 = new JLabel();
            jLabel1.setText("Datum i vreme");
        }
        return jLabel1;
    }

    private JPanel getJPanel2() {
        if (jPanel2 == null) {
            jPanel2 = new JPanel();
            jPanel2.setLayout(new GroupLayout());
            jPanel2.add(getJButton0(), new Constraints(new Leading(278, 10, 10), new Leading(35, 10, 10)));
            jPanel2.add(getJButton1(), new Constraints(new Leading(151, 10, 10), new Leading(35, 12, 12)));
            jPanel2.add(getJButton2(), new Constraints(new Leading(23, 10, 10), new Leading(35, 12, 12)));
            jPanel2.add(getJLabel3(), new Constraints(new Leading(184, 173, 10, 10), new Leading(9, 10, 10)));
        }
        return jPanel2;
    }

    private JLabel getJLabel3() {
        if (jLabel3 == null) {
            jLabel3 = new JLabel();
            jLabel3.setText("Vise informacija o");
        }
        return jLabel3;
    }

    private JButton getJButton3() {
        if (btnUcitaj == null) {
            btnUcitaj = new JButton();
            btnUcitaj.setText("Ucitaj");
        }
        return btnUcitaj;
    }

    private JPanel getJPanel0() {
        if (jPanel0 == null) {
            jPanel0 = new JPanel();
            jPanel0.setLayout(new GroupLayout());
            jPanel0.add(getJLabel0(), new Constraints(new Trailing(12, 12, 12), new Leading(3, 19, 10, 10)));
            jPanel0.add(getJButton3(), new Constraints(new Leading(21, 10, 10), new Leading(66, 12, 12)));
            jPanel0.add(getJTextField2(), new Constraints(new Leading(15, 72, 12, 12), new Leading(28, 12, 12)));
        }
        return jPanel0;
    }

    private JLabel getJLabel0() {
        if (jLabel0 == null) {
            jLabel0 = new JLabel();
            jLabel0.setText("Unesite redni broj intervencije");
        }
        return jLabel0;
    }

    private JButton getJButton2() {
        if (btnIzlaz == null) {
            btnIzlaz = new JButton();
            btnIzlaz.setText("Izlaz");
            btnIzlaz.addMouseListener(new MouseAdapter() {

                public void mouseClicked(MouseEvent event) {
                    btnIzlazMouseMouseClicked(event);
                }
            });
        }
        return btnIzlaz;
    }

    private JButton getJButton1() {
        if (btnEkipa == null) {
            btnEkipa = new JButton();
            btnEkipa.setText("Ekipi");
            btnEkipa.addMouseListener(new MouseAdapter() {

                public void mouseClicked(MouseEvent event) {
                    btnEkipaMouseMouseClicked(event);
                }
            });
        }
        return btnEkipa;
    }

    private JButton getJButton0() {
        if (btnObjekat == null) {
            btnObjekat = new JButton();
            btnObjekat.setText("Objektu");
        }
        return btnObjekat;
    }

    private JPanel getJPanel1() {
        if (jPanel1 == null) {
            jPanel1 = new JPanel();
            jPanel1.setLayout(new GroupLayout());
            jPanel1.add(getJLabel1(), new Constraints(new Leading(4, 135, 10, 10), new Leading(2, 29, 10, 10)));
            jPanel1.add(getJLabel2(), new Constraints(new Leading(13, 12, 12), new Leading(69, 57, 115)));
            jPanel1.add(getJTextField1(), new Constraints(new Leading(8, 105, 12, 12), new Leading(31, 12, 12)));
            jPanel1.add(getJScrollPane0(), new Constraints(new Bilateral(12, 12, 22), new Bilateral(99, 12, 22)));
        }
        return jPanel1;
    }

    public static void installLnF() {
        try {
            String lnfClassname = PREFERRED_LOOK_AND_FEEL;
            if (lnfClassname == null)
                lnfClassname = UIManager.getCrossPlatformLookAndFeelClassName();
            UIManager.setLookAndFeel(lnfClassname);
        } catch (Exception e) {
            System.err.println("Cannot install " + PREFERRED_LOOK_AND_FEEL
                    + " on this platform:" + e.getMessage());
        }
    }

    /**
     * Main entry of the class.
     * Note: This class is only created so that you can easily preview the result at runtime.
     * It is not expected to be managed by the designer.
     * You can modify it as you like.
     */

    private void btnIzlazMouseMouseClicked(MouseEvent event) {
        this.dispose();
    }

    public void btnEkipaMouseMouseClicked(MouseEvent event) {
    this.setVisible(false);
    Objekat o = new Objekat();
    o.setVisible(true);


    }
}

这里objekat o是扩展JFrame或JDialog

的类
import java.awt.Color;
import java.awt.Dialog;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GraphicsConfiguration;
import java.awt.Window;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import org.dyno.visual.swing.layouts.Constraints;
import org.dyno.visual.swing.layouts.GroupLayout;
import org.dyno.visual.swing.layouts.Leading;


//VS4E -- DO NOT REMOVE THIS LINE!
public class Objekat extends JDialog {

    private static final long serialVersionUID = 1L;
    private JButton jButton0;
    private static final String PREFERRED_LOOK_AND_FEEL = "javax.swing.plaf.metal.MetalLookAndFeel";
    public Objekat(JFrame parent) {
        super(parent);
        initComponents();
    }

    public Objekat(Dialog parent, String title, boolean modal,
            GraphicsConfiguration arg) {
        super(parent, title, modal, arg);
        initComponents();
    }

    public Objekat(Dialog parent, String title, boolean modal) {
        super(parent, title, modal);
        initComponents();
    }

    public Objekat(Dialog parent, String title) {
        super(parent, title);
        initComponents();
    }

    public Objekat(Window parent, String title, ModalityType modalityType,
            GraphicsConfiguration arg) {
        super(parent, title, modalityType, arg);
        initComponents();
    }

    public Objekat(Window parent, String title, ModalityType modalityType) {
        super(parent, title, modalityType);
        initComponents();
    }

    public Objekat(Window parent, String title) {
        super(parent, title);
        initComponents();
    }

    public Objekat(Window parent, ModalityType modalityType) {
        super(parent, modalityType);
        initComponents();
    }

    public Objekat(Frame parent, String title) {
        super(parent, title);
        initComponents();
    }

    public Objekat(Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    public Objekat(Frame parent) {
        super(parent);
        initComponents();
    }

    public Objekat() {
        initComponents();
    }

    public Objekat(Dialog parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    public Objekat(Dialog parent) {
        super(parent);
        initComponents();
    }

    public Objekat(Frame parent, String title, boolean modal,
            GraphicsConfiguration arg) {
        super(parent, title, modal, arg);
        initComponents();
    }

    public Objekat(Frame parent, String title, boolean modal) {
        super(parent, title, modal);
        initComponents();
    }

    private void initComponents() {
        setFont(new Font("Dialog", Font.PLAIN, 12));
        setBackground(Color.white);
        setForeground(Color.black);
        setLayout(new GroupLayout());
        add(getJButton0(), new Constraints(new Leading(237, 10, 10), new Leading(81, 10, 10)));
        addMouseListener(new MouseAdapter() {

            public void mouseClicked(MouseEvent event) {
                mouseMouseClicked(event);
            }
        });
        setSize(428, 240);
    }

    private JButton getJButton0() {
        if (jButton0 == null) {
            jButton0 = new JButton();
            jButton0.setText("jButton0");
        }
        return jButton0;
    }

    private static void installLnF() {
        try {
            String lnfClassname = PREFERRED_LOOK_AND_FEEL;
            if (lnfClassname == null)
                lnfClassname = UIManager.getCrossPlatformLookAndFeelClassName();
            UIManager.setLookAndFeel(lnfClassname);
        } catch (Exception e) {
            System.err.println("Cannot install " + PREFERRED_LOOK_AND_FEEL
                    + " on this platform:" + e.getMessage());
        }
    }

    /**
     * Main entry of the class.
     * Note: This class is only created so that you can easily preview the result at runtime.
     * It is not expected to be managed by the designer.
     * You can modify it as you like.
     */
//  public static void main(String[] args) {
//      installLnF();
//      SwingUtilities.invokeLater(new Runnable() {
//          public void run() {
//              Objekat dialog = new Objekat();
//              dialog.setDefaultCloseOperation(Objekat.DISPOSE_ON_CLOSE);
//              dialog.setTitle("Objekat");
//              dialog.setLocationRelativeTo(null);
//              dialog.getContentPane().setPreferredSize(dialog.getSize());
//              dialog.pack();
//              dialog.setVisible(true);
//          }
//      });


    private void mouseMouseClicked(MouseEvent event) {
        //this.getParent().setVisible(true);
        this.setVisible(false);
    }

}

public class main {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    Intervencije d = new Intervencije();
    d.setLocationRelativeTo(null);
    //d.setVisible(true);
    //d.installLnF();

}

4 个答案:

答案 0 :(得分:2)

我查看了你的代码,看看你在哪里创建了JButton,但是我没有看到ActionListeners被添加到任何东西的任何地方。如果没有AcionListeners,JButtons将不会在按下按钮时调用行为。我认为你需要首先解决这个问题,并且还要阅读Oracle Java教程网站上的基本Swing教程。

答案 1 :(得分:1)

Objekat类中为JFrame创建一个实例变量,例如private JFrame frame;,现在在构造函数中,从调用中获取JFrame的对象类,获取此引用,如this.frame = frame;,现在将以下给定的方法修改为此形式:

private void mouseMouseClicked(MouseEvent event) 
{
    frame.setVisible(true);
    this.dispose();
}

并修改以下方法,如下所述:

public void btnEkipaMouseMouseClicked(MouseEvent event) 
{
    this.setVisible(false);
    Objekat o = new Objekat(this);
    o.setVisible(true);
}

在我看来,这就是你完成这项工作所需要的一切。因为在您的示例中,您在创建Objekat类的对象时未传递任何内容,因此我认为没有为其分配父级,因此此类中的actionPerformed(...)方法未按预期工作,请尝试做出这些改变,希望它会起作用。

答案 2 :(得分:1)

优秀的摆设Here

教程

当你单击一个按钮时打开任何框架或对话框,你必须在动作执行方法中创建该类的对象并将其visiblity设置为true;在默认情况下,对于我们创建的框架,可见性是错误的。

而不是写这个

  public void btnEkipaMouseMouseClicked(MouseEvent event) {
    this.setVisible(false);
    Objekat o = new Objekat();
    o.setVisible(true);

代码最好像这样编写actionlinstener

btn.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){ Objekat o = new Objekat(this);}
});

上面的代码与您的代码完全相同,但它使用的是默认的事件监听器,而不是创建一个新的......

这是因为如果标准库中有可用的actionlistener,那么为简单的按钮点击事件创建自定义处理程序将不值得称赞... 如果我错了请告诉我......

如果你想设置第二帧,并显示再次第一帧,那么你可以创建你的类两个框架的引用和对象分配给它,而构建类的框架是将五个两种帧dteady参考如果你想对任何一个做任何操作......

public class Intervencije extends JFrame {
Objekat prev, next;
}

构造函数将是这样的

public Objekat (Objekat prev) {
   //the next frame is obviously the onn in which u r working
next = this;
prev.setVisible(true);
next.setVisible(false);//you can do whatever you want with prev and next
}

actionlistener将是这样的

Objekat prev = this;
public void actionPerformed(Actionevent e){
    Objekat newFrame = new Objekat(prev)
}

答案 3 :(得分:1)

当你想打开一个新的框架或对话框时,你必须使用JButton ActionListeners事件的ActionListeners事件参见ActionListener上的例子