gpt4 book ai didi

java - 如何在JFrame中切换场景

转载 作者:太空宇宙 更新时间:2023-11-04 13:37:32 25 4
gpt4 key购买 nike

嘿伙计们,我想知道。例如,我怎样才能制作我的主窗口,当用户输入某些内容或单击某些内容时,场景会发生变化,我的意思是一切都有新图像新东西,但无需更改窗口或至少不必弹出一个新窗口而必须关闭另一个窗口。然后,例如,如果他单击新窗口上的某些内容,他将返回到主窗口。我是 JFrame 的新人。谢谢你的时间。编辑:这是我的代码(测试 JFrame 代码)

public class ActionListenerTest extends JFrame {

private static final long serialVersionUID = 1L;

public ActionListenerTest(){
super("Yeah");
setLayout(new FlowLayout());
setSize(800,800);
setVisible(true);
setResizable(false);
setLocationRelativeTo(null);


ImageIcon image=new ImageIcon (getClass().getResource("FileName2.png"));
JPanel p =new JPanel(new GridBagLayout());
JPanel p2 =new JPanel(new GridBagLayout());
JLabel lbl= new JLabel(image);
JButton b=new JButton("Button Test");
JButton b2=new JButton("Button Test 2");
lbl.addMouseListener(new MouseListener() {

@Override
public void mouseReleased(MouseEvent e) {
JOptionPane.showMessageDialog(null, "Men... you weak..","WOOOOO",JOptionPane.PLAIN_MESSAGE);
}

@Override
public void mousePressed(MouseEvent e) {
JDialog d=new JDialog();
d.setLocationRelativeTo(null);
d.setSize(300,300);
d.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
d.setTitle("Dialog TEST");
d.setVisible(true);
JButton test=new JButton("TESTING");
JPanel testP= new JPanel();
testP.add(test);
d.add(testP, BorderLayout.WEST);
}

@Override
public void mouseExited(MouseEvent e) {

}

@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mouseClicked(MouseEvent e) {

}
});
b.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Hey wanna hang out sometime?","WOOOOO",JOptionPane.PLAIN_MESSAGE);
}
});


GridBagConstraints gbc = new GridBagConstraints();
gbc.insets=new Insets(150,20,30,20);
gbc.gridx=3;
gbc.gridy=2;
p.add(b, gbc);
gbc.gridx=3;
gbc.gridy=3;
p.add(b2, gbc);
gbc.gridx=15;
gbc.gridy=15;
p2.add(lbl, gbc);
add(p2, BorderLayout.SOUTH);
add(p, BorderLayout.NORTH);
}
public static void main(String[] args){
ActionListenerTest a = new ActionListenerTest();
a.setVisible(true);
}
}

最佳答案

您需要的是 SWING - CardLayout , Example 。欢迎您!

如果您必须经常更换面板或想自己动手。我建议您在 JFrame (默认情况下)上创建一组设置 BorderLayout 的面板,然后在按钮监听器中相应地交换面板。为了获得平滑的外观,所有面板边界应该相同。

关于java - 如何在JFrame中切换场景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31560870/

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