java 使用 JPanels 添加多个按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14935297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 17:55:00  来源:igfitidea点击:

adding multiple buttons using JPanels

javaswingjframejpanellayout-manager

提问by Jonny_Appleby12

I have created a simple compass program that moves the GUI box around the screen and I am now progressing on to adding NE, NW etc.

我创建了一个简单的指南针程序,可以在屏幕上移动 GUI 框,现在我正在继续添加 NE、NW 等。

This involves using JPanels as this allows multiple objects. My issue is that I've made 9 separate panels for the separate buttons but I have no idea how to add them to the JFrame as everything I do doesn't seem to work.

这涉及使用 JPanel,因为它允许多个对象。我的问题是我为单独的按钮制作了 9 个单独的面板,但我不知道如何将它们添加到 JFrame,因为我所做的一切似乎都不起作用。

Any ideas would be appreciated.

任何想法,将不胜感激。

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

public class MovingCompassExtraJFrame extends JFrame implements ActionListener {

    private JButton north;
    private JButton east;
    private JButton west; 
    private JButton south;
    private JButton center;
    private JButton northEast;
    private JButton northWest;
    private JButton southEast;
    private JButton southWest;
    int screenHeight;
    int screenWidth;
    int height;
    int width;

    public MovingCompassExtraJFrame() 
   {
        super();

            width = 400;
            height = 300;

            setSize(width, height);
            setLocation(200, 100);
            setDefaultCloseOperation(EXIT_ON_CLOSE);

            getContentPane().setBackground(Color.RED); 
            getContentPane().setLayout(new GridLayout(3,3));

            setupNEPanel();
            setupNPanel();
            setupNWPanel();
            setupWPanel();
            setupCPanel();
            setupEPanel();
            setupSWPanel();
            setupSPanel();
            setupSEPanel();

            setVisible(true);

   }

   private JPanel setupNEPanel()
      { 
            northEast = new JButton("Move to North East");

            JPanel northEP = new JPanel();

            getContentPane().add(northEast);
            east.addActionListener(this);

            return northEP;
      }

    private JPanel setupNPanel()
      {
         north = new JButton("Move to North");

         JPanel northP = new JPanel();

         getContentPane().add(north);
         north.addActionListener(this);

         return northP;
      }

    private JPanel setupNWPanel()
      {
         northWest = new JButton("Move to North West");

         JPanel northWP = new JPanel();

         getContentPane().add(northWest); 
         west.addActionListener(this);

         return northWP;
      }

    private JPanel setupWPanel()
      {

            west = new JButton("Move to West");

            JPanel westP = new JPanel();

            getContentPane().add(west); 
            west.addActionListener(this);

            return westP;
      }

    private JPanel setupCPanel()
      {
            center = new JButton("Move to Center");

            JPanel centerP = new JPanel();

            getContentPane().add(center);
            center.addActionListener(this);

            return centerP;
      }

    private JPanel setupEPanel()
      {
            east = new JButton("Move to East");

            JPanel eastP = new JPanel();

            getContentPane().add(east);
            east.addActionListener(this);

            return eastP;

      }

    private JPanel setupSEPanel()
      {
            southEast = new JButton("Move to South East");

            JPanel southEP = new JPanel();

            getContentPane().add(southEast);
            east.addActionListener(this);

            return southEP;
      }

    private JPanel setupSPanel()
      {
            south = new JButton("Move to South");

            JPanel southP = new JPanel();

            getContentPane().add(south);
            south.addActionListener(this);

            return southP;
      }

    private JPanel setupSWPanel()
      {
            southWest = new JButton("Move to South West");

            JPanel southWP = new JPanel();

            getContentPane().add(southWest); 
            west.addActionListener(this);

            return southWP;
      }

    public void actionPerformed(ActionEvent e)
    {
        screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;
        screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;


        if (e.getSource() == north)
        {
            this.setLocation((screenWidth - width)/ 2, 0);
        }
        else if (e.getSource() == south)
        {
            this.setLocation((screenWidth - width)/ 2, (screenHeight-height)- 30);
        }
        else if (e.getSource() == east)
        {
            this.setLocation(screenWidth - width , (screenHeight-height)/2);
        }
        else if (e.getSource() == west)
        {
           this.setLocation(0, (screenHeight-height)/2); 
        }
        else if (e.getSource() == center)
        {
            this.setLocation((screenWidth-width)/2, (screenHeight - height)/2);
        }
  }
}

回答by Mikhail Vladimirov

JFrame frame = new JFrame ("Compass");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new GridLayout(3, 3));
frame.add (new JButton ("NW"));
frame.add (new JButton ("N"));
frame.add (new JButton ("NE"));
frame.add (new JButton ("W"));
frame.add (new JButton (" "));
frame.add (new JButton ("E"));
frame.add (new JButton ("SW"));
frame.add (new JButton ("S"));
frame.add (new JButton ("SE"));
frame.pack();
frame.setVisible(true);