随便写了一些,练练SWING界面

发布时间:2016-12-9 0:10:24 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"随便写了一些,练练SWING界面",主要涉及到随便写了一些,练练SWING界面方面的内容,对于随便写了一些,练练SWING界面感兴趣的同学可以参考一下。

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); } }

上一篇:C++封装成C Makefile
下一篇:分组背包--hdu1712

相关文章

相关评论