2012-01-31 50 views
1

這是文件1(我的導師更喜歡是我們使用單獨的文件爲每個分機)如何將圓圈移動到「WEST」邊框佈局框架?

import javax.swing.*; 
import java.awt.*; 

public class Lab2 extends JFrame { 

Lab2(){ 
    setTitle("Lab 1b - Application #2"); 
    Lab2Panel p = new Lab2Panel(); 
    add(p); 
} 

public static void main(String[] args){ 

    Lab2 frame = new Lab2(); 
    frame.setTitle("Lab2 Application # 1"); 
    frame.setLocationRelativeTo(null); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setSize(200, 400); 
    frame.setVisible(true); 
    } 

} 

文件2:

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 

public class Lab2Panel extends JPanel{ 
Lab2Button canvas = new Lab2Button(); 
JPanel panel = new JPanel(); 

Lab2Panel() { 

    setLayout(new BorderLayout()); 

    JButton leftButton = new JButton("left"); 
    JButton rightButton = new JButton("right"); 
    JButton upButton = new JButton("up"); 
    JButton downButton = new JButton("down"); 

    panel.add(leftButton); 
    panel.add(rightButton); 
    panel.add(upButton); 
    panel.add(downButton); 

    this.add(canvas, BorderLayout.CENTER); 
    this.add(panel, BorderLayout.SOUTH); 

    leftButton.addActionListener(new Lab2MoveBallListener()); 
} 


} 

文件3:

import javax.swing.*; 
import java.awt.*; 

public class Lab2Button extends JPanel { 
int radius = 5; 

protected void paintComponent(Graphics g){ 
    super.paintComponent(g); 
    g.drawOval(getWidth()/2 - radius, getHeight()/2 - radius, 2 * radius, 2 * radius); 

} 

     public void moveLeft(){ 

      this.add(this, BorderLayout.WEST); 
      this.repaint(); 
     } 


} 

動作監聽代碼:

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 

class Lab2MoveBallListener implements ActionListener{ 


public void actionPerformed(ActionEvent e){ 
    this.moveLeft(); 
} 
} 

當用戶單擊「左」按鈕時,我正嘗試將圓圈移至WEST邊框佈局。有人能幫助我嗎。

+0

不擴展每個類並只擴展actionlistener的原因是什麼? – Robert 2012-01-31 02:23:00

+1

比繼承更喜歡構圖。 *必須*實現'ActionListener',其他可以用其他方式完成。 – 2012-01-31 02:24:53

+0

我不認爲我會跟着你。到目前爲止,在課堂上我們只創建了這樣的類。當我們需要一個按鈕時,我們編寫一個擴展JButton的類並調用該類的一個實例。 – Robert 2012-01-31 02:28:18

回答

3

我不太確定我明白這個GUI應該做什麼,但是這樣做可以將圓圈向左移動。點3)& 4)我的評論仍然需要解決。

這是更改後的代碼。

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 

public class Lab2 extends JFrame { 

Lab2(){ 
    setTitle("Lab 1b - Application #2"); 
    Lab2Panel p = new Lab2Panel(); 
    add(p); 
} 

public static void main(String[] args){ 

    Lab2 frame = new Lab2(); 
    frame.setTitle("Lab2 Application # 1"); 
    frame.setLocationRelativeTo(null); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setSize(200, 400); 
    frame.setVisible(true); 
    } 

} 

class Lab2Panel extends JPanel{ 
Lab2Button canvas = new Lab2Button(); 
JPanel panel = new JPanel(); 

Lab2Panel() { 

    setLayout(new BorderLayout()); 

    JButton leftButton = new JButton("left"); 
    JButton rightButton = new JButton("right"); 
    JButton upButton = new JButton("up"); 
    JButton downButton = new JButton("down"); 

    panel.add(leftButton); 
    panel.add(rightButton); 
    panel.add(upButton); 
    panel.add(downButton); 

    this.add(canvas, BorderLayout.CENTER); 
    this.add(panel, BorderLayout.SOUTH); 

    leftButton.addActionListener(new Lab2MoveBallListener(canvas)); 
} 


} 

class Lab2Button extends JPanel { 
int radius = 5; 
int x = -1; 
int y = -1; 

protected void paintComponent(Graphics g){ 
    if (x<0 || y<0) { 
     x = getWidth()/2 - radius; 
     y = getHeight()/2 - radius; 
    } 
    super.paintComponent(g); 
    g.drawOval(x,y, 2 * radius, 2 * radius); 

} 

     public void moveLeft(){ 

      //this.add(this, BorderLayout.WEST); 
      x -= 5; 
      this.repaint(); 
     } 


} 

class Lab2MoveBallListener implements ActionListener{ 
    private Lab2Button canvas; 

Lab2MoveBallListener(Lab2Button canvas) { 
    this.canvas = canvas; 
} 

public void actionPerformed(ActionEvent e){ 
    canvas.moveLeft(); 
} 
} 

..how我按鈕之間的區別行動執行?

有很多方法。

  1. ActionEvent.getActionCommand()/getSource()if/else語句來選擇正確的行動。
  2. 爲每個按鈕添加一個單獨的偵聽器。這在現實世界的代碼中更常見。無需獲取源代碼或檢查操作命令。
+0

