我正在尝试为具有自动和手动模式的假想车辆创建一个用户界面。当用户将车辆设置为其中一种模式时,它应该只显示与该模式相关的控件,我已经使用 CardLayout
完成了这一点。
但是,我还希望能够手动指定每张卡布局的各个元素的位置 - 对于静态布局,我会按照 mainPanel.setLayout(null )
,但是当在 CardLayout 上使用时,这只会给出一个空白窗口(因此下面的代码中出现了两行注释掉的行)。
我如何实现这两件事?我当前的代码如下:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class UI extends JFrame implements ActionListener{
public UI() {
initUI();
}
private JPanel cardPanel;
private CardLayout cardLayout = new CardLayout();
public final void initUI() {
cardPanel = new JPanel();
cardPanel.setLayout(cardLayout);
JPanel manualPanel = new JPanel();
getContentPane().add(manualPanel);
//manualPanel.setLayout(null);
cardPanel.add(manualPanel, "manual");
JPanel autoPanel = new JPanel();
//autoPanel.setLayout(null);
cardPanel.add(autoPanel, "auto");
JButton startButton = new JButton("START/STOP");
startButton.setBounds(50, 150, 200, 50);
startButton.addActionListener(new startListener());
manualPanel.add(startButton);
autoPanel.add(startButton);
JButton autoButton = new JButton("SWITCH TO AUTO");
autoButton.setBounds(50, 250, 200, 50);
autoButton.addActionListener(new autoListener());
manualPanel.add(autoButton);
JButton upButton = new JButton("^");
upButton.setBounds(125, 320, 50, 50);
upButton.addActionListener(new returnListener());
manualPanel.add(upButton);
JButton downButton = new JButton("\\/");
downButton.setBounds(125, 380, 50, 50);
downButton.addActionListener(new returnListener());
manualPanel.add(downButton);
JButton ccwButton = new JButton("<-");
ccwButton.setBounds(55, 350, 50, 50);
ccwButton.addActionListener(new returnListener());
manualPanel.add(ccwButton);
JButton cwButton = new JButton("->");
cwButton.setBounds(195, 350, 50, 50);
cwButton.addActionListener(new returnListener());
manualPanel.add(cwButton);
JButton ngzButton = new JButton("SOMETHING ELSE");
ngzButton.setBounds(50, 450, 200, 50);
ngzButton.addActionListener(new returnListener());
manualPanel.add(ngzButton);
JButton manualButton = new JButton("SWITCH TO MANUAL");
manualButton.setBounds(50, 250, 200, 50);
manualButton.addActionListener(new manualListener());
autoPanel.add(manualButton);
JButton returnButton = new JButton("SOMETHING ELSE");
returnButton.setBounds(50, 350, 200, 50);
returnButton.addActionListener(new returnListener());
autoPanel.add(returnButton);
setTitle("UI");
setSize(800, 600);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
add(cardPanel, BorderLayout.NORTH);
}
public static void main(String[] args) {
UI ui = new UI();
ui.setVisible(true);
}
public void actionPerformed(ActionEvent e){
}
private class returnListener implements ActionListener {
public void actionPerformed (ActionEvent event) {
}
}
private class autoListener implements ActionListener {
public void actionPerformed (ActionEvent event) {
cardLayout.show(cardPanel, "auto");
}
}
private class startListener implements ActionListener {
public void actionPerformed (ActionEvent event) {
}
}
private class manualListener implements ActionListener {
public void actionPerformed (ActionEvent event) {
cardLayout.show(cardPanel, "manual");
}
}
}
最佳答案
在您的示例中,您创建了 startButton
,但您随后尝试将同一实例添加到两个不同的面板。由于一个组件只能占用一个容器,因此您需要创建两个按钮,每个面板一个。
顺便说一句,不要使用 null
布局,给每个面板 BorderLayout
,将按钮添加到 JPanel
默认为 FlowLayout
,并将按钮面板添加到 SOUTH
。然后,您可以将插图嵌套在 CENTER
中使用任何合适的布局。
附录:如@Frakcool comments ,使用布局将改善按钮的跨平台外观。调用pack()
在封闭的窗口上,并覆盖 getPreferredSize()
在嵌套插图面板上为其提供所需的尺寸。在此相关example ,CENTER
面板仅用于绘图;如果没有组件,它的布局就变得无关紧要。
关于java - 在 CardLayout 中设置面板布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32146783/