Hax*_*xed 20 java swing jframe jbutton
我想在JFrame中的特定坐标上放置一个Jbutton .我为JPanel(我放在JFrame上)设置了setBounds,并为JButton设置了setBounds.但是,它们似乎没有按预期运行.
我的输出:
替代文字http://i49.tinypic.com/2d8kuah.jpg
这是我的代码:
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Control extends JFrame {
// JPanel
JPanel pnlButton = new JPanel();
// Buttons
JButton btnAddFlight = new JButton("Add Flight");
public Control() {
// FlightInfo setbounds
btnAddFlight.setBounds(60, 400, 220, 30);
// JPanel bounds
pnlButton.setBounds(800, 800, 200, 100);
// Adding to JFrame
pnlButton.add(btnAddFlight);
add(pnlButton);
// JFrame properties
setSize(400, 400);
setBackground(Color.BLACK);
setTitle("Air Traffic Control");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new Control();
}
}
Run Code Online (Sandbox Code Playgroud)
如何放置JButtonat坐标(0,0)?
Eug*_*kov 23
在添加组件之前,应调用以下行
pnlButton.setLayout(null);
Run Code Online (Sandbox Code Playgroud)
上面将设置您的内容面板使用绝对布局.这意味着您必须始终使用setBounds方法显式设置组件的边界.
一般来说,我不建议使用绝对布局.
| 归档时间: |
|
| 查看次数: |
201781 次 |
| 最近记录: |