2013-02-05 33 views
2

林好吗Java和有相当好的认识也不过IM停留在一个项目IM工作..即时通讯aattempting到一个JTextField移动到屏幕的左侧,但不管我怎么努力也停留在顶部的中心位置。这里是我的代码:边境布局控制。如何移动周围的事物

public class Display extends JFrame{ 
    public static final int CANVAS_WIDTH = 1000; 
    public static final int CANVAS_HEIGHT = 450; 
    public static final Color LINE_COLOR = Color.BLACK; 
    public static final Color GROUND_COLOR = Color.GREEN; 
    public static final Color CANVAS_BACKGROUND = Color.CYAN; 
    public static final Color TRANS = Color.CYAN; 

    private int x1 = CANVAS_WIDTH/2; 
    private int y1 = CANVAS_HEIGHT/2; 
    private int x2 = CANVAS_WIDTH /2; 
    private int y2 = CANVAS_HEIGHT/2; 

    private int Sx1 = CANVAS_WIDTH ; 
    private int Sy1 = CANVAS_HEIGHT ; 
    private int Sy2 = CANVAS_HEIGHT ; 

    private int Rx1 = CANVAS_WIDTH - CANVAS_WIDTH; 
    private int Ry1 = CANVAS_HEIGHT; 
    private int Rx2 = CANVAS_WIDTH; 
    private int Ry2 = CANVAS_HEIGHT ; 

    private int Lx1 = CANVAS_WIDTH/2; 
    private int Ly1 = CANVAS_HEIGHT/2; 
    private int Lx2 = CANVAS_WIDTH/2; 
    private int Ly2 = CANVAS_HEIGHT/2; 

    private int Mx1 = CANVAS_WIDTH/2; 
    private int My1 = CANVAS_HEIGHT/2; 
    private int Mx2 = CANVAS_WIDTH/2; 
    private int My2 = CANVAS_HEIGHT/2; 

    int[] xs = {380, 460, 460, 540, 540, 620, 500, 380}; 
    int[] ys = {260, 260, 250, 250, 260, 260, 205, 260}; 

    private DrawCanvas canvas; 
    private JTextField tfCount; 
    private int count = 0; 

    public class CountUpAction extends AbstractAction { 
    /** Constructor */ 
    public CountUpAction(String name, String shortDesc, Integer mnemonic) { 
     super(name); 
     putValue(SHORT_DESCRIPTION, shortDesc); 
     putValue(MNEMONIC_KEY, KeyEvent.VK_NUMPAD4); 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     addKeyListener(new KeyAdapter() { 
     public void keyPressed(KeyEvent evt) { 
      switch(evt.getKeyCode()) { 
      case KeyEvent.VK_NUMPAD1: 

      //original count 
      count += 1; 
      tfCount.setText(count + ""); 

      } 
     } 
     }); 
     } 
    } 

public class CountDownAction extends AbstractAction { 
    /** Constructor */ 
    public CountDownAction(String name, String shortDesc, Integer mnemonic) { 
     super(name); 
     putValue(SHORT_DESCRIPTION, shortDesc); 
     putValue(MNEMONIC_KEY, KeyEvent.VK_NUMPAD5); 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     addKeyListener(new KeyAdapter() { 
     public void keyPressed(KeyEvent evt) { 
     switch(evt.getKeyCode()) { 
     case KeyEvent.VK_NUMPAD2: 

     //original count 
     count -= 1; 
     tfCount.setText(count + ""); 

     } 
     } 
     }); 
    } 
    } 

    public Display() { 
    canvas = new DrawCanvas(); 
    canvas.setPreferredSize(new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT)); 

    Container cp = getContentPane(); 
    cp.setLayout(new BorderLayout()); 
    cp.add(canvas, BorderLayout.LINE_START); 

    // Create the Actions shared by the button and menu-item 
    Action countUpAction = new CountUpAction("Count Up", 
      "", new Integer(KeyEvent.VK_ENTER)); 
    Action countDownAction = new CountDownAction("Count Down", 
      "", new Integer(KeyEvent.VK_D)); 

    canvas.add(new JLabel("alt+4: activeate up")); 
    canvas.add(new JLabel("alt+5: activate down")); 
    tfCount = new JTextField("0", 8); 
    tfCount.setHorizontalAlignment(JTextField.CENTER); 
    tfCount.setEditable(false); 
    canvas.add(tfCount, BorderLayout.LINE_START); 
    //Invisible 
    JButton btnCountUp = new JButton(); 
    btnCountUp.setFocusable(false); 
    btnCountUp.setHideActionText(true); 
    btnCountUp.setContentAreaFilled(false); 
    btnCountUp.setBorderPainted(false); 
    canvas.add(btnCountUp, DrawCanvas.LEFT_ALIGNMENT); 
    //Invisible 
    JButton btnCountDown = new JButton(); 
    btnCountDown.setFocusable(false); 
    btnCountDown.setHideActionText(true); 
    btnCountDown.setContentAreaFilled(false); 
    btnCountDown.setBorderPainted(false); 

