gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:56:24 26 4
gpt4 key购买 nike

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

最佳答案

这是一个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?

关于java GUI 窗体打开其他窗体 onclick 按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54349854/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com