java组件练习之复数计算器和下拉框联动
Posted 溪风吹雨
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java组件练习之复数计算器和下拉框联动相关的知识,希望对你有一定的参考价值。
package test1; public class test1 { public static void main(String[] args) { Window JF=new Window(); } } package test1; import java.awt.*; import java.awt.event.*; import javax.swing.*; class Window extends JFrame { JLabel number1; JLabel number1T; JLabel number1_; JLabel number2; JLabel number2T; JLabel number2_; JTextField virtual1_text; JTextField true1_text; JTextField true2_text; JTextField virtual2_text; JButton addButton; //加法+ JButton subtractButton; //减法- JButton multiplyButton; //乘法* JButton divideButton; //除法/ JLabel showAnswer; JTextArea textShow; ComputerListener computer; public Window() { init(); setVisible(true); setDefaultCloseOperation(DISPOSE_ON_CLOSE); } void init() { setLayout(new FlowLayout()); setTitle("复数计算器"); number1=new JLabel("复数1:"); number1T=new JLabel("实部:"); true1_text=new JTextField(18); number1_=new JLabel("虚部:"); virtual1_text=new JTextField(18); number2=new JLabel("复数2:"); number2T=new JLabel("实部:"); true2_text=new JTextField(18); number2_=new JLabel("虚部:"); virtual2_text=new JTextField(18); addButton=new JButton("+"); subtractButton=new JButton("-"); multiplyButton=new JButton("*"); divideButton=new JButton("/"); showAnswer=new JLabel("结果"); textShow=new JTextArea(5,20); computer=new ComputerListener(); computer.settrue1(true1_text); computer.setvirtual1(virtual1_text); computer.settrue2(true2_text); computer.setvirtual2(virtual2_text); computer.setJTextArea(textShow); addButton.addActionListener(computer); subtractButton.addActionListener(computer); multiplyButton.addActionListener(computer); divideButton.addActionListener(computer); setBounds(200,200,250,400); add(number1);//添加组件 add(number1T); add(true1_text); add(number1_); add(virtual1_text); add(number2); add(number2T); add(true2_text); add(number2_); add(virtual2_text); add(addButton); add(subtractButton); add(multiplyButton); add(divideButton); add(showAnswer); add(textShow); } } import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ComputerListener implements ActionListener{ JTextField number1_true;//实部 JTextField number1_virtual;//虚部 JTextField number2_true; JTextField number2_virtual; JTextArea textShow; public void settrue1(JTextField t) { number1_true=t;//this.xx = xx; } public void setvirtual1(JTextField t) { number1_virtual=t; } public void settrue2(JTextField t) { number2_true=t; } public void setvirtual2(JTextField t) { number2_virtual=t; } public void setJTextArea(JTextArea t) { textShow=t; } public void actionPerformed(ActionEvent e) { try { double result_true=0; double result_virtual=0; //getTEXT获取字符串形式 double true1=Double.parseDouble(number1_true.getText()); double virtual1=Double.parseDouble(number1_virtual.getText()); double true2=Double.parseDouble(number2_true.getText()); double virtual2=Double.parseDouble(number2_virtual.getText()); if(e.getActionCommand().equals("+")) { result_true=true1+true2; result_virtual=virtual1+virtual2; } if(e.getActionCommand().equals("-")) { result_true=true1-true2; result_virtual=virtual1-virtual2; } if(e.getActionCommand().equals("*")) { result_true=(true1*true2-virtual1*virtual2); result_virtual=(true1*virtual2+true2*virtual1); } if(e.getActionCommand().equals("/")) { result_true=1.0*(true1*true2+virtual1*virtual2)/(true2*true2-virtual2*virtual2); result_virtual=1.0*(virtual1*true2-true1*virtual2)/(true2*true2-virtual2*virtual2); } textShow.append(result_true+"+"+result_virtual+"i"+"\\n"); } catch(Exception ex) {//防止文本框为空,指针null textShow.append("\\n请重新输入数字\\n"); } } }
package test2; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import javax.swing.ButtonGroup; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JRadioButton; import javax.swing.JTextArea; import javax.swing.JTextField; public class test2 { public static void main(String [] args){ window win= new window(); win.setVisible(true); //win.setBounds(200, 200, 200, 200); win.setTitle("name"); } } class window extends JFrame implements ItemListener,ActionListener{ //各个组件 JLabel name; JTextField name_text; JLabel gender; ButtonGroup gender_bg;//性别单选组 JRadioButton man; JRadioButton woman; JLabel age; ButtonGroup age_bg;//年龄选择组 JRadioButton age1; JRadioButton age2; JRadioButton age3; JRadioButton age4; //年龄选项 JLabel address; //地址 JLabel province_name;//省名字 JLabel city_name;//市名字 JLabel area_name;//区名字 JComboBox Province; JComboBox City; JComboBox Area;//下拉列表 JTextField specific_address;//具体地址输入框 JLabel hobby;//爱好 JCheckBox read_hobby;//爱好选项 JCheckBox music_hobby; JCheckBox gane_hobby; JCheckBox ball_hobby; JCheckBox dance_hobby; JCheckBox sreech_hobby; JLabel prize;//获奖 JTextArea prize_text;//文本区 JButton apply; //提交按钮 //组件完毕 public window() { setBounds(500,500,300,400); init(); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } private void init(){//初始化 //第一行 name=new JLabel("姓名:"); name_text=new JTextField(10); gender=new JLabel(" 性别:"); man=new JRadioButton("男 "); woman=new JRadioButton("女 "); gender_bg=new ButtonGroup(); gender_bg.add(man); gender_bg.add(woman);//合并到一起 //第二行 age=new JLabel(" 年龄: "); age1=new JRadioButton(" 18岁以下 "); age2=new JRadioButton(" 18-35岁 "); age3=new JRadioButton(" 36-59岁 "); age4=new JRadioButton(" 60岁以上 "); age_bg=new ButtonGroup(); age_bg.add(age1); age_bg.add(age2); age_bg.add(age3); age_bg.add(age4); //地址 address=new JLabel(" 地址:"); //省市县的标签 province_name=new JLabel("省份:"); city_name=new JLabel("市:"); /*废弃 * String [] province={"安徽省","山东省","浙江省"}; * //三个对应的市 String[][] cities = newString[][]{{"海淀区","崇文区","西城区"},{"长沙市","株洲市","湘潭市"},{"广州市","深圳市","珠海市"}}; * cmbState=new JComboBox(province); * cmbCity=new JComboBox(cities[0]); * cmbState.addItemListener(this); */ //爱好区 hobby=new JLabel(" 爱好:"); read_hobby=new JCheckBox("阅读 "); music_hobby=new JCheckBox("音乐 "); gane_hobby=new JCheckBox("玩游戏 "); ball_hobby=new JCheckBox("羽毛球 "); dance_hobby=new JCheckBox("跳舞 " ); sreech_hobby=new JCheckBox("演讲 " ); //获奖区 //specific_address=new JTextField(30); prize=new JLabel(" 曾获奖项: "); prize_text=new JTextArea("",4,18); //提交 apply=new JButton("提交"); apply.addActionListener(this);//提交按钮设置监视器事件处理 // System.out.println("?????"); //添加组件 //第一行 add(name); add(name_text); add(gender); add(man); add(woman); //第二行 add(age); add(age1); add(age2); add(age3); add(age4); //第三行 Province = new JComboBox(); Province.addItem("请选择省份 "); Province.addItem("陕西省"); Province.addItem("安徽省"); //Province.setSize(120, 30); //Province.setLocation(30, 30); Province.addItemListener(this); add(Province); City = new JComboBox(); City.addItem("请选择城市"); City.addItem(" "); //City.setSize(120, 30); //City.setLocation(30, 80); add(City); Area = new JComboBox(); //Area.setSize(120, 30); //Area.setLocation(30, 130); Area.addItem("请选择县区"); add(Area); Area.addItem(" "); //add(specific_address); //爱好 第四行 add(hobby); add(read_hobby); add(music_hobby); add(gane_hobby); add(ball_hobby); add(dance_hobby); add(sreech_hobby); //获奖区 第五行 add(prize); add(prize_text); //提交按钮 add(apply); setVisible(true); setLayout(new FlowLayout()); // setSize(400,550); //setResizable(false); } public void itemStateChanged(ItemEvent e) { String temp = ((JComboBox) e.getSource()).getSelectedItem().toString(); if (temp.equals("陕西省")) { City.removeItemListener(this); City.removeAllItems(); City.addItemListener(this); String[] city = { "西安市", "咸阳市", }; for (int i = 0; i < city.length; i++) { City.addItem(city[i]); } } if (temp.equals("安徽省")) { City.removeItemListener(this); City.removeAllItems(); City.addItemListener(this); String[] city = { "合肥市", "黄山市"}; for (int i = 0; i < city.length; i++) { City.addItem(city[i]); } } if (temp.equals("西安市")) { Area.removeItemListener(this); Area.removeAllItems(); Area.addItemListener(this); String[] area = { "未央区", "雁塔区", "新城区" }; //动态数组存储地名 for (int i = 0; i < area.length; i++) { Area.addItem(area[i]); } } if (temp.equals("咸阳市")) { Area.removeItemListener(this); Area.removeAllItems(); Area.addItemListener(this); Area.removeAllItems(); String[] area = { "秦都区", "渭滨区", "长武县", "武功县", "泾阳县" }; for (int i = 0; i < area.length; i++) { Area.addItem(area[i]); } } if (temp.equals("合肥市")) { Area.removeItemListener(this); Area.removeAllItems(); Area.addItemListener(this); String[] area = { "庐阳区", "蜀山区" }; for (int i = 0; i < area.length; i++) { Area.addItem(area[i]); } } if (temp.equals("黄山市")) { Area.removeItemListener(this); Area.removeAllItems(); Area.addItemListener(this); Area.removeAllItems(); String[] area = { "徽州区", "黄山区" }; for (int i = 0; i < area.length; i++) { Area.addItem(area[i]); } } if (temp.equals(" ")) { Area.removeItemListener(this); Area.removeAllItems(); Area.addItem("请选择市区"); City.removeItemListener(this); City.removeAllItems(); City.addItem("请选择省份"); } } public void actionPerformed(ActionEvent e) { //输出信息拼接字符串 String message="姓名:"; message+=name.getText().toString(); message+=" 性别:"; if (man.isSelected()){ message+="男"; }else { if (woman.isSelected()){ message+="女"; } } //年龄 message+=" 年龄:"; if (age1.isSelected()){ message+="18岁以下"; }else if (age2.isSelected()){ message+="19-35岁"; }else if (age3.isSelected()){ message+="36-59岁"; }else if (age4.isSelected()) { message += "60岁以上"; } //地址 message+="地址 "; message+=Province.getSelectedItem(); message+=City.getSelectedItem(); message+=Area.getSelectedItem(); message+=" 具体位置"; message+=specific_address.getText(); //爱好 message+=" 爱好"; if (read_hobby.isSelected()){ message+="阅读 "; } if (music_hobby.isSelected()){ message+="音乐 "; } if (gane_hobby.isSelected()){ message+="玩游戏 "; } if (ball_hobby.isSelected()){ message+="羽毛球 "; } if (dance_hobby.isSelected()){ message+="跳舞 " ; } if (sreech_hobby.isSelected()){ message+="演讲 "; } //奖项 if(prize.getText()!=""){ message+="曾获奖项 "; message+=prize.getText(); } JOptionPane.showConfirmDialog(this,message); } }
示意:
(联动方法较为笨拙,更好的方法还在学习)
以上是关于java组件练习之复数计算器和下拉框联动的主要内容,如果未能解决你的问题,请参考以下文章