Java 如何在JPanel中将JButton放置在自定义位置?
我似乎无法将按钮的自定义位置设置为JPanel。它总是在左上角添加按钮,然后继续向右。我还有一张背景图片,这让事情变得有点困难。框架如下所示:imageshack.com/a/img838/3240/ez6l.png我的代码是:Java 如何在JPanel中将JButton放置在自定义位置?,java,swing,localization,jpanel,jbutton,Java,Swing,Localization,Jpanel,Jbutton,我似乎无法将按钮的自定义位置设置为JPanel。它总是在左上角添加按钮,然后继续向右。我还有一张背景图片,这让事情变得有点困难。框架如下所示:imageshack.com/a/img838/3240/ez6l.png我的代码是: private JFrame mainframe = new JFrame(); public void main() { mainframe.setLocation(400, 150); mainframe.setDefaultCloseO
private JFrame mainframe = new JFrame();
public void main()
{
mainframe.setLocation(400, 150);
mainframe.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
mainframe.setVisible(true);
mainframe.setSize(800, 600);
JPanel menupanel2 = new JPanel();
JPanel MusicPanel = new JPanel();
mainframe.setResizable(false);
mainframe.setContentPane(new JLabel(new ImageIcon("src/res/LED0.png")));
mainframe.setLayout(new FlowLayout());
menupanel2.setBackground(Color.black);
JButton PlayButton = new JButton("Play");
PlayButton.setBackground(Color.green);
JButton highscores = new JButton("Highscores");
highscores.setBackground(Color.MAGENTA);
JButton CustomButton = new JButton("Custom Shapes");
CustomButton.setBackground(Color.orange);
JButton HelpButton = new JButton("Help");
HelpButton.setBackground(Color.red);
JButton AboutButton = new JButton("About");
AboutButton.setBackground(Color.yellow);
final JButton MusicButton = new JButton("music");
MusicButton.setPreferredSize(new Dimension(50, 50));
CustomButton.setPreferredSize(new Dimension(140, 40));
PlayButton.setPreferredSize(new Dimension(140, 40));
HelpButton.setPreferredSize(new Dimension(140, 40));
AboutButton.setPreferredSize(new Dimension(140, 40));
highscores.setPreferredSize(new Dimension(140, 40));
mainframe.add(menupanel2, BorderLayout.NORTH);
mainframe.add(MusicPanel, BorderLayout.SOUTH);
menupanel2.add(PlayButton, BorderLayout.NORTH);
menupanel2.add(CustomButton, BorderLayout.NORTH);
menupanel2.add(HelpButton, BorderLayout.NORTH);
menupanel2.add(highscores, BorderLayout.NORTH);
menupanel2.add(AboutButton, BorderLayout.NORTH);
MusicPanel.add(MusicButton, BorderLayout.SOUTH);
我想在右下角/中间/或左侧添加按钮“MusicButton”。如果您可以在JPanel中定制JButtons,请与我们分享。谢谢如果我错了,不要开始扔西红柿,但是 您的大型机布局是FlowLayout mainframe.setLayout(新的FlowLayout()) 您正在指定将MusicPanel设置为为FlowLayout设置的JFrame上的BorderLayout位置 mainframe.add(MusicPanel,BorderLayout.SOUTH) 通过这样做,您无法真正预测GUI编译时的外观。这可能是问题的根源 此外,另一方面,指南针参考不再是在边界布局中设置位置的首选方法。
我会尝试将JFrame的布局设置为BorderLayout,将MusicPanel设置为JFrame的PAGE_END(南)位置。然后,使用MusicPanel上的GridBag布局,将JButton定位到最后一行结束位置
让我知道结果如何 如果我错了,不要开始扔西红柿,但是 您的大型机布局是FlowLayout mainframe.setLayout(新的FlowLayout()) 您正在指定将MusicPanel设置为为FlowLayout设置的JFrame上的BorderLayout位置 mainframe.add(MusicPanel,BorderLayout.SOUTH) 通过这样做,您无法真正预测GUI编译时的外观。这可能是问题的根源 此外,另一方面,指南针参考不再是在边界布局中设置位置的首选方法。
我会尝试将JFrame的布局设置为BorderLayout,将MusicPanel设置为JFrame的PAGE_END(南)位置。然后,使用MusicPanel上的GridBag布局,将JButton定位到最后一行结束位置
让我知道结果如何 我认为您应该研究一下java中的布局。 使
frame.setVisible(true)成为一个好习惯代码>
在最后一段代码中。
这是我的解决方案,希望能对你有所帮助
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class LayoutDemo {
public static void main(String[] args) {
LayoutDemo gridLayoutDemo = new LayoutDemo();
try {
gridLayoutDemo.createUI();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("File read exception");
e.printStackTrace();
}
}
public void createUI() throws IOException{
JFrame frame = new JFrame("Grid Layout");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
JPanel mainPanel = new MainPanel("background.png");
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
frame.add(mainPanel,BorderLayout.CENTER);
JPanel topPanel = new JPanel();
topPanel.setOpaque(false);
mainPanel.add(topPanel);
mainPanel.add(Box.createVerticalStrut(500));
BottomPanel bottomPanel = new BottomPanel();
mainPanel.add(bottomPanel);
frame.setSize(820, 620);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
@SuppressWarnings("serial")
class MainPanel extends JPanel{
private Image background;
public MainPanel(String fileName) throws IOException{
background = ImageIO.read(new File(fileName));
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(background, 0, 0, this);
}
}
@SuppressWarnings("serial")
class BottomPanel extends JPanel{
public BottomPanel(){
LeftPanel leftPanel = new LeftPanel();
leftPanel.setOpaque(false);
leftPanel.setBorder(new EmptyBorder(0, 0, 0, 0));
RightPanel rightPanel = new RightPanel();
rightPanel.setBorder(new EmptyBorder(0, 0 , 0, 0));
rightPanel.setOpaque(false);
add(leftPanel,BorderLayout.WEST);
add(rightPanel,BorderLayout.EAST);
setOpaque(false);
}
}
@SuppressWarnings("serial")
class LeftPanel extends JPanel{
public LeftPanel(){
setLayout(new GridLayout(1,5,5,10));
setBorder(new EmptyBorder(0, 5, 0, 55));
setOpaque(false);
JButton playButton = new JButton("Play");
playButton.setBackground(Color.green);
playButton.setPreferredSize(new Dimension(140, 40));
add(playButton);
JButton shapesButton = new JButton("Custom Shapes");
shapesButton.setBackground(Color.orange);
add(shapesButton);
JButton helpButton = new JButton("Help");
helpButton.setBackground(Color.red);
add(helpButton);
JButton scoresButton = new JButton("HighScores");
scoresButton.setBackground(new Color(120,81,169));
add(scoresButton);
JButton aboutButton = new JButton("About");
aboutButton.setBackground(Color.yellow);
add(aboutButton);
}
}
@SuppressWarnings("serial")
class RightPanel extends JPanel{
public RightPanel(){
setBorder(new EmptyBorder(0, 0, 0, 0));
JButton button = new JButton(new ImageIcon("buttonIcon.png"));
button.setBorder(new EmptyBorder(0, 0, 0, 0));;
add(button,BorderLayout.CENTER);
}
}
}
以下是操作效果。
我认为您应该研究一下java中的布局。
使frame.setVisible(true)成为一个好习惯代码>
在最后一段代码中。
这是我的解决方案,希望能对你有所帮助
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class LayoutDemo {
public static void main(String[] args) {
LayoutDemo gridLayoutDemo = new LayoutDemo();
try {
gridLayoutDemo.createUI();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("File read exception");
e.printStackTrace();
}
}
public void createUI() throws IOException{
JFrame frame = new JFrame("Grid Layout");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
JPanel mainPanel = new MainPanel("background.png");
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
frame.add(mainPanel,BorderLayout.CENTER);
JPanel topPanel = new JPanel();
topPanel.setOpaque(false);
mainPanel.add(topPanel);
mainPanel.add(Box.createVerticalStrut(500));
BottomPanel bottomPanel = new BottomPanel();
mainPanel.add(bottomPanel);
frame.setSize(820, 620);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
@SuppressWarnings("serial")
class MainPanel extends JPanel{
private Image background;
public MainPanel(String fileName) throws IOException{
background = ImageIO.read(new File(fileName));
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(background, 0, 0, this);
}
}
@SuppressWarnings("serial")
class BottomPanel extends JPanel{
public BottomPanel(){
LeftPanel leftPanel = new LeftPanel();
leftPanel.setOpaque(false);
leftPanel.setBorder(new EmptyBorder(0, 0, 0, 0));
RightPanel rightPanel = new RightPanel();
rightPanel.setBorder(new EmptyBorder(0, 0 , 0, 0));
rightPanel.setOpaque(false);
add(leftPanel,BorderLayout.WEST);
add(rightPanel,BorderLayout.EAST);
setOpaque(false);
}
}
@SuppressWarnings("serial")
class LeftPanel extends JPanel{
public LeftPanel(){
setLayout(new GridLayout(1,5,5,10));
setBorder(new EmptyBorder(0, 5, 0, 55));
setOpaque(false);
JButton playButton = new JButton("Play");
playButton.setBackground(Color.green);
playButton.setPreferredSize(new Dimension(140, 40));
add(playButton);
JButton shapesButton = new JButton("Custom Shapes");
shapesButton.setBackground(Color.orange);
add(shapesButton);
JButton helpButton = new JButton("Help");
helpButton.setBackground(Color.red);
add(helpButton);
JButton scoresButton = new JButton("HighScores");
scoresButton.setBackground(new Color(120,81,169));
add(scoresButton);
JButton aboutButton = new JButton("About");
aboutButton.setBackground(Color.yellow);
add(aboutButton);
}
}
@SuppressWarnings("serial")
class RightPanel extends JPanel{
public RightPanel(){
setBorder(new EmptyBorder(0, 0, 0, 0));
JButton button = new JButton(new ImageIcon("buttonIcon.png"));
button.setBorder(new EmptyBorder(0, 0, 0, 0));;
add(button,BorderLayout.CENTER);
}
}
}
以下是操作效果。
此MCVE使用标准JSE布局、插图和边框来创建上述GUI。GUI中空白的技巧在源代码中被注释为//1
,这可以通过简单(但更长)的指令//根据需要调整数字来解释
将其拖得更宽、更高,以查看:
- UI的额外宽度在每个按钮之间大致等分。“黑色”按钮粘在UI的右侧
- 额外高度显示在最上面一行按钮和底部按钮之间
import java.awt.*;
导入java.awt.image.buffereImage;
导入javax.swing.*;
导入javax.swing.border.EmptyBorder;
公共类形状{
//用户看到的GUI(无框架)
JPanel ui=newjpanel(newborderlayout());
公共静态最终字符串[]操作={
“播放”、“自定义形状”、“帮助”、“高分”、“关于”
};
公共静态最终颜色[]颜色={
颜色。绿色,颜色。橙色,颜色。红色,颜色。洋红,颜色。黄色
};
ShapeShape(){
initUI();
}
public void initUI(){
ui.setboorder(新的EmptyBorder(20,30,20,30));//1
JPanel menuPanel=newjpanel(newgridlayout(1,0,10,10));//1
添加(菜单面板,边框布局,页面开始);
插图插图=新插图(10,20,10,20);//1
对于(int ii=0;ii
此MCVE使用标准JSE布局、插图和边框创建上述GUI。GUI中空白的技巧在源代码中注释为//1
,这可以通过简单(但更长)指令//根据需要调整数字来解释
将其拖得更宽、更高,以查看:
- 用户界面的额外宽度大致平均分配给每个按钮。“黑色”按钮固定在用户界面的右侧
- 额外高度显示在最上面一行按钮和底部按钮之间
import java.awt.*;
导入java.awt.image.buffereImage;
导入javax.swing.*;
导入javax.swing.border.EmptyBorder;
公共类形状{
//用户看到的GUI(无框架)
JPanel ui=newjpanel(newborderlayout());
公共静态最终字符串[]操作={
“播放”、“自定义形状”、“帮助”、“高分”、“关于”
};
公共静态最终颜色[]颜色={
颜色。绿色,颜色。橙色,颜色。红色,颜色。洋红,颜色。黄色
};
ShapeShape(){
initUI();
}
public void initUI(){
ui.setboorder(新的EmptyBorder(20,30,20,30));//1
JPanel menuPanel=newjpanel(newgridlayout(1,0,10,10));//1
添加(菜单面板,边框布局,页面开始);
插图插图=新插图(10,20,10,20);//1
对于(int ii=0;ii1)请参见(Yes.)2)请学习类、方法和属性名称的通用(特别是用于名称的大小写)并一致使用它们。“如何在JFrame中具有背景图像的同时,将JButton放置在JPanel中的自定义位置?”使用与将按钮添加到没有图像背景的面板时相同的方法进行操作。添加EmptyBorder
。menupanel2.Add(P