當你點擊一個特定的按鈕(左,右,上,下)時,賦值是讓我們有一個「球」並將它移動到屏幕上。 – Robert 2012-01-31 02:32:23

+0

我的想法是,我可以使用BorderLayout來移動「球」,當你點擊有問題的按鈕。 – Robert 2012-01-31 02:33:04

+0

此代碼與我的想法很接近,但我需要在「中心」窗格中啓動「球」。 – Robert 2012-01-31 02:36:18

2

我認爲你對「this」這個關鍵字感到困惑。以下可能會讓你更接近你想要的東西。

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 

public class Lab2Panel extends JPanel 
{ 
    Lab2Button canvas = new Lab2Button(); 
    JPanel panel = new JPanel(); 

    Lab2Panel() 
    { 
     setLayout(new BorderLayout()); 

     JButton leftButton = new JButton("left"); 
     JButton rightButton = new JButton("right"); 
     JButton upButton = new JButton("up"); 
     JButton downButton = new JButton("down"); 

     panel.add(leftButton); 
     panel.add(rightButton); 
     panel.add(upButton); 
     panel.add(downButton); 

     this.add(canvas, BorderLayout.CENTER); 
     this.add(panel, BorderLayout.SOUTH); 

     leftButton.addActionListener(new ActionListener() 
     { 
     public void actionPerformed(ActionEvent e) 
     { 
      Lab2Panel.this.remove(canvas); 
      Lab2Panel.this.add(canvas, BorderLayout.WEST); 
      Lab2Panel.this.repaint(); 
     } 
     }); 
    } 
} 
+0

Lab2Panel.this.remove(canvas)調用畫布的特定實例嗎? – Robert 2012-01-31 02:33:54

+0

是的,因爲canvas是Lab2Panel類中的類範圍變量。就Lab2Panel類的給定實例而言,只有一個「畫布」,即在Lab2Button canvas = new Lab2Button()行中創建的畫布。 – IaD 2012-01-31 04:07:31

2

這是我建議的,希望它適合你。

Lab2.java

import javax.swing.JFrame; 


public class Lab2 extends JFrame 
{ 

    Lab2() 
    { 
     setTitle("Lab 1b - Application #2"); 
     Lab2Panel p = new Lab2Panel(); 
     add(p); 
    } 

    public static void main(String[] args) 
    { 
     Lab2 frame = new Lab2(); 
     frame.setTitle("lab2 Application # 1"); 
     frame.setLocationRelativeTo(null); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setSize(300, 400); 
     frame.setVisible(true); 
    } 

} 

Lab2Panel.java

import java.awt.BorderLayout; 

import javax.swing.JButton; 
import javax.swing.JPanel; 


public class Lab2Panel extends JPanel 
{ 

    Lab2Button canvas = new Lab2Button(); 
    JPanel panel = new JPanel(); 

    Lab2Panel() 
    { 
     setLayout(new BorderLayout()); 

     JButton leftButton = new JButton("left"); 
     JButton rightButton = new JButton("right"); 
     JButton upButton = new JButton("up"); 
     JButton downButton = new JButton("down"); 

     panel.add(leftButton); 
     panel.add(rightButton); 
     panel.add(upButton); 
     panel.add(downButton); 

     this.add(canvas, BorderLayout.CENTER); 
     this.add(panel, BorderLayout.SOUTH); 

     leftButton.addActionListener(new Lab2MoveBallListener(canvas)); 
     rightButton.addActionListener(new Lab2MoveBallListener(canvas)); 
     upButton.addActionListener(new Lab2MoveBallListener(canvas)); 
     downButton.addActionListener(new Lab2MoveBallListener(canvas)); 

    } 

} 

Lab2MoveBallListener.java

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 


public class Lab2MoveBallListener implements ActionListener 
{ 
    private Lab2Button canvas; 

    public Lab2MoveBallListener(Lab2Button canvas) 
    { 
     this.canvas = canvas; 

    } 

    @Override 
    public void actionPerformed(ActionEvent e) 
    { 
     if (e.getActionCommand().equals("left")) 
     { 
      canvas.moveLeft(); 
     } 

     if (e.getActionCommand().equals("right")) 
     { 
      canvas.moveRight(); 
     } 

     if (e.getActionCommand().equals("up")) 
     { 
      canvas.moveTop();   
     } 

     if (e.getActionCommand().equals("down")) 
     { 
      canvas.moveDown(); 
     } 

    } 

} 

Lab2Button.java

import java.awt.Graphics; 

import javax.swing.JPanel; 


public class Lab2Button extends JPanel 
{ 
    int radius = 5; 
    int x = -1; 
    int y = -1; 

    protected void paintComponent(Graphics g) 
    { 
     if (x < 0 || y < 0) 
     { 
      x = getWidth()/2 - radius; 
      y = getHeight()/2 - radius; 
     } 
     super.paintComponent(g); 
     g.drawOval(x, y, 2 * radius, 2 * radius); 
    } 

    public void moveLeft() 
    { 
     x -= 5; 
     this.repaint(); 
    } 

    public void moveRight() 
    { 
     x += 5; 
     this.repaint(); 
    } 

    public void moveTop() 
    { 
     y -= 5; 
     this.repaint(); 
    } 

    public void moveDown() 
    { 
     y += 5; 
     this.repaint(); 
    } 

}