外部クラスでは、これらのメソッドを何度も呼び出しますが、setBackground()はこれらのメソッドを初めて呼び出すときにのみ機能します。なぜsetBackgroundは初めて動作するのですか? (JPanel)
たとえば、私はオブジェクトCellPane cellpane1, cellpane2
を持っています。cellpane1.drawPlayer()
を初めて呼び出すと、期待通りに動作し、cellpane2.drawWall()
を初めて呼び出すと、期待通りに動作します。しかし、私がcellpane1.drawPlayer()
を呼び出した後にcellpane1のdrawWall()を呼び出すと、cellpane1.drawWall()
が動作しません。背景色は変化していません。
再塗り替えまたは再検証する必要がありますか?私はこれらのものに新しいです。 :(私は間違ってやっている何
private static class CellPane extends JPanel {
private Color defaultBackground;
private CellPane() {
addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
defaultBackground = getBackground();
setBackground(Color.BLUE);
}
@Override
public void mouseExited(MouseEvent e) {
setBackground(defaultBackground);
}
});
}
private void drawDefault() {
setBackground(defaultBackground);
}
private void drawBoxOnGoal() {
defaultBackground = getBackground();
setBackground(Color.decode("#4a3d32"));
}
private void drawPlayerOnGoal() {
defaultBackground = getBackground();
setBackground(Color.decode("#f9cda8"));
}
private void drawGoal() {
defaultBackground = getBackground();
setBackground(Color.decode("#f9cda8"));
}
private void drawBox() {
defaultBackground = getBackground();
setBackground(Color.decode("#7a5230"));
}
private void drawPlayer() {
defaultBackground = getBackground();
setBackground(Color.YELLOW);
}
private void drawWall() {
defaultBackground = getBackground();
setBackground(Color.RED);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(50, 50);
}
}
[EDIT]
例:?
public class MainClass {
public static void main(String[] args) {
new MainClass();
}
private MainClass() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { }
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new Map());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
private static class Map extends JPanel implements ActionListener{
CellPane cellPane;
boolean flag = true;
JButton button = new JButton("button");
public Map() {
setLayout(new GridBagLayout());
button.addActionListener(this);
add(button);
create(flag);
}
public void create(boolean flag) {
GridBagConstraints gbc = new GridBagConstraints();
cellPane = new CellPane();
gbc.gridx = 0;
gbc.gridy = 0;
if(flag)
cellPane.drawPlayer();
else cellPane.drawWall();
add(cellPane, gbc);
}
@Override
public void actionPerformed(ActionEvent arg0) {
flag = false;
create(flag);
}
}
@SuppressWarnings("serial")
private static class CellPane extends JPanel {
private Color defaultBackground;
private void drawPlayer() {
defaultBackground = getBackground();
setBackground(Color.YELLOW);
}
private void drawWall() {
defaultBackground = getBackground();
setBackground(Color.RED);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(50, 50);
}
}}
私はそれが完璧な例ではないことを知っているが、それは実行可能ですので、私は変更したいです。コンストラクタは、flagがtrueで背景がYELLOWに設定されている場合、create(flag)を呼び出し、ボタンをクリックすると、フラグがfalseに変わり、create(flag) falseのため、代わりにcellPane.setWall()を呼び出して背景のREDを設定しますが、そうではありません。
私は間違っているのですか?たぶん簡単なことかもしれませんが、私はこれらのことに新しいです。
問題の実行可能なバージョンは高く評価されます。 –
完了。それは最良の例ではありませんが、実行可能です。前もって感謝します。 :) –
いつものように、Sir @ JBNizetによって解決策が提案されているようです:-) –