Java GUI窗体打开其他窗体onclick按钮

时间:2019-01-24 15:16:20

标签: java forms swing user-interface intellij-idea

当我从form1单击按钮打开form2时,我正在尝试做。听起来很简单,但是我无法找到任何方法来执行此操作。我正在使用java intellij。 当我使用netbeans并摆动时,我正在使用以下命令: “ Form2 form2 = new Form2();

form2.setVisible(true);

dispose(); “

Form1(主):

public class Main {
    private JButton b_show;
    private JButton b_Add;
    private JPanel jp_main;


    public Main() {

        b_show.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {

            }
        });
    }


    public static void main(String[]args){
        JFrame frame=new JFrame();
        frame.setContentPane(new Main().jp_main);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300,300);
        frame.setVisible(true);


    }

}

form2(显示):

public class Show {
    private JButton b_back;
    public JPanel jpanelmain;


    public Show() {
        Show show=new Show();
        geriButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {

            }
        });
    }

    public static void main(String[]args){
        JFrame frame=new JFrame();
        frame.setContentPane(new Show().jpanelmain);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300,300);
        frame.setVisible(true);

    }

}

有谁能帮助我吗?

当单击b_show打开form2(显示)时。

2 个答案:

答案 0 :(得分:1)

最好的方法是使用JDialogs。在“ Form1”上的Foreach ($Item in Get-ChildItem *.JPG) {Rename-Item -newname {$_.LastWriteTime.toString("dd-MM-yyyy HH.mm") + ".JPG"}}被调用时,您将实例化一个新的JDialog并将其设置为可见。这是一个示例:

actionPerformed()

最后,当您想关闭对话框时,在其中实现一个public class Show extends JDialog { private JButton b_back; public JPanel jpanelmain; public Show(Frame owner, boolean modal) { super(owner, modal); } //method that creates the GUI } b_show.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent actionEvent) { Show show = new Show(JOptionPane.getFrameForComponent(this), true); show.setVisible(true); } }); ,然后调用actionPerformed()方法

答案 1 :(得分:1)

这是一个演示它的mcve

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main {

    private final JButton b_show;
    private final JPanel jp_main;

    public Main() {
        jp_main = new JPanel();
        b_show = new JButton("Show");
        b_show.addActionListener(actionEvent -> {
            new Show();
        });
        jp_main.add(b_show);
    }
    public static void main(String[]args){
        JFrame frame=new JFrame();
        frame.setContentPane(new Main().jp_main);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300,300);
        frame.setVisible(true);
    }
}

class Show {
    private JButton b_back;
    public JPanel jpanelmain;


    public Show() {
        createAndShowGui();
    }

    void createAndShowGui(){

        JFrame frame=new JFrame();
        frame.setLocationRelativeTo(null);
        jpanelmain = new JPanel();
        b_back = new JButton("Back");
        jpanelmain.add(b_back);
        frame.setContentPane(jpanelmain);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300,300);
        frame.setVisible(true);
    }
} 

但是,请阅读The Use of Multiple JFrames: Good or Bad Practice?