0
FlowLayout 내부에 여러 개의 CardLayouts를 나란히 표시하려고합니다. 모든 것이 잘 실행되지만 창에 아무것도 나타나지 않습니다. FlowLayout에 CardLayouts 및 그 구성 요소를 표시하려면 어떻게합니까?FlowLayout에서 여러 CardLayouts을 나란히 중첩
나는이 모든 문제를 발견하는데 도움이되지 않고 관련이있는 docs을 이미 읽었습니다.
이import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class RenderTest extends JPanel{
private JFrame window;
private FlowLayout topLevelLayout;
private Slot[] slots;
public static void main(String[] args) {
RenderTest instance = new RenderTest();
instance.init();
}
private void init(){
window = new JFrame("Render Test");
topLevelLayout = new FlowLayout();
window.setLayout(topLevelLayout);
window.setResizable(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLocationRelativeTo(null);
slots = new Slot[]{new Slot(0), new Slot(2)};
window.add(slots[0]);
window.add(slots[1]);
window.pack();
window.setVisible(true);
}
private class Slot extends JPanel{
JPanel panel;
CardLayout cardLayout;
public Slot(int index){
RemoveButton remove = new RemoveButton(index);
AddButton add = new AddButton(index);
cardLayout = new CardLayout();
panel = new JPanel();
panel.setLayout(cardLayout);
cardLayout.addLayoutComponent(add.getPanel(), "add");
cardLayout.addLayoutComponent(remove.getPanel(), "show");
topLevelLayout.addLayoutComponent("card"+index, panel);
}
private JPanel getPanel(){
return this.panel;
}
private CardLayout getCardLayout(){
return this.cardLayout;
}
}
private class AddButton extends JPanel{
JPanel panel;
private AddButton(int index){
panel = new JPanel();
JButton addButton = new JButton("+");
addButton.setVerticalTextPosition(AbstractButton.CENTER);
addButton.setHorizontalTextPosition(AbstractButton.CENTER);
addButton.setActionCommand("add"+index);
addButton.addActionListener(new Button());
panel.add(addButton);
}
private JPanel getPanel(){
return this.panel;
}
}
private class RemoveButton extends JPanel{
JPanel panel;
private RemoveButton(int index){
panel = new JPanel();
JButton removeButton = new JButton("-");
removeButton.setVerticalTextPosition(AbstractButton.CENTER);
removeButton.setHorizontalTextPosition(AbstractButton.CENTER);
removeButton.setActionCommand("remove"+index);
removeButton.addActionListener(new Button());
panel.add(removeButton);
}
private JPanel getPanel(){
return this.panel;
}
}
class Button implements ActionListener{
public void actionPerformed(ActionEvent e) {
System.out.println(e.getActionCommand());
if (e.getActionCommand().equals("add0")){
slots[0].getCardLayout().show(getParent(), "show");
}
if (e.getActionCommand().equals("add1")){
slots[1].getCardLayout().show(getParent(), "show");
}
if (e.getActionCommand().equals("remove0")){
slots[0].getCardLayout().show(getParent(), "hide");
}
if (e.getActionCommand().equals("remove1")){
slots[1].getCardLayout().show(getParent(), "hide");
}
}
}
}
업데이트 된 코드 대신 new JPanel
의의 this
를 사용하기 : https://pastebin.com/e0fhkaen
가 작동을 얻었다 : 여기
내 예제 코드입니다 페이스트 빈 5XrFYarD
간다하지만 여전히 아무것도 표시되지 않습니다. – bloxz64