- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有类 'Frame',它扩展了 JFrame
和 separetad JPanels
:MainMenu
和 SinglePanel
我正在使用 CardLayout
,但在使用 buttonSingle
和 powrot
按钮切换回面板时遇到问题。所以我的问题是如何使用这些按钮在卡片之间更改/交换?
我的 Frame
类:
public class Frame extends JFrame{
CardLayout cl = new CardLayout();
final MainMenu menuPanel = new MainMenu();
final SinglePanel singlePanel = new SinglePanel();
public Frame(){
setLayout(cl);
add(menuPanel,"menu");
add(singlePanel,"single");
setSize(200, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setVisible(true);
setEnabled(true);
swapView("menu");
}
public void swapView(String view){
cl.show(getContentPane(),view);
}
}
我的 MainMenu
类:
public class MainMenu extends JPanel{
public MainMenu(){
setLayout(new BoxLayout(this , BoxLayout.Y_AXIS));
add(Box.createVerticalGlue());
JButton buttonSingle = new JButton("Single");
buttonSingle.setAlignmentX(Component.CENTER_ALIGNMENT);
buttonSingle.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
}
});
add(buttonSingle);
add(Box.createVerticalGlue());
JButton buttonMulti = new JButton("Multiplayer");
buttonMulti.setAlignmentX(Component.CENTER_ALIGNMENT);
buttonMulti.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
});
add(buttonMulti);
add(Box.createVerticalGlue());
JButton buttonExit = new JButton("Wyjście");
buttonExit.setAlignmentX(Component.CENTER_ALIGNMENT);
buttonExit.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}});
add(buttonExit);
add(Box.createVerticalGlue());
}
}
我的 SinglePanel
类
public class SinglePanel extends JPanel{
SinglePanel(){
setLayout(new FlowLayout());
JButton powrot = new JButton("Wróć do menu");
powrot.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
}
});
add(powrot);
}
}
主类:
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
/*MainMenu mM = new MainMenu();*/
Frame f = new Frame();
}
}
最佳答案
您需要在面板类中引用 JFrame
的 CardLayout
。你可以做的是将 Frame
传递给 JPanel
类作为引用,然后你可以使用 Frame
的 CardLayout
code> 在这些类中 show
或 next
等。类似
public class MainMenu {
private CardLayout layout;
private Frame frame;
public MainMenu(final Frame frame) {
this.frame = frame;
this.layout = (CardLayout)frame.getLayout();
JButton buttonSingle = new JButton("Single");
buttonSingle.setAlignmentX(Component.CENTER_ALIGNMENT);
buttonSingle.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
layout.show(frame, "single");
}
});
}
}
当您创建MainPanel
时,您需要将Frame.this
传递给它,引用当前的Frame
MainMenu menuPanel = new MainMenu(Frame.this);
编辑
我刚刚注意到您的 swapView
方法。因此,与其在面板类中直接使用 CardLayout
,不如在 actionPerformed
swapView
frame.swapView("single");
或者更好的是,为了不公开 Frame
类,您可以让 Frame
类实现一个 interface
说 SwapInterface
具有您需要重写的方法 swapView
。并将 SwapInterface
传递给面板类。有点像
public interface SwapInterface {
public void swapView(String view);
}
public Frame extends JFrame implements SwapInterface {
MainMenu mainPanel = new MainMenu(Frame.this);
....
@Override
public void swapView(String view) {
cl.show(getContentPane(), view);
}
}
public class MainMenu extends JPanel {
private SwapInterface swap;
public MainMenu(SwapInterface swap) {
this.swap = swap;
...
public void actionPerfomed(ActionEvent e) {
swap.swapView("single");
}
}
}
旁注
正如 HovercraftFullOfEels 在他的评论中指出的那样,您应该为字符串卡值使用字符串常量,这样就不会出现错误。有点像
private static final String SINGLE_CARD = "single";
然后在你使用“single”
的地方,使用SINGLE_CARD
代替
关于Java:CardLayout在卡片之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22593162/
嗨,我正在使用表单做一个小项目。目前我使用了 netbeans,但我的类变得非常复杂,因为所有 jpanel ie 卡都在一个类中,这是一个框架。我被要求简化。 我的问题是如果我把一个 jpanel
是否有一种优雅的方法可以用指向另一个对象的新引用替换添加到 CardLayout 中的对象的引用。我不知道现在我是否想得好,但我只想得到反馈。 最佳答案 而不是 bankAccPanel 直接添加将其
在我的项目中,我在 JFrame 中有一个 JPanel (bottomPanel) 需要交换,因此我在 BottomPanel 上使用 CardLayout。 在我的父 JFrame 中,我基本上在
我想熟悉 CardLayout,所以我正在制作一个模拟游戏菜单。该菜单应该有三个按钮,但布局部分很简单。 所以,我想做的是用带有三个按钮的菜单启动它。单人游戏按钮应该将用户看到的内容更改为单个按钮,这
我是java初学者。在第二个卡片面板中,用户名和密码对齐不正确。有什么办法可以解决吗?我还想知道使用多个框架的缺点是什么。 import java.awt.*; import java
我正在尝试编写一个简短的程序,它有一个带有标题的主页和 4 个按钮,其中 3 个按钮将离开主屏幕并转到一个新页面,您可以在其中相应地输入信息。我开始使用拖放编辑器,但通过论坛发现我应该使用卡片布局,而
这是我的 SSCE(尽管分为三个单独的类(class))。 启动.java public class Startup { public static void main(String args
每当我感觉自己已经学到了很多关于 Java 的知识时,我就会突然遇到一堵砖墙,让我感觉自己像个十足的白痴。今天的大问题是 CardLayout。至少我终于通过在字段列表中实例化 buttonsCard
我正在使用 Eclipse 和 Window Builder。但是我无法在 Window Builder 中使用卡片布局。所以我开始输入自己的代码,现在我卡在显示第一张卡上,该卡显示正确,但在单击 j
有没有办法告诉使用 CardLayout 的 JPanel 在哪里添加组件? 假设我在框架中央有一个这样的面板,并且我想在该面板内显示 3 个组件。这可能吗? 最佳答案 当然,这很容易。只需将 JPa
我在 CardLayout 中放置了一堆面板,其中第 n 面板取决于 (n - 1)th 面板。由于使用 CardLayout,您必须事先初始化并添加所有面板。因此,它使得管理状态变得比必要的更加困难
我制作了一个简单的程序,几乎可以完成所有操作,除了返回到第一个面板的开关之外。因此,如果我单击“Druck”按钮,它将把钱从一个银行帐户转移到另一个银行帐户,然后切换到第二个面板,其中显示两个帐户的余
我正在关注 YouTube 上关于 CardLayout 的教程。我下载了the original code ,效果很好。但对于我试图制作的程序,我需要一个单独的类来运行应用程序(即仅具有 main
我有一个已经运行并可以运行的游戏,我想为其添加一个标题屏幕。我正在尝试添加 CardLayout 以便在游戏和标题屏幕之间轻松切换。我当前的问题是没有显示任何内容。这是一张图片:/image/AooM
我创建了使用多个面板的应用程序,所以我选择了cardLayout。问题是,当执行以下代码时,在 UserInterface 方法 SinglePlayer() 中会发生一些奇怪的事情。我使用命令 fr
我正在为一个相当简单的棋盘游戏制作我的第一个 GUI。除了游戏 View 之外,我还需要主菜单和其他一些 View 。不幸的是,我的 GUI 看起来比早上更难看,因为整个菜单结构都在一个类中。我使用卡
这是我第一次尝试使用 Cardlayout 运行代码。这是 add 方法的异常(空指针)。不过,我也尝试弄清楚如何设计卡片布局,例如卡片并排或一张在下。我更喜欢后者。我已经尝试更改我的代码并阅读有关类
所以我有一个带有 CardLayout 的 JPanel。正如预期的那样,此 CardLayout 管理框架中面板的切换。切换是通过两个按钮完成的:“后退”和“下一步”。 我想知道当它位于最后一张卡上
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
我知道这个问题之前已经被问过,但我似乎无法让答案起作用,也不理解它们。 我想做的是交换按钮单击的面板。这是我的主要功能: public class CreateWindow extends JFram
我是一名优秀的程序员,十分优秀!