gpt4 book ai didi

java - 使用卡片布局,卡片不交换?

转载 作者:行者123 更新时间:2023-12-01 11:46:04 31 4
gpt4 key购买 nike

我想将我的 Controller 面板保持为 JPanel 类型,因为我稍后将合并到选项卡中,我想使用这些特定屏幕上的按钮在 Main 和 NextPage 之间交换,我不想在底部有一致的按钮对于在卡片之间切换的两个屏幕(即我不想在两个屏幕上都出现添加和返回),我试图让 Main 中的添加按钮转到 NextPage,并让 NextPage 中的后退按钮转到 Main 。这是我到目前为止所拥有的:

对于 Controller :

import java.awt.CardLayout;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Controller extends JPanel {

private static Controller instance = new Controller();

JPanel cards;
Main mainPanel;
NextPage nextPage;

public Controller() {
setLayout(new BorderLayout());
setSize(810, 510);
cards = new JPanel(new CardLayout());

mainPanel = new Main();
nextPage = new NextPage();
cards.add(mainPanel, "Main");
cards.add(nextPage, "Next");
add(cards);
setVisible(true);
}

private static void createAndShowGUI() {
JFrame frame = new JFrame("MainPanel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Controller con = new Controller();
frame.getContentPane().add(con);
frame.setSize(800, 600);
frame.setVisible(true);
}

public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}


public void changeCard(String card) {
CardLayout cl = (CardLayout) (cards.getLayout());
cl.show(cards, card);
}

public static Controller getInstance() {
return instance;
}
}

对于主要部分:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;

public class Main extends JPanel implements ActionListener {

private JButton search, add, delete;
private JTextField textField;

public Main() {

search = new JButton("Search");
add = new JButton("Add");
delete = new JButton("Delete");
textField = new JTextField(20);
add.addActionListener(this);
delete.addActionListener(this);
setLayout(new BorderLayout());
JPanel top = new JPanel();
top.add(search);
add(top, BorderLayout.NORTH);
JPanel bottom = new JPanel();
bottom.add(add);
bottom.add(delete);
add(bottom, BorderLayout.SOUTH);
setVisible(true);
setSize(400, 500);
}

public void actionPerformed(ActionEvent e) {
if (e.getSource() == add) {
Controller.getInstance().changeCard("Next");
} else if (e.getSource() == delete) {
System.out.println("do something");
}
}

}

对于下一页:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;

public class NextPage extends JPanel implements ActionListener {

private JButton back;
private JTextField textField;

public NextPage() {
back = new JButton("Back");
textField = new JTextField(20);
back.addActionListener(this);
setLayout(new BorderLayout());
add(back);
setVisible(true);
setSize(400, 500);
}

public void actionPerformed(ActionEvent e) {
if (e.getSource() == back) {
Controller.getInstance().changeCard("Next");
}

}
}

最佳答案

查看Card Layout Actions

它是 CardLayout 的扩展,为您提供上一个/下一个按钮,您可以轻松地将这些按钮添加到与 CardLayout 分开的面板中。

关于java - 使用卡片布局,卡片不交换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29118516/

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