簡體   English   中英

單擊按鈕時移動卡住

[英]Movement stuck when clicking buttons

創建了一個類似於繪圖的程序,其中有一個矩形,其在屏幕內的移動可以通過“w、a、s、d”鍵控制,並且可以使用鼠標上的滾動器增加或減小其大小。 還有幾個不同顏色的按鈕,按下時會用各自的顏色填充矩形。 現在的問題是在單擊任何顏色按鈕后停止移動,即。 矩形不動。 您可以增加和減少它的大小,但不能使用“w,a,s,d”鍵移動。 請幫幫我。

而且作為一個可選請求,我也在嘗試用這個矩形進行繪畫,即。 當我按下空格鍵時,我想填充在指定區域選擇的顏色。 現在即使我能做到。 我做的下一個動作,即。 按“w、a、s、d”鍵或滾動條,顏色會消失。 現在我知道顏色或 fillRect() 必須以某種方式保存,以便下一個操作不會影響它,但我嘗試了幾種方法但它沒有發生。 我已經評論了下面這幅畫的代碼。

我的第一個請求是我的主要請求,如果您無法理解我的意思或不知道解決方案,請留下第二個請求。

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

public class Animation extends Frame implements KeyListener,MouseWheelListener,ActionListener {
    int x,y,a,b;
    char choice1;
    int draw=1;
    int n=0;
    int color1,color2,color3;
    Button button1,button2,button3,button4,button5,button6,button7,button8,button9,button10;

    Animation() {
        setSize(1000, 1000);
        setVisible(true);
        x = 500;
        y = 500;
        a = 20;
        b = 50;
        addKeyListener(this);
        addMouseWheelListener(this);

         JFrame frame = new JFrame();

        frame.getContentPane().setLayout(null);

        button1 = new Button("Black");
        button2 = new Button("Blue");
        button3 = new Button("Green");
        button4 = new Button("Orange");
        button5 = new Button("Red");
        button6 = new Button("Yellow");
        button7 = new Button("Gray");
        button8 = new Button("Cyan");
        button9 = new Button("Magenta");
        button10 = new Button("Pink");

        add(button1);add(button2);add(button3);add(button4);add(button5);
        add(button6);add(button7);add(button8);add(button9);add(button10);

        button1.setBounds(50,680,50,20); button2.setBounds(120,680,50,20);
        button3.setBounds(190,680,50,20); button4.setBounds(260,680,50,20);
        button5.setBounds(330,680,50,20); button6.setBounds(400,680,50,20);
        button7.setBounds(470,680,50,20); button8.setBounds(540,680,50,20);
        button9.setBounds(610,680,50,20); button10.setBounds(680,680,50,20);

        button1.addActionListener(this);button2.addActionListener(this);
        button3.addActionListener(this);button4.addActionListener(this);
        button5.addActionListener(this);button6.addActionListener(this);
        button7.addActionListener(this);button8.addActionListener(this);
        button9.addActionListener(this);button10.addActionListener(this);
        addWindowListener(new WindowListener() {
            @Override
            public void windowOpened(WindowEvent e) {
            }

            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }

            @Override
            public void windowClosed(WindowEvent e) {
            }

            @Override
            public void windowIconified(WindowEvent e) {
            }

            @Override
            public void windowDeiconified(WindowEvent e) {
            }

            @Override
            public void windowActivated(WindowEvent e) {
            }

            @Override
            public void windowDeactivated(WindowEvent e) {
            }
        });
    }

    @Override
    public void keyTyped(KeyEvent e) {
    }
    @Override
    public void keyPressed(KeyEvent e) {
    }
    @Override
    public void keyReleased(KeyEvent e) {
        choice1 = e.getKeyChar();
        if (choice1 == 'w') {
            y = y - 10;
        }
        if (choice1 == 's') {
            y = y + 10;
        }
        if (choice1 == 'a') {
            x = x - 10;
        }
        if (choice1 == 'd') {
            x = x + 10;
        }
        if(choice1 == ' '){
            draw=2;
        }
        repaint();
    }

    @Override
    public void mouseWheelMoved(MouseWheelEvent e) {
        double p = e.getPreciseWheelRotation();
        if(p>0){
            a=a+5;
            b=b+5;
        } else{
            a=a-5;
            b=b-5;
        }
        repaint();
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getActionCommand().equals("Black")){
            color1 = 0;
            color2 = 0;
            color3 = 0;
        }
        if(e.getActionCommand().equals("Blue")){
            color1 = 0;
            color2 = 0;
            color3 = 255;
        }
        if(e.getActionCommand().equals("Green")){
            color1 = 0;
            color2 = 255;
            color3 = 0;
        }
        if(e.getActionCommand().equals("Orange")){
            color1 = 255;
            color2 = 165;
            color3 = 0;
        }
        if(e.getActionCommand().equals("Red")){
            color1 = 255;
            color2 = 0;
            color3 = 0;
        }
        if(e.getActionCommand().equals("Yellow")){
            color1 = 255;
            color2 = 255;
            color3 = 0;
        }
        if(e.getActionCommand().equals("Gray")){
            color1 = 169;
            color2 = 169;
            color3 = 169;
       }
        if(e.getActionCommand().equals("Cyan")){
            color1 = 0;
            color2 = 255;
            color3 = 255;
        }
        if(e.getActionCommand().equals("Magenta")){
            color1 = 255;
            color2 = 0;
            color3 = 255;
        }
        if(e.getActionCommand().equals("Pink")){
            color1 = 255;
            color2 = 192;
            color3 = 203;
        }
        repaint();

    }
    public void paint(Graphics g) {
        if(draw==1) {

            g.drawRect(x, y, a, b);
            g.setColor(new Color(color1,color2,color3));
            g.fillRect(x,y,a,b);

        }
//        if(draw==2){
//            fillColor(g);
//            draw=1;
//        }
    }

