gpt4 book ai didi

java - 在不同类的 JPanel 之间切换

转载 作者:行者123 更新时间:2023-12-01 14:27:16 28 4
gpt4 key购买 nike

我偶然发现了这种复杂情况,并花了 4 个多小时进行调试和谷歌搜索,但没有结果。

基本上我这里有 1 个 JFrame,2 个 JPanel。我将 JFrame setContentPane 设置为 JPanel 1,当我运行应用程序时,JFrame 将与 JPanel 一起出现。

现在这个 JPanel 里面有 1 个 JButton,当我单击它时我希望它切换到另一个 JPanel。从代码中可以看到,当我单击 JButton(添加产品)时,我希望 OnlineShopAdPane 切换到 AddProduct。我尝试使用 CardLayout 但它只有 NSEW 格式。

package OnlineShop.ui;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.CardLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class OnlineShopMainFrame extends JFrame {

/**
* Launch the application.
*/
AddProduct Add;

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
OnlineShopMainFrame MainFrame = new OnlineShopMainFrame();
MainFrame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public OnlineShopMainFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);

OnlineShopAdPane AdPanel = new OnlineShopAdPane();
setContentPane(AdPanel);

}


}


package OnlineShop.ui;

import javax.swing.JPanel;
import java.awt.CardLayout;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.BorderLayout;

public class OnlineShopAdPane extends JPanel {

/**
* Create the panel.
*/

public OnlineShopAdPane() {

JLabel lblWhatDoYou = new JLabel("What do you want to do?");
lblWhatDoYou.setBounds(28, 26, 160, 26);
add(lblWhatDoYou);

JButton btnAddProduct = new JButton("Add Product");
btnAddProduct.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
OnlineShopMainFrame MainFrame = new OnlineShopMainFrame();
MainFrame.removeAll();
MainFrame.add(new AddProduct());
MainFrame.revalidate();
MainFrame.repaint();
}
});

btnAddProduct.setBounds(46, 75, 115, 23);
add(btnAddProduct);

}

}


package OnlineShop.ui;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class AddProduct extends JPanel {
private JTextField textField;

/**
* Create the panel.
*/
public AddProduct() {

JLabel lblProductName = new JLabel("Product Name:");
lblProductName.setBounds(35, 26, 77, 24);
add(lblProductName);

JLabel lblProductDescription = new JLabel("Product Description:");
lblProductDescription.setBounds(10, 50, 106, 24);
add(lblProductDescription);

textField = new JTextField();
textField.setBounds(116, 28, 141, 20);
add(textField);
textField.setColumns(10);

JTextArea textArea = new JTextArea();
textArea.setBounds(116, 66, 141, 112);
add(textArea);

JButton btnClose = new JButton("Close");
btnClose.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

}
});
btnClose.setBounds(223, 244, 89, 23);
add(btnClose);

}

}

最佳答案

I tried using CardLayout but it only has NSEW formatting.

这是什么意思? CardLayout 仅包含两个或多个面板。一次只能看到一个面板。每个面板都可以使用它想要的任何布局来布局面板上的组件。

when I click it I want it to switch to another JPanel.

这正是 CardLayout 的作用。请参阅 How to Use Card Layout 上的 Swing 教程获取工作示例和解释。

每当我看到像删除/添加/重新验证/重绘这样的代码时,它几乎总是应该被替换为 CardLayout

关于java - 在不同类的 JPanel 之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17109490/

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