以下のコードでは、XPos は値を変更し続けますが、私のコードでは何のアクションも実行されていません。
public class JTwinPreviewButtons {
static PreviewPanel previewPanel;
static EditorPanel editorPanel;
ArrayList<JTwinPreviewButtons> subButtons = new ArrayList<JTwinPreviewButtons>();
private static int overallYPos = 0;
Item item;
JButton plusButton;
JButton itemButton;
private boolean isPlus;
private boolean shown;
private int YPos;
private int XPos;
public JTwinPreviewButtons(Item someItem, int itemNum) {
plusButton = new JButton("-");
itemButton = new JButton(someItem.getName());
item = someItem;
isPlus = false;
shown = true;
System.out.println("Item Number: "+itemNum);
XPos = (itemNum * 15) + 25;
System.out.println(XPos); //XPos == 70
if (previewPanel!=null) {
if (itemNum == 0) {
previewPanel.addTopButton(this);
}
previewPanel.refresh(); //printed inside here XPos == 25
System.out.println(XPos); //XPos == 70
}
plusButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (isPlus) {
for (JTwinPreviewButtons subs : subButtons) {
subs.setSubShown(true);
}
plusButton.setText("-");
isPlus = false;
previewPanel.refresh();
} else {
for (JTwinPreviewButtons subs : subButtons) {
subs.setSubShown(false);
}
plusButton.setText("+");
isPlus = true;
previewPanel.refresh();
}
}
});
itemButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
//here XPos == 70
//after initialization method XPos == 25
}
}
private void updateYPos() {
YPos = overallYPos;
overallYPos += 25;
}
private void setBounds() {
updateYPos();
System.out.println(XPos); //25
plusButton.setBounds(XPos-20, YPos+2, 16, 16);
itemButton.setBounds(XPos, YPos, itemButton.getText().length()*7+15, 20);
}
public void display() {
if (shown) {
System.out.println(XPos); //25
setBounds();
previewPanel.add(plusButton);
previewPanel.add(itemButton);
}
}
そのため、XPos は初期化メソッドの期間中のみその値を保持しているように見えます。XPos の値が設定される場所は他にありません。これについて私を本当に混乱させているのは、初期化で XPos を何に設定しても常に 25 に戻ってしまうということです。私のコード...他に何が働いているのでしょうか?
編集:
import java.awt.Component;
import java.awt.Dimension;
import java.util.ArrayList;
import javax.swing.JPanel;
public class PreviewPanel {
ArrayList<JTwinPreviewButtons> topButtons = new ArrayList<JTwinPreviewButtons>();
JPanel panel = new JPanel(null);
public PreviewPanel() {
panel.setPreferredSize(new Dimension(600,750));
refresh();
}
public void addTopButton(JTwinPreviewButtons button) {
topButtons.add(button);
}
public void add(Component comp) {
panel.add(comp);
}
public void refresh() {
panel.removeAll();
recursiveRefresh(topButtons);
}
private void recursiveRefresh(ArrayList<JTwinPreviewButtons> list) {
for (JTwinPreviewButtons o : list) {
o.display();
recursiveRefresh(o.getSubButtons());
}
}
public JPanel getComponent() {
return panel;
}
}