이것은 제 정신이 나게합니다. 3 개의 다른 버튼을 클릭하면 패널을 변경하려고하는데 하나의 패널에 대해서는 한 번만 작동합니다.동일한 패널을 다시 추가 할 수 없습니다.
addPerson을 클릭하면 Person 패널에 이 표시됩니다. 그런 다음 addCD를 클릭하면 CD 패널이 표시됩니다. (보기 저장소와 동일) addPerson을 클릭하면 작동하지 않습니다. nullpointer 예외를 throw합니다. addCD/viewstore를 클릭하고 THEN을 추가해도 표시되지만 두 번째 시간은 표시되지 않습니다. 테스트 파일에서
, 나는 추가로 GUI를 생성 및 제거 : 나는 널 포인터 예외를 던졌다 추가 클릭 한 경우하지만 난 그냥 이미 추가하고 컴파일하면, 그것은 ... 괜찮다고/* PERSON PANEL */
public JPanel create_PersonPnl()
{
personPnl = new JPanel(new FlowLayout(FlowLayout.CENTER));
personPnl.setBackground(Color.WHITE);
personPnl.setPreferredSize(minPnl);
/* VERTICAL BOX for Person boxes */
Box personBox = Box.createVerticalBox();
personBox.setBorder(new TitledBorder(new LineBorder(Color.DARK_GRAY), "Person"));
/* Horizontal Box for Name Lbl & TF */
Box nameBox = Box.createHorizontalBox();
nameBox.add(Box.createHorizontalStrut(10));
nameLbl = new JLabel("Name: ");
nameBox.add(nameLbl);
nameBox.add(Box.createHorizontalStrut(5));
nameTF = new JTextField();
nameBox.add(nameTF);
nameBox.add(Box.createHorizontalStrut(10));
personBox.add(nameBox);
personBox.add(Box.createVerticalStrut(10));
Box limitBox = Box.createHorizontalBox();
limitBox.add(Box.createHorizontalStrut(10));
limitLbl = new JLabel("CD Limit: ");
limitBox.add(limitLbl);
limitTF = new JFormattedTextField();
limitBox.add(limitTF);
limitBox.add(Box.createHorizontalStrut(10));
personBox.add(limitBox);
personBox.add(Box.createVerticalStrut(10));
personBox.add(addPersonBtn = new JButton("Add Person"));
personBox.add(Box.createVerticalStrut(10));
personList = new JList(temp);
personList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
scrollp = new JScrollPane(personList);
scrollp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scrollp.setSize(100, 45);
personBox.add(scrollp);
personBox.add(Box.createVerticalStrut(10));
Box optionsBox = Box.createHorizontalBox();
editPrsnBtn = new JButton("Edit");
editPrsnBtn.addActionListener(this);
optionsBox.add(editPrsnBtn);
removePrsnBtn = new JButton("Remove");
removePrsnBtn.addActionListener(this);
optionsBox.add(removePrsnBtn);
personBox.add(optionsBox);
personPnl.add(personBox);
return personPnl;
}
내 ActionPerformed 메소드에있는 내용입니다.
if(e.getSource() == addPersonBtn)
{
changePnl.removeAll();
changePnl.add(create_PersonPnl());
changePnl.revalidate();
System.out.println("PersonPnl added");
}
if(e.getSource() == addCDBtn)
{
changePnl.removeAll();
changePnl.add(create_CDPnl());
changePnl.revalidate();
}
if(e.getSource() == viewStoreBtn)
{
changePnl.removeAll();
changePnl.add(create_StorePnl());
changePnl.revalidate();
}
ChangePnl 전용 코드입니다.
changePnl = new JPanel();
changePnl.setBackground(Color.WHITE);
defaultPnl = new JPanel();
defaultPnl.setBackground(Color.WHITE);
defaultPnl.add(new JLabel("Welcome to the CD Store"));
defaultPnl.add(new JLabel("Click an option from the left"));
changePnl.add(defaultPnl);
S.O.P는 .. 실행되고 있었는지보고 디버깅 할 수 있었고 그 한 번만 인쇄 내가 .add을 꺼내하지 않는 그거야 (create_PersonPnl()); 그래서 그것을 좁혔지만 처음부터 작동하지 않기 때문에 단서가 없습니다.
감사합니다.
별도의 테스트 파일은 create_PersonPnl의 증명() 당신의 방법에서
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
public class Test implements ActionListener
{
JButton add, remove;
JButton addPersonBtn, editPrsnBtn, removePrsnBtn;
JFrame frame;
JFormattedTextField limitTF;
JLabel nameLbl, limitLbl;
JList personList;
JPanel TotalGUI, welcomePnl, mainPnl, imagePnl, changePnl, defaultPnl, cdPnl, storePnl;
JPanel personPnl;
JScrollPane scrollp;
JTextField nameTF;
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
Dimension minPnl = new Dimension(300, 400);
/* Test for JList */
String[] temp = {"1", "2", "3", "4", "1", "2", "3", "4","1", "2", "3", "4","1", "2", "3", "4" };
public Test()
{
frame = new JFrame("CD Store");
frame.setExtendedState(JFrame.NORMAL);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.pack(); //sets size based on components size in TotalGUI
//frame.setJMenuBar(create_MenuBar());
//frame.setMinimumSize(minDim);
frame.setSize(725, 550);
//set frame location (central to screen)
int fw = frame.getSize().width;
int fh = frame.getSize().height;
int fx = (dim.width-fw)/2;
int fy = (dim.height-fh)/2;
frame.getContentPane().add(create_Content_Pane());
frame.setVisible(true);
//moves the frame to the centre
frame.setLocation(fx, fy);
}
public JPanel create_Content_Pane()
{
JPanel TotalGUI = new JPanel();
TotalGUI.add(remove = new JButton("Remove"));
remove.addActionListener(this);
TotalGUI.add(add = new JButton("Add")); <- this crashes when selected
add.addActionListener(this);
//TotalGUI.add(create_PersonPnl()); <- works just fine
return TotalGUI;
}
public JPanel create_PersonPnl()
{
personPnl = new JPanel(new FlowLayout(FlowLayout.CENTER));
personPnl.setBackground(Color.WHITE);
personPnl.setPreferredSize(minPnl);
/* VERTICAL BOX for Person boxes */
Box personBox = Box.createVerticalBox();
personBox.setBorder(new TitledBorder(new LineBorder(Color.DARK_GRAY), "Person"));
/* Horizontal Box for Name Lbl & TF */
Box nameBox = Box.createHorizontalBox();
nameBox.add(Box.createHorizontalStrut(10));
nameLbl = new JLabel("Name: ");
nameBox.add(nameLbl);
nameBox.add(Box.createHorizontalStrut(5));
nameTF = new JTextField();
nameBox.add(nameTF);
nameBox.add(Box.createHorizontalStrut(10));
personBox.add(nameBox);
personBox.add(Box.createVerticalStrut(10));
Box limitBox = Box.createHorizontalBox();
limitBox.add(Box.createHorizontalStrut(10));
limitLbl = new JLabel("CD Limit: ");
limitBox.add(limitLbl);
limitTF = new JFormattedTextField();
limitBox.add(limitTF);
limitBox.add(Box.createHorizontalStrut(10));
personBox.add(limitBox);
personBox.add(Box.createVerticalStrut(10));
personBox.add(addPersonBtn = new JButton("Add Person"));
personBox.add(Box.createVerticalStrut(10));
personList = new JList(temp);
personList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
scrollp = new JScrollPane(personList);
scrollp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scrollp.setSize(100, 45);
personBox.add(scrollp);
personBox.add(Box.createVerticalStrut(10));
Box optionsBox = Box.createHorizontalBox();
editPrsnBtn = new JButton("Edit");
editPrsnBtn.addActionListener(this);
optionsBox.add(editPrsnBtn);
removePrsnBtn = new JButton("Remove");
removePrsnBtn.addActionListener(this);
optionsBox.add(removePrsnBtn);
personBox.add(optionsBox);
personPnl.add(personBox);
return personPnl;
}
public static void main(String[] args)
{
new Test();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == remove)
{
TotalGUI.removeAll();
TotalGUI.revalidate();
}
if(e.getSource() == add)
{
TotalGUI.add(create_PersonPnl());
TotalGUI.revalidate();
}
}
}
: public JPanel create_PersonPnl() {personPnl = new JPanel (새 FlowLayout (FlowLayout.CENTER)); return personPnl; } 괜찮아. 우리가 보지 못하는'changePnl'에 뭔가가 일어나고 있습니까? – David
자바 명명 규칙을 배우고 그들에게 붙이십시오 – kleopatra