JAVA布局的设置问题
来源:百度知道 编辑:UC知道 时间:2024/05/11 05:50:52
import java.awt.*;
public class exp4 extends JFrame
{
JPanel CacuLation;
public exp4()
{
CacuLation=new JPanel();
CacuLation.setLayout(new FlowLayout());
caculation operation=new caculation();
this.setContentPane(CacuLation);
CacuLation.add(operation);
setTitle("计算器");
setSize(250,300);
setResizable(false);
setVisible(true);
setLocation(300,200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new exp4();
}
}
class caculation extends JPanel{
JTextField input;
JButton button,button1,button2,button3,button4,button5,
button6,button7,button8,button9;
public caculation ()
{
input=new JTextField(20);
button=new JButton("0");
button1=new JBut
FlowLayout的布局方式就是自左向右,自上而下,如果JPanel宽度足够,那么按钮就是在文本域右边了,你可以减小JPanel的宽度,就可以看到结果了。
/**
*修改了一下,看是否满足你的需求
*/
import javax.swing.*;
import java.awt.*;
public class exp4 extends JFrame
{
public exp4()
{
//CacuLation=new JPanel();
//CacuLation.setLayout(new FlowLayout());
TextPane textPane=new TextPane();
textPane.setLayout(new FlowLayout());
ButtonPane buttonPane=new ButtonPane();
buttonPane.setLayout(new GridLayout(4,3));
//caculation operation=new caculation();
//this.setContentPane(CacuLation);
//CacuLation.add(operation);
this.getContentPane().add(textPane,BorderLayout.NORTH);
this.getContentPane().add(buttonPane,BorderLayout.CENTER);
setTitle("计算器");
setSize(250,300);
setResizable(false);
setVisible(true);
setLocation(300,200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public