j_combobox.java
来自「可进行整型和浮点型的四则运算」· Java 代码 · 共 113 行
JAVA
113 行
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class J_ComboBox extends JFrame {
private JTextField[] m_textField= {
new JTextField(10),new JTextField(10),new JTextField(10)
};
private JButton m_button=new JButton("=");
private String m_items[]={"+","-","*","/"};
private JComboBox m_comboBox=new JComboBox(m_items);
private JRadioButton[] m_radio={new JRadioButton("整数运算"),new JRadioButton("浮点运算")};
public J_ComboBox() {
super("JComboBox");
Container container=getContentPane();
container.setLayout(new FlowLayout());
container.add(m_textField[0]);
//container.add(m_button[0]);
container.add(m_comboBox);
container.add(m_textField[1]);
container.add(m_button);
container.add(m_textField[2]);
container.add(m_radio[0]);
container.add(m_radio[1]);
J_Handler handler=new J_Handler();
m_textField[0].addActionListener(handler);
m_textField[1].addActionListener(handler);
m_textField[2].addActionListener(handler);
m_button.addActionListener(handler);
m_comboBox.addActionListener(handler);
ButtonGroup radioGroup=new ButtonGroup();
radioGroup.add(m_radio[0]);
radioGroup.add(m_radio[1]);
m_radio[0].setSelected(true);
m_radio[1].setSelected(false);
setSize(500,150);
setVisible(true);
}
private class J_Handler implements ActionListener {
public void actionPerformed(ActionEvent event) {
String s1=m_textField[0].getText();
String s2=m_textField[1].getText();
if(m_radio[0].isSelected()) {
int a=Integer.parseInt(s1);
int b=Integer.parseInt(s2);
int c=0;
if(event.getSource()==m_button) {
if(m_comboBox.getSelectedIndex()==0)
c=a+b;
else if(m_comboBox.getSelectedIndex()==1)
c=a-b;
else if(m_comboBox.getSelectedIndex()==2)
c=a*b;
else if(m_comboBox.getSelectedIndex()==3)
c=a/b;
m_textField[2].setText(""+c);
}
}
else if(m_radio[1].isSelected()) {
float aa=Float.parseFloat(s1);
float bb=Float.parseFloat(s2);
float cc=0;
if(event.getSource()==m_button) {
if(m_comboBox.getSelectedIndex()==0)
cc=aa+bb;
else if(m_comboBox.getSelectedIndex()==1)
cc=aa-bb;
else if(m_comboBox.getSelectedIndex()==2)
cc=aa*bb;
else if(m_comboBox.getSelectedIndex()==3)
cc=aa/bb;
m_textField[2].setText(""+cc);
}
}
}
}
public static void main(String args[]) {
J_ComboBox application=new J_ComboBox();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?