java中怎么在JFrame中任意位置添加一个按钮button_java – 如何将对象放在JFrame上的特定位置......

在这里找到Absolute Positioning Tutorials.请仔细阅读,为什么不鼓励使用LayoutManagers这种方法

要将JButton添加到JPanel,您可以使用:

JButton button = new JButton("Click Me");

button.setBounds(5, 5, 50, 30);

panel.add(button);

在这里尝试这个示例程序:

import java.awt.*;

import javax.swing.*;

public class AbsoluteLayoutExample

{

private void displayGUI()

{

JFrame frame = new JFrame("Absolute Layout Example");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel contentPane = new JPanel();

contentPane.setOpaque(true);

contentPane.setBackground(Color.WHITE);

contentPane.setLayout(null);

JLabel label = new JLabel(

"This JPanel uses Absolute Positioning"

, JLabel.CENTER);

label.setSize(300, 30);

label.setLocation(5, 5);

JButton button = new JButton("USELESS");

button.setSize(100, 30);

button.setLocation(95, 45);

contentPane.add(label);

contentPane.add(button);

frame.setContentPane(contentPane);

frame.setSize(310, 125);

frame.setLocationByPlatform(true);

frame.setVisible(true);

}

public static void main(String... args)

{

SwingUtilities.invokeLater(new Runnable()

{

public void run()

{

new AbsoluteLayoutExample().displayGUI();

}

});

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值