java组件运用案例.docx_第1页
java组件运用案例.docx_第2页
java组件运用案例.docx_第3页
java组件运用案例.docx_第4页
java组件运用案例.docx_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

组合框运用实例import java.awt.*;import java.awt.event.*;import javax.swing.*;public class IntBinaryJFrame extends JFrame implements ActionListener private JTextField texts; /操作数和运算结果 private JComboBox combox; /算术运算符 public IntBinaryJFrame() super(十进制算术运算的竖式及二进制显示); this.setSize(280,220); this.setResizable(true); Dimension dim=getToolkit().getScreenSize(); /获得屏幕分辨率 this.setLocation(dim.width-this.getWidth()/2, (dim.height-this.getHeight()/2); /窗口居中 this.setBackground(Color.lightGray); this.setDefaultCloseOperation(EXIT_ON_CLOSE); /结束主进程 int number=3; /设置行数为3行 this.getContentPane().setLayout(new GridLayout(number,1); texts = new JTextFieldnumber*2; Object operators=+, -; for (int i=0; inumber; i+) JPanel panel=new JPanel(new FlowLayout(FlowLayout.RIGHT); this.getContentPane().add(panel); if (i=1) combox = new JComboBox(operators); /组合框,默认不可编辑 panel.add(combox); if (i=number-1) JButton button = new JButton(=); panel.add(button); button.addActionListener(this); /注册单击事件监听器 textsi = new JTextField(0,5); textsi.setHorizontalAlignment(SwingConstants.RIGHT); /文本行右对齐 panel.add(textsi); textsi+number = new JTextField(0,8); textsi+number.setHorizontalAlignment(SwingConstants.RIGHT); textsi+number.setEditable(false); panel.add(textsi+number); textsnumber-1.setEditable(false); /只能显示,不允许编辑 this.setVisible(true); public void actionPerformed(ActionEvent e) /按钮单击事件处理方法 try int x=Integer.parseInt(texts0.getText(),10); String a=Integer.toBinaryString(x); int y=Integer.parseInt(texts1.getText(),10); String b=Integer.toBinaryString(y); texts3.setText(a); texts4.setText(b); switch (combox.getSelectedIndex() case 0: x+=y; break; case 1: x-=y; break; texts2.setText(x+); texts5.setText(Integer.toString(x, 2); catch(NumberFormatException nfe) JOptionPane.showMessageDialog(this,字符串不能转换成整数,请重新输入!); finally public static void main(String arg) new IntBinaryJFrame(); 计算器运用实例import java.awt.*;import javax.swing.*;import java.awt.event.*;public class jisuanqi extends JFrame implements ActionListener private JTextField text_math;private JPanel top;private JPanel calckeysPanel;private final String keys = sqrt, +/-, CE, C, 7, 8, 9, /, 4, 5, 6,*, 1, 2, 3, -, 0, ., =, + ;private JButton k = new JButtonkeys.length;private boolean first = true; private double re_num = 0; private String operator = =; private boolean operatev = true;public jisuanqi()super(计算器);this.init();this.setBounds(300, 200, 300, 300);this.setBackground(java.awt.Color.lightGray);this.setDefaultCloseOperation(EXIT_ON_CLOSE);this.setVisible(true);this.pack();private void init() text_math=new JTextField(0);text_math.setHorizontalAlignment(JTextField.RIGHT);text_math.setBackground(Color.WHITE);top = new JPanel(); top.setLayout(new BorderLayout(); top.add(Center, text_math);calckeysPanel = new JPanel(); calckeysPanel.setLayout(new GridLayout(5, 4, 3, 3); for (int i = 0; i keys.length; i+) ki = new JButton(keysi); calckeysPanel.add(ki); getContentPane().setLayout(new BorderLayout(3, 5); getContentPane().add(North, top); getContentPane().add(Center, calckeysPanel); for (int i = 0; i = 0) handlenum(label); else handleOperator(label); private void handlenum(String k) if (first) text_math.setText(k); else if (k.equals(.) & (text_math.getText().indexOf(.) 0) text_math.setText(text_math.getText() + .); else if (!k.equals(.) text_math.setText(text_math.getText() + k); first = false; private void handleC() text_math.setText(0); first = true; operator = =; private void handleOperator(String k) if (operator.equals(/) / 除法运算 / 如果当前结果文本框中的值等于0 if (getNumberFromText() = 0) / 操作不合法 operatev = false; text_math.setText(除数不能为零); else re_num /= getNumberFromText(); else if (operator.equals(+) / 加法运算 re_num += getNumberFromText(); else if (operator.equals(-) / 减法运算 re_num -= getNumberFromText(); else if (operator.equals(*) / 乘法运算 re_num *= getNumberFromText(); else if (operator.equals(sqrt) / 平方根运算 re_num = Math.sqrt(re_num); else if (operator.equals(+/-) / 正数负数运算 re_num = re_num * (-1); else if (operator.equals(=) / 赋值运算 re_num = getNumberFromText(); if (operatev) / 双精度浮点数的运算 long t1; double t2; t1 = (long) re_num; t2 = re_num - t1; if (t2 = 0) text_math.setText(String.valueOf(t1); else text_math.setText(String.valueOf(re_num); / 运算符等于用户按的按钮 operator = k; first = true; operatev = true; private double getNumberFromText() double result = 0; try result = Double.valueOf(text_math.getText().doubleValue(); catch (NumberFormatException e) return result; 表格运用实例import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.table.DefaultTableModel;public class FangZheng extends JFrame implements ActionListenerprivate JTextField textn;private JButton jb1,jb2;private DefaultTableModel tablemodel;public FangZheng()super(循环移位方阵);Dimension dim=getToolkit().getScreenSize(); this.setBounds(dim.width/4,dim.height/4,dim.width/2,dim.height/2); this.setBackground(java.awt.Color.lightGray); this.setDefaultCloseOperation(EXIT_ON_CLOSE); JPanel panel=new JPanel(); this.getContentPane().add(panel,North); panel.add(new JLabel(随机个数(范围1-10):); textn=new JTextField(5,5); panel.add(textn); jb1=new JButton(确定); panel.add(jb1); jb1.addActionListener(this); String lk= , ; for(int i=0;ilk.length;i+) panel.add(new JLabel(lki); panel.add(new JLabel(循环移位); jb2=new JButton(开始); jb2.addActionListener(this); panel.add(jb2); String titles=new String10; for(int i=0;ititles.length;i+) titlesi=(i+1)+; this.tablemodel=new DefaultTableModel(titles,10); JTable table=new JTable(this.tablemodel); this.getContentPane().add(new JScrollPane(table),Center); this.setVisible(true);public void actionPerformed(ActionEvent e) if(e.getSource()=jb1)tryint n=Integer.parseInt(this.textn.getText();for(int i=0;i10;i+)for(int j=0;j10;j+)this.tablemodel.setValueAt(null, i, j);random(n);catch(NumberFormatException nfex) JOptionPane.showMessageDialog(this, 无法转换成整数, 提醒, JOptionPane.WARNING_MESSAGE); if(e.getSource()=jb2)int n=Integer.parseInt(this.textn.getText();for(int i=1;in;i+)for(int j=0;jn-1;j+)this.tablemodel.setValueAt(this.tablemodel.getValueAt(i-1, j), i, j+1);this.tablemodel.setValueAt(this.tablemodel.getValueAt(i-1, n-1), i, 0);private void random(int n) for(int j=0;j10;j+)if(jn)this.tablemodel.setValueAt(int)(Math.random()*100)+, 0, j);else this.tablemodel.setValueAt(null, 0, j);public static void main(String args) new FangZheng();画布运用实例import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ArchimedesJFrame extends JFrame implements ActionListener private ArchimedesCanvas archimedes; private JButton jb3;private JPanel jp1;private JTextField text_q;private JRadioButton bcolor;private double n; public ArchimedesJFrame() super(阿基米德螺线); Dimension dim=getToolkit().getScreenSize(); this.setBounds(dim.width/4,dim.height/4,dim.width/2,dim.height/2); this.setDefaultCloseOperation(EXIT_ON_CLOSE); text_q=new JTextField(10); jp1=new JPanel(); jp1.add(new JLabel(圈数:); jp1.add(text_q); jb3=new JButton(确定); jp1.add(jb3); jb3.addActionListener(this); String lk= , ; for(int i=0;ilk.length;i+) jp1.add(new JLabel(lki); jp1.add(new JLabel(颜色:); String colorstr=红,绿,蓝; ButtonGroup colorgroup=new ButtonGroup(); bcolor=new JRadioButtoncolorstr.length; for(int i=0;ibcolor.length;i+) bcolori=new JRadioButton(colorstri); colorgroup.add(bcolori); jp1.add(bcolori); bcolori.addActionListener(this); this.getContentPane().add(jp1,North); archimedes=new ArchimedesCanvas(Color.black,this.n); this.getContentPane().add(new JScrollPane(archimedes),Center); this.setVisible(true); public void actionPerformed(ActionEvent e) Color c=null; if(e.getSource()=bcolor0) c=new Color(255,0,0); if(e.getSource()=bcolor1) c=new Color(0,255,0); if(e.getSource()=bcolor2) c=new Color(0,0,255); if(e.getSource()=jb3) try this.n=Double.parseDouble(text_q.getText(); catch(NumberFormatException nfex) JOptionPane.showMessageDialog(this, 无法转换成浮点数, 提醒, JOptionPane.WARNING_MESSAGE); try if(n20) throw new Exception( ); archimedes.setn(n); archimede

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论