亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

java GUI Form open other form onclick button

java GUI Form open other form onclick button

元芳怎么了 2022-05-21 16:38:47
當從form1打開form2單擊按鈕時,我正在嘗試做。它聽起來很簡單,但我找不到任何方法來做到這一點。我正在使用 java intellij。當我使用netbeans和swing時,我正在這樣做:“Form2 form2=new Form2();form2.setVisible(true);處置();"表格1(主要):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);    }}表格2(顯示):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 回答

?
HUWWW

TA貢獻1874條經驗 獲得超12個贊

這是一個演示它的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);

    }


查看完整回答
反對 回復 2022-05-21
?
MMMHUHU

TA貢獻1834條經驗 獲得超8個贊

最好的方法是使用 JDialogs。在actionPerformed()調用“Form1”時,您將實例化一個新的 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()方法


查看完整回答
反對 回復 2022-05-21
  • 2 回答
  • 0 關注
  • 146 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號