Java界面编程实战(二)

您所在的位置:网站首页 英文单词图案设计 Java界面编程实战(二)

Java界面编程实战(二)

2024-07-01 12:55| 来源: 网络整理| 查看: 265

目标图:

目标功能:

为了能和考驾照科目一的题库一样,可以把题目从题库中删除,看着题目一个个减少具有很大的成就感,特出此项目:

1.按“不认识”按钮显示汉字 2.按“认识”按钮把数据从“字典”删除 3.按“下一个”按钮显示下一个单词 4.可以点击保存按钮可以保存进度。 

界面分析:

首先有一个背景图,布局看样子是流动布局,因为流动布局默认把组件放中间,而且会在放不下的时候自动换行,2个文本框、4个按钮。

编程分析:

把单词当成一个个小的对象,利用ArrayList集合类把单词添加到“字典”类中。

在按钮上全部注册ActionListener监听,并编写相应的事件方法。

前面三个按钮的方法只要更改文本框的显示内容即可。

“保存”按钮需要把“字典”类中用PrintWriter输出流把未删除的单词重新写到文件中,实现保存进度的功能。

代码实现: import java.util.*; import java.io.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class dictionary2_0 extends JFrame implements ActionListener{ //定义组件 JPanel imagePanel; ImageIcon background; JButton jb1,jb2,jb3,jb4; JPanel jp1,jp2,jp3,jp4; JTextField jtf1,jtf2; Dictionary dictionary; JLabel label; int i; public static void main(String[] args) { dictionary2_0 a=new dictionary2_0(); } dictionary2_0() { this.createComponent(); // 内容窗格默认的布局管理器为BorderLayout imagePanel.setLayout(new FlowLayout()); imagePanel.add(jtf1); imagePanel.add(jtf2); imagePanel.add(jp2); jp2.add(jb1); jp2.add(jb2); jp2.add(jb3); jp2.add(jb4); this.getLayeredPane().setLayout(null); // 把背景图片添加到分层窗格的最底层作为背景 this.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE)); this.setTitle("Words"); this.setSize(background.getIconWidth(), background.getIconHeight()); this.setLocation(300, 300); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } public void createComponent() //创建组件的函数 { //添加组件 i=0; background=new ImageIcon("java.png"); // 设置背景图片 label = new JLabel(background);// 把背景图片显示在一个标签里面 // 把标签的大小位置设置为图片刚好填充整个面板 label.setBounds(0, 0, background.getIconWidth(),background.getIconHeight()); // 把内容窗格转化为JPanel,否则不能用方法setOpaque()来使内容窗格透明 imagePanel=(JPanel)this.getContentPane(); imagePanel.setOpaque(false); dictionary=new Dictionary(); jb1=new JButton("不认识"); jb1.addActionListener(this); jb1.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); //设置鼠标放上去变为小手 jb1.setIcon(new ImageIcon("不开心.png")); //按钮上添加图片 jb2=new JButton("认识"); jb2.addActionListener(this); jb2.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); jb2.setIcon(new ImageIcon("开心.png")); jb3=new JButton("下一个"); jb3.addActionListener(this); jb3.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); jb3.setIcon(new ImageIcon("一般.png")); jb4=new JButton("保存"); jb4.addActionListener(this); jb4.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); jp1=new JPanel(); jp1.setOpaque(false); jp2=new JPanel(); jp2.setOpaque(false); jp3=new JPanel(); jp3.setOpaque(false); jp4=new JPanel(); jp4.setOpaque(false); jtf1=new JTextField(30); jtf1.setEditable(false); //设置文本不可编辑 jtf1.setFont(new Font("黑体",Font.PLAIN,20)); //设置文本字体 jtf1.setText(dictionary.words.get(0).word); //设置文本域初始值 jtf2=new JTextField(30); //设置文本域宽度 jtf2.setEditable(false); jtf2.setFont(new Font("黑体",Font.PLAIN,20)); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(e.getSource()==jb1) //如果点击第一个按钮 { jtf2.setText(dictionary.words.get(i).character); //显示单词解释 } else if(e.getSource()==jb2) //如果点击第二个按钮 { if(i


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3