[英]Trouble using Cardlayout in java
麻烦在cardlayout的面板上添加组件时,它们看起来很奇怪(非常小且位于顶部中心),尝试了许多布局,但未获得合适的结果,我必须在不同面板上放置,按钮,Split Panes,Tab Panes这里是示例码。 现在我正在工作的代码有相同的问题
请看看我哪里错了
public static void main(String[] args) {
CardLayout cards = new CardLayout();
JPanel cardPanel = new JPanel();
cardPanel.setLayout(cards);
JFrame guiFrame = new JFrame();
//make sure the program exits when the frame closes
guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
guiFrame.setTitle("Frame");
guiFrame.setSize(528, 555);
//This will center the JFrame in the middle of the screen
guiFrame.setLocationRelativeTo(null);
guiFrame.setVisible(true);
JButton B_1 = new JButton("");
JButton B_2 = new JButton("");
JPanel firstCard = new JPanel();
firstCard.add(B_1);
B_1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
cards.next(cardPanel);
}
});
JPanel secondCard = new JPanel();
secondCard.add(B_2);
B_2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
cards.previous(cardPanel);
}
});
cardPanel.add(firstCard);
cardPanel.add(secondCard);
guiFrame.add(cardPanel);
}
}
您的代码甚至无法编译。
我重新排列了您的代码并添加了以下功能:
我通过调用SwingUtilities invokeLater来启动您的GUI。 这会将Swing GUI放在事件分发线程 (EDT)上。
我将类似的代码放在一起(JFrame,JPanel),以便使代码更容易理解。
我在每个卡面板上都放置了一个JLabel,因此您可以看到哪个面板是哪个面板。
我将文本放在JButton中。 它们之所以很小,是因为您没有用于显示按钮的文本或标签。
我将一些JFrame代码移到了方法的末尾。 您在Swing GUI中所做的最后一件事就是将框架设置为可见。 在使JFrame窗口可见之前,必须构造所有Swing组件。
这是您尝试编写的名片布局的经过测试的最小工作示例。
import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class CardLayoutTest implements Runnable {
@Override
public void run() {
JFrame guiFrame = new JFrame();
// make sure the program exits when the frame closes
guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
guiFrame.setTitle("Frame");
final CardLayout cards = new CardLayout();
final JPanel cardPanel = new JPanel();
cardPanel.setLayout(cards);
final JPanel firstCard = new JPanel();
firstCard.setLayout(new FlowLayout());
JLabel label1 = new JLabel("Panel 1");
firstCard.add(label1);
JButton b_1 = new JButton("Swap to Panel 2");
firstCard.add(b_1);
final JPanel secondCard = new JPanel();
secondCard.setLayout(new FlowLayout());
JLabel label2 = new JLabel("Panel 2");
secondCard.add(label2);
JButton b_2 = new JButton("Swap to Panel 1");
secondCard.add(b_2);
b_1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
cards.next(cardPanel);
}
});
b_2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
cards.previous(cardPanel);
}
});
cardPanel.add(firstCard, "First Panel");
cardPanel.add(secondCard, "Second Panel");
guiFrame.add(cardPanel);
guiFrame.setSize(528, 555);
// This will center the JFrame in the middle of the screen
guiFrame.setLocationRelativeTo(null);
guiFrame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new CardLayoutTest());
}
}
这是CarLayout
工作方式,默认情况下,它将对象放置在水平中心和垂直顶部。 如果未指定添加组件的大小,则将其调整为可能的最小大小。 配置布局对齐方式,在组件上设置尺寸或使用其他布局。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.