【发布时间】:2025-12-02 23:10:02
【问题描述】:
我的框架中有 2 个面板,1 个用于按钮(我想使用 radioButton,但现在使用按钮更容易),另一个用于卡片布局面板。我的计划是在我按下特定按钮时随机播放乍得。就像移动按钮会显示移动面板卡一样。移动面板卡有 x0 标签和文本字段,线路面板卡有 x0 和 x1 的标签和文本字段。
有 2 个类,1 个用于按钮面板 = Buttons 另一个用于卡片 = PanelMiddle 这是我的代码:
public class PanelMiddle{
JPanel controlPanel = new JPanel();
CardLayout cl = new CardLayout();
JPanel movePanel = new JPanel();
JPanel linePanel = new JPanel();
JLabel x0Label = new JLabel("x0");
JTextField x0TextField = new JTextField(3);
JLabel x1Label = new JLabel("x1");
JTextField x1TextField = new JTextField(3);
public PanelMiddle(){
controlPanel.setLayout(cl);
//move panel
movePanel.setLayout(new GridLayout (1,2));
movePanel.add(x0Label);
movePanel.add(x0TextField);
controlPanel.add(movePanel,"Move"); //add the keyword Move to show the move card
//line panel
linePanel.setLayout(new GridLayout (2,2));
//linePanel.add(x0Label);
linePanel.add(x1Label);
//linePanel.add(x0TextField);
linePanel.add(x1TextField);
controlPanel.add(linePanel,"Line"); // add the keyword Line to show the line card
}
}
在我的另一堂课中:
public class Buttons extends PanelMiddle{
JPanel buttonPanel = new JPanel();
JButton moveB = new JButton ("Move");
JButton lineB = new JButton ("Line");
public Buttons(){
buttonPanel.setLayout(new GridLayout (2,1));
buttonPanel.add(moveB);
buttonPanel.add(lineB);
action();
}
public void action(){
moveB.addActionListener((e) -> {
cl.show(controlPanel,"Move");
});
lineB.addActionListener((e) -> { cl.show(controlPanel,"Line");});
}
}
我得到的结果很奇怪。它没有完全显示我的面板。但是当我尝试评论所有线路面板时,它可以工作。有人在这里修复吗?
NB:对不起,我不知道如何编辑这里的文字,所以有点乱。
编辑 1:正如 guleryuz 所说,我从线路面板中注释掉了 x0Label 和 x0TextField
【问题讨论】:
标签: java swing button jbutton cardlayout