java action 打开新页面,单击一个按钮以打开新的JFrame/GUI,

I seem to not be able to find a way to get my code to work.

I am making a program and until now everything was working, i have some buttons and they do what they should.

But now i added a button that when a user click it, it should close the current GUI and open a new one.

I also want to point out that i created a new class for this new GUI.

The other GUI class that i want to call is the GuiCrafting, in that class the GUI is also all coded, and works if i call it on the Main.

My question is what do i type here (I tried a lot of things like dispose() etc but i just get error messages) :

public void actionPerformed(ActionEvent event) {

if( str.equals("Crafting")){

//insert code to call the GuiCrafting class and open his GUI

}

Thanks in advance and if you need something more please let me know.

解决方案

Multiple JFrames are frowned upon as you can read about here and here

Perhaps what you want to use is a CardLayout which manages two or more components (usually JPanel instances) that share the same display space.

QTWb5.png

After clicking the button "Goto Card 2"

apd1j.png

TestApp.java:

import java.awt.BorderLayout;

import java.awt.CardLayout;

import java.awt.event.ActionEvent;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.SwingUtilities;

public class TestApp {

final static String CARD1 = "Card1";

final static String CARD2 = "Card2";

public TestApp() {

initComponents();

}

public static void main(String[] args) {

SwingUtilities.invokeLater(TestApp::new);

}

private void initComponents() {

JFrame frame = new JFrame("TestApp");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// create the panel that contains the "cards".

JPanel cards = new JPanel(new CardLayout());

// card 1 components

JButton buttonGotoCard2 = new JButton("Goto Card 2");

buttonGotoCard2.addActionListener((ActionEvent e) -> {

CardLayout cl = (CardLayout) (cards.getLayout());

cl.show(cards, CARD2);

});

// create card 1

JPanel card1 = new JPanel();

card1.add(new JLabel("Card 1"));

card1.add(buttonGotoCard2);

// card 2 components

JButton buttonGotoCard1 = new JButton("Goto Card 1");

buttonGotoCard1.addActionListener((ActionEvent e) -> {

CardLayout cl = (CardLayout) (cards.getLayout());

cl.show(cards, CARD1);

});

// create card 2

JPanel card2 = new JPanel();

card2.add(new JLabel("Card 2"));

card2.add(buttonGotoCard1);

// add cards to cards panel

cards.add(card1, CARD1);

cards.add(card2, CARD2);

frame.getContentPane().add(cards, BorderLayout.CENTER);

frame.pack();

frame.setVisible(true);

}

}

There is also a JDialog which could be what you want.

HOWEVER

You can easily do something like that (Open a JFrame from another If you must):

TestApp.java:

import java.awt.event.ActionEvent;

import java.awt.event.WindowAdapter;

import javax.swing.BoxLayout;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.SwingUtilities;

import javax.swing.border.EmptyBorder;

public class TestApp {

public TestApp() {

initComponents();

}

public static void main(String[] args) {

SwingUtilities.invokeLater(TestApp::new);

}

private void initComponents() {

JFrame mainFrame = new JFrame();

mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel panel = new JPanel();

panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

panel.setBorder(new EmptyBorder(10, 10, 10, 10));

JLabel label = new JLabel("JFrame 1");

JButton button = new JButton("Open JFrame 2");

button.addActionListener((ActionEvent e) -> {

this.showNewJFrame(new WindowAdapter() {

@Override

public void windowClosing(java.awt.event.WindowEvent e) {

// here we listen for the second JFrame being closed so we can bring back the main JFrame

mainFrame.setVisible(true);

}

});

// hide the main JFrame

mainFrame.setVisible(false);

});

panel.add(label);

panel.add(button);

mainFrame.add(panel);

mainFrame.pack();

mainFrame.setVisible(true);

}

private void showNewJFrame(WindowAdapter windowAdapter) {

JFrame frame2 = new JFrame();

frame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // we dont wnat to exit when this JFrame is closed

JPanel panel2 = new JPanel();

panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));

panel2.setBorder(new EmptyBorder(10, 10, 10, 10));

JLabel label2 = new JLabel("JFrame 2");

panel2.add(label2);

frame2.add(panel2);

frame2.addWindowListener(windowAdapter);

frame2.pack();

frame2.setVisible(true);

}

}

This produces:

zKs7H.png

and when the "Open JFrame 2" is clicked:

ovZNi.png

and when JFrame 2 is closed it brings back the main JFrame via the WindowAdapter#windowClosing.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值