1

我正在为 Eclipse 使用视觉摆动插件 - 这就是问题所在。在我单击按钮时,它应该显示对话框(或其他 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

4 回答 4

2

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

于 2012-05-22T03:59:30.053 回答
1

当您想打开一个新的框架或对话框时,您必须使用 JButton 的 ActionListeners 事件来处理 JButton ActionListeners 事件,请参阅 ActionListener 上的示例

于 2012-05-22T05:43:17.623 回答
1

在您的类中为,例如Objekat创建一个实例变量,现在在您的构造函数中,您从调用类中获取一个对象,获取此引用,例如,现在将您的以下给定方法修改为这种形式:JFrameprivate JFrame frame;JFramethis.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);
}

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

于 2012-05-22T04:30:48.190 回答
1

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; in swing by default the visiblity is false for the frame which we've created....

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

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)
}
于 2012-05-22T05:11:24.393 回答