    canvas.add(btnCountDown); 
    // Set actions for buttons 
    btnCountUp.setAction(countUpAction); 
    btnCountDown.setAction(countDownAction); 

addKeyListener(new KeyAdapter() { 
    public void keyPressed(KeyEvent evt) { 
    switch(evt.getKeyCode()) { 
    case KeyEvent.VK_LEFT: 
     Rx1 -= 10; Ry1 += 10; 
     Rx2 += 10; Ry2 -= 10; 
     x1 -=10; x2 +=10; 
     Lx1 -= 10; Lx2 -= 10; 
     Mx1 += 10; Mx2 -= 10; 
     repaint(); 
     break; 
    case KeyEvent.VK_RIGHT: 
     Rx1 -= 10; Ry1 -= 10; 
     Rx2 += 10; Ry2 += 10; 
     x1 += 10; x2 += 10; 
     Lx1 += 10; Lx2 += 10; 
     Mx1 -= 10; Mx2 += 10; 
     repaint(); 
     break; 
    case KeyEvent.VK_DOWN: 
      y1 -= 10; y2 -= 10; 
      Ly1 -= 10; Ly2 -= 10; 
      Sy1 += 10; Sy2 -= 10; 
      Ry1 +=10; Ry2 += 10; 
      repaint(); 
      break; 
     case KeyEvent.VK_UP: 
      y1 += 10; y2 += 10; 
      Ly1 += 10; Ly2 += 10; 
      Sy1 -= 10; Sy2 += 10; 
      Ry1 -= 10; Ry2 -= 10; 
      repaint(); 
      break; 
     case KeyEvent.VK_M: 
      System.exit(0); 
     } 
     } 
    }); 

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setTitle("FLIGHT DISPLAY"); 
    pack(); 
    setVisible(true); 
    requestFocus(); 
    } 

    class DrawCanvas extends JPanel { 
    public void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     setBackground(CANVAS_BACKGROUND); 

     g.setColor(GROUND_COLOR); 
     //Draw ground Color 
     g.drawRect(Sx1 - Sx1,Sy1 /2, CANVAS_WIDTH, Sy2 /2); 
     g.fillRect(Sx1 - Sx1, Sy1 /2, CANVAS_WIDTH, Sy2 /2); 
     g.setColor(LINE_COLOR); 
     //Draw line centre horizontal 
     g.drawLine(Rx1, Ry1 /2, Rx2, Ry2 /2); 
     g.drawOval(x1 -15, y1 -15, 30, 30); 
     g.fillOval(x1 - 5, y1 -5, 10, 10); 
     //Draw line centre vertical 
     g.drawLine(Mx1, My1 + My1, Mx2, My2 - My2); 
     //Draw line dim 
     g.setColor(Color.YELLOW); 
     g.fillArc(300, 0, 400, 140, 0, 180); 
     g.setColor(LINE_COLOR); 
     g.drawLine(Lx1 -25, Ly1 +20, Lx2 +25, Ly2 +20); 
     g.drawLine(Lx1 -50, Ly1 +40, Lx2 +50, Ly2 +40); 
     g.drawLine(Lx1 -25, Ly1 +60, Lx2 +25, Ly2 +60); 
     g.drawLine(Lx1 -75, Ly1 +80, Lx2 +75, Ly2 +80); 
     g.drawLine(Lx1 -25, Ly1 +100, Lx2 +25, Ly2 +100); 
     //Draw line dim 
     g.drawLine(Lx1 -25, Ly1 -20, Lx2 +25, Ly2 -20); 
     g.drawLine(Lx1 -50, Ly1 -40, Lx2 +50, Ly2 -40); 
     g.drawLine(Lx1 -25, Ly1 -60, Lx2 +25, Ly2 -60); 
     g.drawLine(Lx1 -75, Ly1 -80, Lx2 +75, Ly2 -80); 
     g.drawLine(Lx1 -25, Ly1 -100, Lx2 +25, Ly2 -100); 
     //Draw polyline centre plane 
     g.drawPolyline(xs, ys, 8); 

     g.drawArc(300, 0, 400, 140, 0, 180); 
     g.drawLine(Mx1+30, My1 + My1, Mx2, My2 - My2); 
     g.drawLine(Mx1-30, My1 + My1, Mx2, My2 - My2); 

     g.drawString(Ccount, 100,100); 

    } 
    } 

    public static void main(String[] args) { 
    SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
     new Display(); 
     } 
    }); 
    } 
} 
+2

1)删除所有大喊狩猎Focus。我们是能够读取小写字母以及2)如果你有一个布局的问题,然后发布代码只包含了相关的组件和它们的布局。剥去所有听众,...(所有逻辑代码)。这使得它更容易调试你没有为你的画布设置布局的代码 – Robin

+1

,所以它使用默认的FlowLayout,而忽略你的'BorderLayout.LINE_START'约束。 –

回答

3

另外:

canvas.setLayout(new BorderLayout()); 
2
  1. 问题非常好,漂亮的代码在这里+1张贴Swing Action

  2. 不这样做canvas.setPreferredSize(new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT));覆盖getPreferredSizeclass DrawCanvas extends JPanel {

  3. 那么所有的坐标将是只为paintComponent()getHeight/Weight,而不是对所有ObjectspaintComponent()

  4. 那么所有Objects将是硬编码值/罐是(取决于你的...)可以调整大小与其父

  5. 放硬编码的坐标作为局部变量数组,以及内侧的阵列paintComponent()仅环

  6. 使用KeyBindings instead of KeyListener,那么你可以忘记追赶,并通过setFocusable()JPanel

+0

感谢所有的帮助 – Delish