[英]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.