我正在尝试设置此JPanel
中间的三个按钮,该按钮设置在另一个面板上方。
一切正常,但无论如何,三个按钮都保持在同一位置。
如何移动panel2
中心的三个按钮?现在,三个按钮位于panel2
的中央左侧。
我的面板代码在这里:
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);
}
您可以使用BoxLayout(使用Box.createHorizontalBox()可能更容易),但是您必须在盒子的每一端放置垂直胶水。您可能还想在按钮之间放置水平支柱以给它们一些间距。
只为您的按钮使用 FlowLayout 会更容易,它相当于我所说的,无需额外的代码。布局可能存在一个潜在的缺点,导致一个按钮或 2 个按钮流到下一行,但对于您的简单应用程序,这可能没有太大的机会。
下面是两个示例。注释掉一行,并在 (???) 另一行中注释以查看按钮的不同方法:
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;
}
}
这解决了我的问题
for(int count1 = 0; count1<3; count1++)
{
panel2.add(Box.createHorizontalGlue());
panel2.add(imageButtons[count1]);
panel2.add(Box.createHorizontalGlue());
}