简体   繁体   English

JFrame和JDialog链接

[英]JFrame & JDialog linking

I'm using visual swing plugin for eclipse - this is the problem. 我正在使用Visual swing插件进行蚀-这就是问题所在。 In my when i click on button it should show dialog (or other jForm, but i've read that only one jfrom should be used by application) and hide first one which is not problem. 在我单击按钮时,它应该显示对话框(或其他jForm,但我已阅读到应用程序只应使用一个jfrom)并隐藏第一个没有问题的。 But on second form i need same thing to happend, when button is clicked, i need first form to be shown back and second to be disposed. 但是在第二种形式上,我需要做同样的事情,单击按钮时,我需要将第一种形式显示回来,然后将第二种形式放置。 Here's code (it's probably not greatly sorted, because i can't find my way around this visual swing) 这是代码(可能排序不大,因为在这种视觉摆动中我找不到自己的出路)

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);


    }
}

here objekat o is class which either extends JFrame or JDialog 这里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);
    }

}

main 主要

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();

} }

I looked through your code and see where you've created JButtons and all, but I don't see anywhere where ActionListeners have been added to anything . 我通过您的代码看起来,看看你已经创建Jbutton将和所有的,但我没有看到任何地方的ActionListeners已被添加到任何东西 Without AcionListeners, JButtons won't invoke a behavior on button press. 没有AcionListeners,JButton不会在按钮按下时调用行为。 I think you need to fix this first, and also go through the basic Swing tutorials found on the Oracle Java tutorial site. 我认为您首先需要解决此问题,还需要阅读在Oracle Java教程站点上找到的基本Swing教程。

Inside your Objekat Class make an instance variable for JFrame , such as private JFrame frame; Objekat类中,为JFrame创建一个实例变量,例如private JFrame frame; , now inside your constructor, where you getting an Object of the JFrame from the calling Class, get this reference, like this.frame = frame; ,现在在构造函数中,您可以从调用类中获取JFrame的Object,获得此引用,例如this.frame = frame; , now modify your below given method to this form : ,现在将您的以下给定方法修改为此表单:

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

and modify your below method as described below : 并如下所述修改您的以下方法:

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

Seems to me that's all you need to make this work. 在我看来,这就是完成此工作所需的全部。 Since in your example, you not passing anything while creating an object of Objekat Class, hence I think no parent is assigned to it, hence your actionPerformed(...) method inside this Class, is not working as expected, try to make these changes, hopefully it will work. 由于在您的示例中,您在创建Objekat类的对象时未传递任何内容,因此我认为未为其分配父对象,因此ObjekatactionPerformed(...)方法未按预期工作,请尝试使它们变化,希望它会起作用。

Excellent tutorial for swing Here 优秀的教程摆在这里

for opening any frame or dialog box when u click a button you have to create an object of that class in the action performed method and set its visiblity to true; 若要在单击按钮时打开任何框架或对话框,则必须在执行动作的方法中创建该类的对象,并将其可见性设置为true; in swing by default the visiblity is false for the frame which we've created.... 默认情况下,在摆动中,对于我们创建的框架,可见性为false。

instead of writing this 而不是写这个

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

code it'd be better to write actionlinstener like this 代码最好像这样编写actionlinstener

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

the above code is just the same as your code but it uses the default event listener rather than creating a new one..... 上面的代码与您的代码相同,但是它使用默认的事件侦听器,而不是创建一个新的事件侦听器。

this is because if there is a available actionlistener available in the standard library it won't be praiseworthy to create custom handlers for a simple button click event... if i am wrong please let me know... 这是因为如果标准库中有可用的动作侦听器,那么为简单的按钮单击事件创建自定义处理程序将不值得称赞...如果我错了,请告诉我...

if you want to dispose the second frame and show again the first frame then you can create reference of both frames in your class and assign the object to it while constructing the frame of the class that would five a dteady reference for both frames if you wanted to do any operations on any of them.... 如果要放置第二个框架并再次显示第一个框架,则可以在类中创建两个框架的引用并为其分配对象,同时构造该类的框架,如果需要的话,可以为两个框架提供五个确定的引用对它们中的任何一个进行任何操作。

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

the constructor will be like this 构造函数将像这样

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 will be like this 动作听者会像这样

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

当您要打开新框架或对话框时,必须将JButton的ActionListeners事件用于JButton ActionListeners事件,请参见ActionListener上的示例

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

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