//    public void fillColor(Graphics g){
//        g.setColor(Color.red);
//        int[] temp1 = new int[50];
//        temp1[n] = x;
//        int[] temp2 = new int[50];
//        temp2[n] = y;
//        int[] temp3 = new int[50];
//        temp3[n] = a;
//        int[] temp4 = new int[50];
//        temp4[n] = b;
//
//
//        n++;
//        for (int i=0;i<n;i++){
//            System.out.println("abcd");
//            g.fillRect(temp1[n],temp2[n],temp3[n],temp4[n]);
//        }
//
//    }

    public static void main(String[] args) {
        Animation animation = new Animation();

    }

}

首先是基本的設計問題:

  1. 不要擴展框架。 無需擴展任何 class。

  2. 不要在 Swing 應用程序中使用 AWT 組件。 JFrame是 Swing。 “按鈕”是 AWT。 對於 Swing 您應該使用JButton

  3. 不要對整個框架使用 null 布局。 保留默認的 BorderLayout。 閱讀 Swing 教程中有關如何使用 BorderLayout的部分。

  4. 對於按鈕,您應該創建一個 JPanel(默認情況下使用 FlowLayout)並將您的按鈕添加到此面板。

然后使用以下方法將此面板添加到框架中:

frame.add(buttonPanel, BorderLayout.PAGE_END);
  1. 自定義繪畫是通過擴展JPanel來完成的,然后覆蓋paintComponent(...)方法。 所以你需要創建一個DrawingPanel來繪制你的矩形。 閱讀有關自定義繪畫的 Swing 教程以獲取工作示例,以幫助您更好地構建代碼。

然后使用以下命令將繪圖面板添加到框架:

frame.add(drawingPanel, BorderLayout.CENTER):
  1. 不要使用 WindowListener 來關閉框架。

創建框架時,您可以設置 close 屬性:

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  1. 使用多個嵌套的 if 語句,就像您在 ActionListner 中所做的那樣是糟糕的設計。 隨着您繼續支持更多 colors,代碼只會不斷增長。 另一種方法可能是:

a) 創建一個 HashMap 作為實例變量以包含按鈕和 colors:

private HashMap<JButton, Color> buttonColors = new HashMap<>();
private Color rectangleColor = Color.BLACK;

b) 現在,當您創建每個按鈕時,您可以更新 hash map:

buttonColors.put(button1, Color.BLACK);

c) 現在您的 ActionListener 中的代碼變為:

JButton button = (JButton)e.getSource();
Color color = buttonColors.get(button);
rectangleColor = color;
repaint();

d) 並且您的 paintComponent() 方法中的代碼變為:

//g.setColor(new Color(color1,color2,color3));
g.setColor( rectangleColor );
  1. 不要通過蠻力創建按鈕。 創建一個方法。

調用方法的代碼

buttonsPanel.add( createButton("Black", Color.BLACK) );
buttonsPanel.add( createButton("Blue", Color.BLUE) );

並且createButton(..)方法看起來像:

public JButton (String text, Color color)
{
    JButton button = new JButton( text );
    button.addActionListener(this);
    buttonColors.put(button, color);

    return button;
}

這里有很多建議。 代碼在未經測試的情況下發布,因此請了解每個概念並一次更改一個並進行測試以確保您正確實施每個步驟。

現在的問題是在單擊任何顏色按鈕后停止移動,即。 矩形不動。

KeyEvent 僅傳遞給具有焦點的組件。 當您單擊按鈕時,繪畫面板失去焦點並且不再響應事件。

更好的解決方案是使用Key Bindings 即使組件沒有焦點,鍵綁定也會起作用。 有關更多信息和工作示例,請參閱: 使用鍵盤進行運動

第一個的簡短回答。 添加:

 button1.setFocusable(false);
    button2.setFocusable(false);
    button3.setFocusable(false);
    button4.setFocusable(false);
    button5.setFocusable(false);
    button6.setFocusable(false);
    button7.setFocusable(false);
    button8.setFocusable(false);
    button9.setFocusable(false);
    button10.setFocusable(false);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM