Swing 在JPanel上定位按钮

Swing 在JPanel上定位按钮,swing,jpanel,awt,layout-manager,boxlayout,Swing,Jpanel,Awt,Layout Manager,Boxlayout,我试图设置这个代码的中间的三个按钮> jPanel ,它设置在另一个面板之上。 一切正常,但无论发生什么情况,三个按钮仍保持在同一位置 如何移动面板2中央的三个按钮?现在这三个按钮位于面板2的左中部 我的面板代码如下: public AbcGeniusPanel() { //this.setVisible(false); ImageIcon[] alphabets = new ImageIcon[26]; ImageIcon[] images = new ImageIcon[

我试图设置这个代码的中间的三个按钮> jPanel ,它设置在另一个面板之上。

一切正常,但无论发生什么情况,三个按钮仍保持在同一位置

如何移动
面板2
中央的三个按钮?现在这三个按钮位于
面板2的左中部

我的面板代码如下:

public AbcGeniusPanel()
 {
   //this.setVisible(false);
   ImageIcon[] alphabets = new ImageIcon[26];
   ImageIcon[] images = new ImageIcon[26];
   setBackground(Color.yellow);

   //Load the images for alphabet images into the alphabets array using a for loop 
  for(int i = 0; i < alphabets.length; i++)
    {
    alphabets[i] = new ImageIcon("C:\\Users\\Dip\\Desktop\\Java Projects\\AbcGeniusApp\\src\\Alphabets\\"+(i+1)+".png");
    }

  //Load the images images in the IMageIcon array
  for(int i = 0; i < images.length; i++)
    {
    images[i] = new ImageIcon("C:\\Users\\Dip\\Desktop\\Java Projects\\AbcGeniusApp\\src\\Images\\"+(i+1)+".png");
    }

    //Create two JPanel objects
    JPanel panel = new JPanel();
    JPanel panel2 = new JPanel();
   //Set a layoutManager on the panel

    panel.setLayout(new GridLayout(2, 13, 5, 5)); //This is good for now

    //Create an array for holdoing the buttons
    buttons = new JButton[26];

    /
    //Try passing Images inside the JButton parameter later.
    for(int i = 0; i < 26; i++)
    {
    buttons[i] = new JButton(alphabets[i]);
    }

    setLayout(new BorderLayout(2,0));
    panel2.setLayout(new BoxLayout(panel2, BoxLayout.X_AXIS));
    //add the panel to the Border layout
    add(panel,BorderLayout.SOUTH);
    add(panel2);

    //Add evenHandling mechanism to all the buttons
     for(int k = 0; k<26; k++)
     {
      buttons[k].addActionListener(this);
     }
    for(int count1 = 0; count1<26; count1++)
    {
     panel.add(buttons[count1]);
    }

    JButton button1 = new JButton();
    JButton button2 = new JButton();
    JButton button3 = new JButton();

    panel2.add(button1);
    panel2.add(button2); 
    panel2.add(button3);
     }
public AbcGeniusPanel()
{
//此.setVisible(false);
ImageIcon[]字母=新的ImageIcon[26];
ImageIcon[]images=新的ImageIcon[26];
挫折地面(颜色:黄色);
//使用for循环将字母表图像的图像加载到字母表数组中
for(int i=0;i
只为按钮使用FlowLayout会更容易,这与我在没有额外代码的情况下所说的是一样的。布局可能有一个潜在的缺点,导致一个或两个按钮流到下一行,但使用简单的应用程序可能不太可能

以下是两个示例。注释掉一行,注释(??)另一行,以查看按钮的不同方法:

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class AlphabetExample {

   public static void main(String[] args) {
      AlphabetExample alphabetExample = new AlphabetExample();
      JFrame frame = alphabetExample.createGui();
      frame.setVisible(true);
   }

   private JFrame createGui() {
      JFrame frame = new JFrame("Letters!");
      frame.setSize(400, 300);

      Container contentPane = frame.getContentPane();
      contentPane.add(setupLetters(), BorderLayout.CENTER);
//      contentPane.add(setupButtonsWithBox(), BorderLayout.NORTH); // <-- with a BoxLayout
      contentPane.add(setupButtonsWithFlowPane(), BorderLayout.NORTH); // <-- with a FlowLayout

      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      return frame;
   }

   private JPanel setupLetters() {
      String  letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

      JPanel lettersPanel = new JPanel(new GridLayout(2, 13, 5, 5));

      for (char x : letters.toCharArray()) {
         final String letter = String.valueOf(x);
         JButton button = new JButton(letter);
         button.setActionCommand(letter);
         lettersPanel.add(button);
      }

      return lettersPanel;
   }

   private JComponent setupButtonsWithBox() {
      Box b = Box.createHorizontalBox();
      b.add(Box.createHorizontalGlue());
      b.add(new JButton("Left Button"));
      b.add(Box.createHorizontalStrut(5));
      b.add(new JButton("Center Button"));
      b.add(Box.createHorizontalStrut(5));
      b.add(new JButton("Right Button"));
      b.add(Box.createHorizontalGlue());
      return b;
   }

   private JComponent setupButtonsWithFlowPane() {
      JPanel panel = new JPanel(); // default layout manager is FlowLayout
      panel.add(new JButton("Left Button"));
      panel.add(new JButton("Center Button"));
      panel.add(new JButton("Right Button"));

      return panel;
   }
}
导入java.awt.BorderLayout;
导入java.awt.Container;
导入java.awt.GridLayout;
导入javax.swing.Box;
导入javax.swing.JButton;
导入javax.swing.JComponent;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
公共类字母表示例{
公共静态void main(字符串[]args){
AlphabetExample AlphabetExample=新的AlphabetExample();
JFrame=alphabetExample.createGui();
frame.setVisible(true);
}
私有JFrame createGui(){
JFrame=newjframe(“字母!”);
框架。设置尺寸(400300);
容器contentPane=frame.getContentPane();
添加(setupLetters(),BorderLayout.CENTER);
//contentPane.add(setupButtonsWithBox(),BorderLayout.NORTH);//这解决了我的问题

     for(int count1 = 0; count1<3; count1++)
    {
     panel2.add(Box.createHorizontalGlue());
     panel2.add(imageButtons[count1]);
     panel2.add(Box.createHorizontalGlue());   
    }

对于(int count1=0;count11),源代码中只需要一行空白。在
{
之后或
}
之前的空白行通常也是多余的。2)为了更快地获得更好的帮助,请发布一个(最小完整的可验证示例)或(简短、自包含、正确的示例).我不知道中间偏左到底是什么意思。它是中间还是左边?无论如何,使用一个简单的具有适当对齐方式的FlowLayout应该会为您提供预期的布局。这里是输出的链接按钮位于左侧,无法移动。但无论如何,谢谢,我将尝试使用不同对齐方式的FlowLayout。