jbutton不在正确的位置上有两个课程

发布于 2025-01-30 01:07:04 字数 2184 浏览 1 评论 0原文

不知道如何过度简化这一点,但是当我尝试将按钮放在其他位置时,它不会移动。我想这是因为边界景点,我看着并且找不到太多适用于此的。感谢您的帮助!我是编码的新手,这是我正在学习的新事物。

public class MazeMouse extends JPanel implements ActionListener {
    JButton d,k;
    int button = 0;
    
    
    MazeMouse(){
        setBackground(Color.black);
        
        
        k = new JButton("Press To Finish");
        d = new JButton("Press To Begin");
        add(d);
    add(k);
        
k.addActionListener(this);  
          d.addActionListener(this);
    }
    
    
    @Override
    
    
    public void paintComponent(Graphics g) {
    
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
    
       
           if(button == 1) {
                 g2.setColor(Color.red);
                g2.fillRect(300,0,200,50);
               g2.fillRect(375, 50, 50, 200);
               g2.fillRect(375,525,50,200);
               g2.fillRect(300, 725, 200, 50);
           }
            
        
    }

    @Override
    public void actionPerformed(ActionEvent e) {
 if(e.getSource()==d)
 {      
     button++;
     d.setEnabled(false);
     repaint();
 }
 
 
 if(e.getSource()==k) {
    setBackground(Color.green);
 }
        
    }

    
    
}

和班级

public class MM implements MouseMotionListener,ActionListener {
    JFrame f;    
    MazeMouse p; 
    
    int button = 0;
    public MM(){    
        
        f = new JFrame(); 
        Container c = f.getContentPane();    
                c.setLayout(new BorderLayout());   
              
    
                
                p = new MazeMouse();   
        c.add(p); 
        
        
        f.setSize(800,800);    
        f.setVisible(true);    
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    
    
    
    
    }
    public static void main(String args[ ]){    
        MM t = new MM();     
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        
    }
    @Override
    public void mouseDragged(MouseEvent e) {
        // TODO Auto-generated method stub
        
    }
    @Override
    public void mouseMoved(MouseEvent e) {
        // TODO Auto-generated method stub
        
    } 
}

Don't know how to over simplify this but when I tried to put the buttons in a different location it wouldn't move. I suppose its because of the BorderLayout, I looked and couldn't find much that applied to this. Thanks for the help! Im new to coding and this is a new thing that I'm learning.

public class MazeMouse extends JPanel implements ActionListener {
    JButton d,k;
    int button = 0;
    
    
    MazeMouse(){
        setBackground(Color.black);
        
        
        k = new JButton("Press To Finish");
        d = new JButton("Press To Begin");
        add(d);
    add(k);
        
k.addActionListener(this);  
          d.addActionListener(this);
    }
    
    
    @Override
    
    
    public void paintComponent(Graphics g) {
    
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
    
       
           if(button == 1) {
                 g2.setColor(Color.red);
                g2.fillRect(300,0,200,50);
               g2.fillRect(375, 50, 50, 200);
               g2.fillRect(375,525,50,200);
               g2.fillRect(300, 725, 200, 50);
           }
            
        
    }

    @Override
    public void actionPerformed(ActionEvent e) {
 if(e.getSource()==d)
 {      
     button++;
     d.setEnabled(false);
     repaint();
 }
 
 
 if(e.getSource()==k) {
    setBackground(Color.green);
 }
        
    }

    
    
}

And class MM

public class MM implements MouseMotionListener,ActionListener {
    JFrame f;    
    MazeMouse p; 
    
    int button = 0;
    public MM(){    
        
        f = new JFrame(); 
        Container c = f.getContentPane();    
                c.setLayout(new BorderLayout());   
              
    
                
                p = new MazeMouse();   
        c.add(p); 
        
        
        f.setSize(800,800);    
        f.setVisible(true);    
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    
    
    
    
    }
    public static void main(String args[ ]){    
        MM t = new MM();     
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        
    }
    @Override
    public void mouseDragged(MouseEvent e) {
        // TODO Auto-generated method stub
        
    }
    @Override
    public void mouseMoved(MouseEvent e) {
        // TODO Auto-generated method stub
        
    } 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文