package MyText2;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*;
import javax.swing.*;
class CreateIcon implements Icon
{
int Height;
int Width;
@Override
public int getIconHeight() {
// TODO 自动生成的方法存根
return Height;
}
@Override
public int getIconWidth() {
// TODO 自动生成的方法存根
return Width;
}
public CreateIcon(int Width, int Height)
{
this.Width = Width;
this.Height = Height;
}
@Override
public void paintIcon(Component arg0, Graphics arg1, int arg2, int arg3) {
// TODO 自动生成的方法存根
arg1.fillOval(arg2, arg3, Width, Height);
arg1.fillOval(arg2 + 70, arg3, Width, Height);
}
}
class CreateWriteDialog extends JDialog
{
public CreateWriteDialog(JFrame jframe)
{
super(jframe, "提示");
Container container = getContentPane();
container.setBackground(Color.white);
Icon icon = new CreateIcon(10, 10);
JLabel jlabel = new JLabel("保存成功", icon, SwingConstants.CENTER);
container.add(jlabel);
setVisible(true);
setBounds(450, 300, 200, 100);
}
}
class CreateReadDialog extends JDialog
{
public CreateReadDialog(JFrame jframe)
{
super(jframe, "提示");
Container container = getContentPane();
container.setBackground(Color.white);
Icon icon = new CreateIcon(10, 10);
JLabel jlabel = new JLabel("读取成功", icon, SwingConstants.CENTER);
container.add(jlabel);
setVisible(true);
setBounds(450, 300, 200, 100);
}
}
class CreateComboBox extends AbstractListModel implements ComboBoxModel
{
String str;
String data[] = {"A", "B", "C", "D"};
@Override
public Object getElementAt(int arg0) {
// TODO 自动生成的方法存根
return data[arg0];
}
@Override
public int getSize() {
// TODO 自动生成的方法存根
return data.length;
}
@Override
public Object getSelectedItem() {
// TODO 自动生成的方法存根
return str;
}
@Override
public void setSelectedItem(Object arg0) {
// TODO 自动生成的方法存根
str = (String)arg0;
}
}
public class MyText {
public static void main (String []args)
{
final JFrame jframe = new JFrame("界面");
Container container = jframe.getContentPane();
container.setBackground(Color.white);
container.setLayout(new BorderLayout());
/*************************************/
JPanel textpanel = new JPanel(new BorderLayout());
final JTextArea textarea = new JTextArea();
textarea.addFocusListener(new FocusListener()
{
public void focusLost(FocusEvent e)
{
JOptionPane.showMessageDialog(null, "别忘了保存文档哦");
}
public void focusGained(FocusEvent e)
{
}
});
jframe.addWindowListener(new WindowAdapter(){
public void windowOpened(WindowEvent e) {
textarea.requestFocus();
}
});
textarea.setLineWrap(true);
JScrollPane scrollpane = new JScrollPane(textarea);
textpanel.add(scrollpane);
container.add(BorderLayout.CENTER, textpanel);
/*************************************/
JPanel buttonpanel = new JPanel(new FlowLayout());
JLabel jlabel = new JLabel("选择选项");
JComboBox combobox = new JComboBox(new CreateComboBox());
JButton writebutton = new JButton("保存");
writebutton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
new CreateWriteDialog(jframe);
File file1 = new File("G:/helloworld");
try {
File file = new File(file1, "helloworld.txt");
if(!file.exists())
file.createNewFile();
FileWriter write = new FileWriter(file);
BufferedWriter in = new BufferedWriter(write);
String str = textarea.getText();
in.write(str);
in.close();
write.close();
} catch (IOException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
}
});
JButton readbutton = new JButton("读取");
readbutton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
new CreateReadDialog(jframe);
File file1 = new File("G:/helloworld");
try{
File file = new File(file1, "helloworld.txt");
FileReader read = new FileReader(file);
BufferedReader in = new BufferedReader(read);
String str = in.readLine();
textarea.setText(str);
read.close();
in.close();
}catch(IOException e1)
{
e1.printStackTrace();
}
}
});
buttonpanel.add(jlabel);
buttonpanel.add(combobox);
buttonpanel.add(writebutton);
buttonpanel.add(readbutton);
container.add(BorderLayout.SOUTH, buttonpanel);
/*************************************/
jframe.setVisible(true);
jframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jframe.setBounds(400, 250, 400, 300);
}
}
分享到:
相关推荐
左键创建小球,滚轮往下删除小球,最后可能把边界删掉,滚轮往上添加小球
NULL 博文链接:https://zc0604.iteye.com/blog/1291130
C#做的练练看
vue写的一个滴滴快车练练手
主要介绍了圣诞节,写个程序练练手————Android 全界面悬浮按钮实现的相关资料,需要的朋友可以参考下
有关java学习的懒人笔记
数据库附加上去即可用,就为能下载少分随便上传个
平时作业,Java实现连连看,适合于初学者Java编程的练习。
高仿练练开机数据录入
是一个VBA编写的游戏,有了VBA,EXCEL可以做很多事。
超详细的注释 ! 实现练练看 如有疑问加我 谢谢 支持!
使用oger做的练练看游戏,里面用到了CEGUI做界面,FMOD播放声音,OIS控制输入,免费给大家分享,有任何问题请邮件: xieaiwanli@hotmail.com
狂练
NirSeller外卖商家版,练练手,我也知道界面丑..
2016年中考证明专题练练.ppt
Cocos2d-x3.0正式版 练练看 源码。有注释。
acm题库及答案 初学者可以自己先练练,看看答案,还有一些相关acm题的知识,解压后是一本pdf书,我个人感觉蛮好的 美国计算机协会(Association for Computing Machinery , 简称ACM)是一个世界性的计算机从业员专业组织,...
网页设计大作业,简单练练手吧.zip