java gui 记_java gui笔记

import java.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;import javax.swing.*;public classMain{

JButton setButtons(String name)

{

JButton tButton= newJButton();

tButton.setFont(new Font("consolas", Font.PLAIN, 20));

tButton.setBackground(Color.white);

tButton.setText(name);returntButton;

}voidcreateFrame(String title)

{

JFrame jf= new JFrame(title); //创建Frame//jf.setLayout(new GridLayout(4,4,2,2));

Container container = jf.getContentPane(); //获得容器//JLabel jl = new JLabel("this is a pane");

JTextField jt = new JTextField("0"); //创建文本框,设置框内默认值

jt.setEditable(false); //设置不可编辑

jt.setHorizontalAlignment(JTextField.RIGHT); //设置文本框的对齐方式

jt.setPreferredSize(new Dimension (300,80)); //设置文本框的高度和宽度

jt.setFont(new Font("consolas", Font.PLAIN, 18)); //设置文本框的字体信息

container.add(jt, BorderLayout.NORTH); //将文本框加入到容器的北部

JPanel buttonPannel = newJPanel();

buttonPannel.setLayout(new GridLayout(4,4,2,2)); //Grid 布局方式。

for(int i = 1; i < 4; i++)

{

JButton jb= setButtons(""+i);

buttonPannel.add(jb);

jb.addActionListener(newActionListener() {

String s=jb.getText();

@Overridepublic voidactionPerformed(ActionEvent e) {

jt.setText(jt.getText()+s);

}

});

}

buttonPannel.add(setButtons("+"));for(int i = 4; i < 7; i++)

{

JButton jb= setButtons(""+i);

buttonPannel.add(jb);

jb.addActionListener(newActionListener() {

String s=jb.getText();

@Overridepublic voidactionPerformed(ActionEvent e) {

jt.setText(jt.getText()+s);

}

});

}

buttonPannel.add(setButtons("-"));for(int i = 7; i < 10; i++)

{

JButton jb= setButtons(""+i);

buttonPannel.add(jb);

jb.addActionListener(newActionListener() {

String s=jb.getText();

@Overridepublic voidactionPerformed(ActionEvent e) {

jt.setText(jt.getText()+s);

}

});

}

buttonPannel.add(setButtons("*"));

JButton CE= setButtons("CE");

CE.addActionListener(newActionListener() {

@Overridepublic voidactionPerformed(ActionEvent e) {

jt.setText("0");

}

});

buttonPannel.add(CE);

JButton zero= setButtons("0");

zero.addActionListener(newActionListener() {

@Overridepublic voidactionPerformed(ActionEvent e) {

jt.setText(jt.getText()+"0");

}

});

buttonPannel.add(zero);

buttonPannel.add(setButtons("="));

buttonPannel.add(setButtons("/"));

container.add(buttonPannel);//jl.setHorizontalAlignment(SwingConstants.CENTER);//container.add(jl);

container.setBackground(Color.white);

jf.setVisible(true);

jf.setSize(600,400);

jf.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

}public static voidmain(String[] args) {new Main().createFrame("not the first");

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值