그래서이 jFrame을 Panel과 함께 사용합니다. 그 패널 안에 두 개의 패널이 더 있으며 레이아웃은 카드로 설정됩니다. 두 개의 패널 중 하나에는 버튼이 있습니다. 버튼을 눌렀을 때 표시되는 패널을 어떻게 변경합니까?자바 스윙 카드 레이아웃이 표시된 패널을 변경 하시겠습니까?
답변
이 코드 스 니펫을 사용해보십시오. 의견이 시퀀스를 이해하는 데 도움이 될 수 있기를 바랍니다.
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
@mKorbel : 사실, 방금이 코드를 한 번 게시했습니다. 어딘가 전에. 나는 일사를 읽고 있었고, 갑자기이 질문이 떠 올랐다. 나는이 것에 대한 해답이 여러 번 전에 게시되었다고 생각하지 않고 게시했다 :-) –
감사합니다! 패널 중 하나의 단추에 actionListener를 추가하려고 시도했지만 기본 양식에서 패널의 레이아웃을 가져 오려고하면 "정적이 아닌 변수 contentPane을 정적 컨텍스트에서 참조 할 수 없습니다"라고 표시됩니다. 어떤 아이디어? – Dangerosking
- 1. 카드 레이아웃이 플로팅 패널을 허용하지 않습니다
- 2. 자바 스윙 - 핑크색 배경으로 패널을 바꿔보기
- 3. 어떻게 품질의 이미지에 자바 스윙 패널을 변환하는
- 4. 자바 스윙 질의
- 5. 자바 스윙 UI 변경 방법
- 6. 런타임에 자바 스윙 언어 변경
- 7. 자바 스윙 레이아웃 문제
- 8. 자바 패널을 자바 패널에로드하십시오.
- 9. 자바 스윙 애플릿 프레임 아이콘 변경
- 10. 자바 스윙 : BorderLayout의 테두리 너비/높이 변경
- 11. 자바 스윙 - JLabel 위치
- 12. Android AVD SD 카드 위치를 변경 하시겠습니까?
- 13. 에뮬레이터에 표시된 레이아웃이 실제 장치와 동일하지 않습니다.
- 14. 자바 스윙 - JPanel과
- 15. 자바 스윙
- 16. 자바 스윙 아키텍처 문제가 있습니까?
- 17. 자바 스윙 JPanel 슬라이드 애니메이션?
- 18. 내가 자바 스윙을 사용하고 스윙
- 19. 자바 스윙 g.drawString 단어 랩
- 20. 자바 스윙 버튼 색상
- 21. extjs 3 - 카드 레이아웃이 탭 패널에서 작동하지 않습니다.
- 22. 자바 소켓이 패널을 차단합니까?
- 23. 자바 스크립트로 이미지 변경 하시겠습니까?
- 24. 자바 스윙 JScrollPane을 아래쪽으로 초기화
- 25. 컨트롤러에서 Sencha Touch의 표시된 카드 변경하기
- 26. 자바 스윙 새로 고침 JPanel
- 27. 내가 자바 스윙 애플리케이션 개발에 새로운 오전 자바
- 28. 자바 스윙 GUI에서 툴바를 동적으로 추가
- 29. 자바 스윙 타이머의 부정적 지연
- 30. 자바 스윙 학습 (GUI 빌더인가?)
CardLayout에 스왑을 수행하도록 요청하는 ActionListener를 해당 JButton에 추가합니다. 나는 당신이 CardLayout 튜토리얼을 읽었다 고 가정하고 만약 그렇다면 당신이 제공 한 정보가 부족한만큼 우리가 말할 수있는 것보다 훨씬 더 많은 정보를 제공하지는 않는다. 더 많은 도움이 필요하면 더 자세하게 설명하십시오. –
stackoverflow.com에서 비슷한 질문이 사이트를 검색 한 다음 귀하의 질문을 게시하는 것이 좋습니다. http://stackoverflow.com/q/9371460/544983 – Juvanis