由于布局会使组件的大小发生改变所以建议使用NoneLayout
也就是使用组件钢性,比如下面的代码,
//NonLayoutDemo.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class NonLayoutDemo {
public NonLayoutDemo() {
JFrame f = new JFrame();
Container contentPane = f.getContentPane();
contentPane.setLayout(null);//这里把布局改变成NONE
JButton b1 = new JButton("first");
contentPane.add(b1);
JButton b2 = new JButton("second");
contentPane.add(b2); b1.setBounds(15, 10, 80, 30);//这里设组件的大小,和在组件上的坐标
b2.setBounds(80, 50, 90, 40);//这里设组件的大小,和在组件上的坐标 f.setTitle("NoLayout");
f.setSize(200,130);
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
} public static void main(String args[]) {
NonLayoutDemo b = new NonLayoutDemo();
}
}
也就是使用组件钢性,比如下面的代码,
//NonLayoutDemo.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class NonLayoutDemo {
public NonLayoutDemo() {
JFrame f = new JFrame();
Container contentPane = f.getContentPane();
contentPane.setLayout(null);//这里把布局改变成NONE
JButton b1 = new JButton("first");
contentPane.add(b1);
JButton b2 = new JButton("second");
contentPane.add(b2); b1.setBounds(15, 10, 80, 30);//这里设组件的大小,和在组件上的坐标
b2.setBounds(80, 50, 90, 40);//这里设组件的大小,和在组件上的坐标 f.setTitle("NoLayout");
f.setSize(200,130);
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
} public static void main(String args[]) {
NonLayoutDemo b = new NonLayoutDemo();
}
}
解决方案 »
- 刚开始学Java,遇到一些问题,可能比较傻
- 我的第一个程序问题啊,高手帮忙啊
- java连接oracle的问题
- 【新手提问】为什么我有的类不能窗口居中,有的又能,有的能用windows装饰,有的也不能!!狂晕.高手救命
- 外行求助--大型机
- java在windows和sorlaris下的不同表现~~,帮忙看看
- 关于java网络编程问题,用java写了个服务端,用delphi写了个客户端.....?
- 高手们,help me--->>java digital canculator
- ==菜鸟问题:如何运行Java程序==
- 我想学关于WEB开发方面,想请问一下应该学java的哪方面>???????
- 高分请教:在应用中如何嵌入浏览器功能
- 用java.exe执行一个xxx.class文件后出现如下错误是怎么回事?
Dimension windowSize = getPreferredSize();
// Put image at the middle of the screen
setLocation(screenSize.width/2 - (windowSize.width/2),
screenSize.height/2 - (windowSize.height/2));