为什么得到的按钮总是充满整个框架,怎样才能调整到合适的位置?代码如下:
package Swing;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class YesNoDemo extends JFrame 
{
JButton button=null;

public YesNoDemo()
{
try{
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
}catch(Exception e){}
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
button=new JButton("Click me");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int resurt=JOptionPane.showConfirmDialog(null,"Exit this program now?","please answer",JOptionPane.YES_NO_OPTION);
if(resurt==JOptionPane.YES_OPTION)
System.exit(0);
}
});


Container contentPane=getContentPane();
contentPane.add(button);

}


public static void main(String[] args) {
YesNoDemo app=new YesNoDemo();
app.setSize(320,240);
app.show();

}
}

解决方案 »

  1.   

    JFrame默认布局的问题,
    你去看下Java的几种布局就OK啦,或者你直接定义Button的大小,显示位置也可以
      

  2.   

    contentPane.add(button,BorderLayout.NORTH);
    就会在顶部的了!!
      

  3.   

    你这个程序可以简单地使用FlowLayout来做: setLayout(new FlowLayout()); 因为你只需要加一个按钮.如果是复杂一点的情况, 可以使用BorderLayout, 在它的各个区块中先添加JPanel之类的容器, 之后再添加你需要的组件.要是再复杂一点呢, 建议使用GridBagLayout最后, 要不然就使用界面编辑好了, 比如JBuilder或者给Eclipse加个插件, netBeans也可以.