具体代码如下:
import javax.swing.*
import java.awt.event.*
import java.awt.*
public class Calculator extends JFrame implements ActionListener {
private JFrame jf
private JButton[] allButtons
private JButton clearButton
private JTextField jtf
public Calculator() {
//对槐纳乱图形组件实例化
jf=new JFrame("任静的计算器1.0:JAVA版")
jf.addWindowListener(new WindowAdapter(){
public void windowClosing(){
System.exit(0)
}
})
allButtons=new JButton[16]
clearButton=new JButton("清除")
jtf=new JTextField(25)
jtf.setEditable(false)
String str="123+456-789*0.=/"
for(int i=0i<allButtons.lengthi++){
allButtons[i]=new JButton(str.substring(i,i+1))
}
}
public void init(){
//完成布局
jf.setLayout(new BorderLayout())
JPanel northPanel=new JPanel()
JPanel centerPanel=new JPanel()
JPanel southPanel=new JPanel()
northPanel.setLayout(new FlowLayout())
centerPanel.setLayout(new GridLayout(4,4))
southPanel.setLayout(new FlowLayout())
northPanel.add(jtf)
for(int i=0i<16i++){
centerPanel.add(allButtons[i])
}
southPanel.add(clearButton)
jf.add(northPanel,BorderLayout.NORTH)
jf.add(centerPanel,BorderLayout.CENTER)
jf.add(southPanel,BorderLayout.SOUTH)
addEventHandler()
}
//添加事件监听
public void addEventHandler(){
jtf.addActionListener(this)
for(int i=0i<allButtons.lengthi++){
allButtons[i].addActionListener(this)
}
clearButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Calculator.this.jtf.setText("")
}
})
}
//事件处理
public void actionPerformed(ActionEvent e) {
//在这里完成事件处理 使计算器可以运行
String action=e.getActionCommand()
if(action=="+"||action=="-"||action=="*"||action=="铅档/"){
}
}
public void setFontAndColor(){
Font f=new Font("宋茄斗体",Font.BOLD,24)
jtf.setFont(f)
jtf.setBackground(new Color(0x8f,0xa0,0xfb))
for(int i=0i<16i++){
allButtons[i].setFont(f)
allButtons[i].setForeground(Color.RED)
}
}
public void showMe(){
init()
setFontAndColor()
jf.pack()
jf.setVisible(true)
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
}
public static void main(String[] args){
new Calculator().showMe()
}
}
1、中尘首先准备好软件即eclipse和java,下载安装完成后打开eclipse。
2、点击左上角的file 新建一个project。
3、给project取一个名字,其他的选项都是默认然后点击finish。
4、接下来是新建一个class。
5、在给class取名字的时候注意用英文名亏培猛的首字母要大写。完成后点击finish。
6、这就是开销桥始写代码的工作台,将代码写在绿字下。
7、这就是第一个hello world程序。
package calcimport java.awt.Container
import java.awt.FlowLayout
import java.awt.GridLayout
import java.awt.event.ActionEvent
import java.awt.event.ActionListener
import java.awt.event.FocusEvent
import java.awt.event.FocusListener
import javax.swing.ButtonGroup
import javax.swing.JButton
import javax.swing.JFrame
import javax.swing.JLabel
import javax.swing.JOptionPane
import javax.swing.JPanel
import javax.swing.JRadioButton
import javax.swing.JTextField
public class Frame extends JFrame{
private JLabel label1 = new JLabel("请输入第一个整数")
private JTextField textField1 = new JTextField("", 10)
private JLabel label2 = new JLabel("请输入第二个整数")
private JTextField textField2 = new JTextField("", 10)
private JRadioButton radioButton1 = new JRadioButton("加法")
private JRadioButton radioButton2 = new JRadioButton("减法")
private JRadioButton radioButton3 = new JRadioButton("乘法")
private JRadioButton radioButton4 = new JRadioButton("除法")
ButtonGroup group = new ButtonGroup()
private JLabel label3= new JLabel("此结果为")
private JTextField textField3 = new JTextField("",10)
private JButton button = new JButton("计算")
public Frame() {
this.setTitle("简单计算器")
this.setBounds(100, 100, 440, 300)
this.setResizable(false)
Container c = this.getContentPane()
//网格布局(5行1列)
JPanel p0 = new JPanel(new GridLayout(5, 1))
//每行中使用流式布局
JPanel p1 笑档= new JPanel(new FlowLayout())
JPanel p2 = new JPanel(new FlowLayout())
JPanel p3 = new JPanel(new FlowLayout())
JPanel p4 = new JPanel(new FlowLayout())
JPanel p5 = 碰高乱new JPanel(new FlowLayout())
//将每行的布局面板放念含置在网格布局的面板p0上
p0.add(p1)p0.add(p2)p0.add(p3)p0.add(p4)p0.add(p5)
//将单选按钮组件编组
group.add(radioButton1)group.add(radioButton2)group.add(radioButton3)group.add(radioButton4)
p1.add(label1)
p1.add(textField1)
p2.add(label2)
p2.add(textField2)
p3.add(radioButton1)
//设置默认选中
radioButton1.setSelected(true)
p3.add(radioButton2)
p3.add(radioButton3)
p3.add(radioButton4)
p4.add(label3)
p4.add(textField3)
p5.add(button)
c.add(p0)
//文本框动作监听事件//设置只能输入数字
textField1.addFocusListener(new FocusListener() {
@Override
public void focusLost(FocusEvent e) {
String str1 = textField1.getText().trim()
if (!str1.matches("[1-9][0-9]*")) {
JOptionPane.showMessageDialog(null, "请输入一个整数!")
textField1.setText("")
textField1.requestFocus()
}
}
@Override
public void focusGained(FocusEvent e) { }
})
textField2.addFocusListener(new FocusListener() {
@Override
public void focusLost(FocusEvent e) {
String str2 = textField2.getText().trim()
if (!str2.matches("[1-9][0-9]*")) {
JOptionPane.showMessageDialog(null, "请输入一个整数!")
textField2.setText("")
textField2.requestFocus()
}
}
@Override
public void focusGained(FocusEvent e) { }
})
//按钮动作监听事件
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (radioButton1.isSelected()) {//选择加法
textField3.setText(Integer.toString(Integer.parseInt(textField1.getText()) + Integer.parseInt(textField2.getText())))
}else if (radioButton2.isSelected()) {//选择减法
textField3.setText(Integer.toString(Integer.parseInt(textField1.getText()) - Integer.parseInt(textField2.getText())))
}else if (radioButton3.isSelected()) {//选择乘法
textField3.setText(Integer.toString(Integer.parseInt(textField1.getText()) * Integer.parseInt(textField2.getText())))
}else {//选择除法
textField3.setText(Integer.toString(Integer.parseInt(textField1.getText()) / Integer.parseInt(textField2.getText())))
}
}
})
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
this.setVisible(true)
}
public static void main(String[] args) {
new Frame()
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)