【问题标题】:java GUI Form open other form onclick buttonjava GUI Form open other form onclick button
【发布时间】:2019-01-24 15:16:20
【问题描述】:

我正在尝试从 form1 中单击按钮打开 form2。它听起来很简单,但我找不到任何方法来做到这一点。我正在使用 java intellij。 当我使用netbeans和swing时,我正在这样做: "Form2 form2=new Form2();

form2.setVisible(true);

处置(); "

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(Show)。

【问题讨论】:

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


【解决方案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?

【讨论】:

    【解决方案2】:

    最好的方法是使用 JDialogs。当调用 'Form1' 处的 actionPerformed() 时,您将实例化一个新的 JDialog 并将其设置为可见。这是一个例子:

    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(),并调用dispose()方法

    【讨论】:

    • 不幸的是,这不像我想要的那样工作。这样做时不会打开我设计的框架。
    猜你喜欢
    • 2017-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-14
    • 1970-01-01
    • 2019-02-28
    • 2013-03-05
    • 1970-01-01
    相关资源
    最近更新 更多