所以我有一個面板這個jFrame。 在該面板內部還有兩個面板,佈局設置爲卡片。 這兩個面板中的一個有一個按鈕。 如何更改當按下該按鈕時顯示的面板?Java Swing卡布局更改顯示的面板?
回答
試試這個代碼片段,希望評論可以幫助你理解序列。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/* Here we are first declaring our class that will act as the
* base for other panels or in other terms the base for CardLayout.
*/
public class CardLayoutTest
{
private static final String CARD_JBUTTON = "Card JButton";
private static final String CARD_JTEXTFIELD = "Card JTextField";
private static final String CARD_JRADIOBUTTON = "Card JRadioButton";
private static void createAndShowGUI()
{
JFrame frame = new JFrame("Card Layout Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
// This JPanel is the base for CardLayout for other JPanels.
final JPanel contentPane = new JPanel();
contentPane.setLayout(new CardLayout(20, 20));
/* Here we be making objects of the Window Series classes
* so that, each one of them can be added to the JPanel
* having CardLayout.
*/
Window1 win1 = new Window1();
contentPane.add(win1, CARD_JBUTTON);
Window2 win2 = new Window2();
contentPane.add(win2, CARD_JTEXTFIELD);
Window3 win3 = new Window3();
contentPane.add(win3, CARD_JRADIOBUTTON);
/* We need two JButtons to go to the next Card
* or come back to the previous Card, as and when
* desired by the User.
*/
JPanel buttonPanel = new JPanel();
final JButton previousButton = new JButton("PREVIOUS");
previousButton.setBackground(Color.BLACK);
previousButton.setForeground(Color.WHITE);
final JButton nextButton = new JButton("NEXT");
nextButton.setBackground(Color.RED);
nextButton.setForeground(Color.WHITE);
buttonPanel.add(previousButton);
buttonPanel.add(nextButton);
/* Adding the ActionListeners to the JButton,
* so that the user can see the next Card or
* come back to the previous Card, as desired.
*/
previousButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
CardLayout cardLayout = (CardLayout) contentPane.getLayout();
cardLayout.previous(contentPane);
}
});
nextButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
CardLayout cardLayout = (CardLayout) contentPane.getLayout();
cardLayout.next(contentPane);
}
});
// Adding the contentPane (JPanel) and buttonPanel to JFrame.
frame.add(contentPane, BorderLayout.CENTER);
frame.add(buttonPanel, BorderLayout.PAGE_END);
frame.pack();
frame.setVisible(true);
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}
class Window1 extends JPanel
{
/*
* Here this is our first Card of CardLayout, which will
* be added to the contentPane object of JPanel, which
* has the LayoutManager set to CardLayout.
* This card consists of Two JButtons.
*/
private ActionListener action;
public Window1()
{
init();
}
private void init()
{
final JButton clickButton = new JButton("CLICK ME");
final JButton dontClickButton = new JButton("DON\'T CLICK ME");
action = new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
if (ae.getSource() == clickButton)
{
JOptionPane.showMessageDialog(null, "Hello there dude!"
, "Right Button", JOptionPane.INFORMATION_MESSAGE);
}
else if (ae.getSource() == dontClickButton)
{
JOptionPane.showMessageDialog(null, "I told you not to click me!"
, "Wrong Button", JOptionPane.PLAIN_MESSAGE);
}
}
};
clickButton.addActionListener(action);
dontClickButton.addActionListener(action);
add(clickButton);
add(dontClickButton);
}
}
class Window2 extends JPanel implements ActionListener
{
/*
* Here this is our second Card of CardLayout, which will
* be added to the contentPane object of JPanel, which
* has the LayoutManager set to CardLayout.
* This card consists of a JLabel and a JTextField
* with GridLayout.
*/
private JTextField textField;
public Window2()
{
init();
}
private void init()
{
setLayout(new GridLayout(1, 2));
JLabel userLabel = new JLabel("Your Name : ");
textField = new JTextField();
textField.addActionListener(this);
add(userLabel);
add(textField);
}
public void actionPerformed(ActionEvent e)
{
if (textField.getDocument().getLength() > 0)
JOptionPane.showMessageDialog(null, "Your Name is : " + textField.getText()
, "User\'s Name : ", JOptionPane.QUESTION_MESSAGE);
}
}
class Window3 extends JPanel
{
/*
* Here this is our third Card of CardLayout, which will
* be added to the contentPane object of JPanel, which
* has the LayoutManager set to CardLayout.
* This card consists of Two JLabels and two JCheckBox
* with GridLayout.
*/
private ActionListener state;
public Window3()
{
init();
}
public void init()
{
setLayout(new GridLayout(2, 2));
JLabel maleLabel = new JLabel("MALE", JLabel.CENTER);
final JCheckBox maleBox = new JCheckBox();
JLabel femaleLabel = new JLabel("FEMALE", JLabel.CENTER);
final JCheckBox femaleBox = new JCheckBox();
state = new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
if (maleBox == (JCheckBox) ae.getSource())
{
femaleBox.setSelected(false);
JOptionPane.showMessageDialog(null, "Congrats you are a Male"
, "Gender : ", JOptionPane.INFORMATION_MESSAGE);
}
else if (femaleBox == (JCheckBox) ae.getSource())
{
maleBox.setSelected(false);
JOptionPane.showMessageDialog(null, "Congrats you are a Female"
, "Gender : ", JOptionPane.INFORMATION_MESSAGE);
}
}
};
maleBox.addActionListener(state);
femaleBox.addActionListener(state);
add(maleLabel);
add(maleBox);
add(femaleLabel);
add(femaleBox);
}
}
這個例子(在這個論壇上)你可以每天使用3-5次+1 – mKorbel 2012-02-24 07:29:15
@mKorbel:太真了,我剛剛意識到,我曾經發布過這段代碼在某個地方之前。我正在讀一些東西,突然之間出現了這個問題,我沒有想到,這個東西的答案已經在SO上發佈過很多次了:-) – 2012-02-24 07:45:14
謝謝,幫助! 我試圖將actionListener添加到其中一個面板中的按鈕,但是當我嘗試從主窗體獲取面板的佈局時,它說「非靜態變量contentPane無法從靜態上下文中引用」。有任何想法嗎? – Dangerosking 2012-02-24 12:21:46
- 1. Java swing佈局,3個面板
- 2. Java佈局管理不顯示面板
- 3. 在SWING中更改佈局
- 4. 擺動卡布局更改面板顯示在用戶的行動
- 5. Java Swing - 將面板全局更改爲粉紅色背景
- 6. Swing:動態顯示面板
- 7. 更改背景和卡布局中面板的大小
- 8. 如何更改選項卡式面板的佈局?
- 9. 的Java Swing佈局
- 10. 如何從另一個面板更換卡布局面板?
- 11. 由「中間」面板分隔的兩個JList的佈局? (Java Swing)
- 12. Java Swing佈局塊
- 13. java面板佈局碼頭
- 14. 佈局使用的Java Swing
- 15. 從另一個類更改Java卡布局中的活動「卡」
- 16. 使用Java Swing作爲選項卡控件的兩個面板顯示器
- 17. 卡片佈局在修改過程後不會改變面板
- 18. wxpython +更改面板和重做佈局面板後非常小
- 19. 如何更改顯示哪個佈局
- 20. 卡布局不承認浮動面板
- 21. 使用卡布局的Java GUI切換面板
- 22. Java swing佈局問題
- 23. Java Swing佈局問題
- 24. Java Swing,如何在Next按鈕後更改佈局
- 25. JAVA Swing - 從另一個卡面板卡面板上的選項卡式面板setSelectedIndex()
- 26. 使用Java Swing的顯示器中的可調節面板
- 27. 更改所有tabbedpane面板java swing的顏色按鈕操作
- 28. 更改Magento購物車頁面上的模板頁面佈局
- 29. 的Java Swing面板問題
- 30. 如何更改按鈕點擊卡內佈局的面板 - extjs 4
通過向該JButton添加一個ActionListener來請求CardLayout進行交換。我假設你已經閱讀過CardLayout教程,如果是這樣的話,我們可以告訴你的只是你提供的很少的信息。如果您需要更多幫助,請考慮提供更多詳細信息。 – 2012-02-24 02:49:29
也有類似的問題在stackoverflow.com,搜索該網站,然後發佈你的問題,我建議。 http://stackoverflow.com/q/9371460/544983 – Juvanis 2012-02-24 03:31:13