java jpanel 位置_如何将JButton放在JPanel的自定义位置?

我想你应该对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);

}

}

}

这是操作效果 .

6315e4d1-fe28-4538-b3e5-c1dc2bb62bba.png

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值