标签:
1 java中提供的类库
1.1 定义
1.2 java包
1.3 组件分类
java中构成用户图形界面的各个元素,统称为组件(Component)
Component又分为容器(Container)和非容器两大类
容器又分为顶层容器和非顶层容器
1.3.1 Component类
1.3.2 Swing容器
2 实现图形界面
2.1 实现图形界面的三步曲:
2.2 示例
import java.awt.*; import javax.swing.*; public class ButtonDemo extends JFrame { JButton btn1 = new JButton("Jbutton1"); JButton btn2 = new JButton("dddd"); JTextField txt = new JTextField(20); public ButtonDemo(){ super("test button"); btn1.setToolTipText("显示点选按钮"); btn2.setIcon(new ImageIcon("cupHJbutton.gif")); setLayout(new FlowLayout()); getContentPane().add(btn1); getContentPane().add(btn2); getContentPane().add(txt); setSize(400,300); setDefaultCloseOperation(EXIT_ON_CLOSE); btn1.addActionListener((e)->{ String name = ((JButton)(e.getSource())).getText(); txt.setText(name + " Pressed"); }); btn2.addActionListener((e)->{ String name = ((JButton)(e.getSource())).getText(); txt.setText(name + " Pressed"); }); } public static void main(String[] args) { // TODO Auto-generated method stub new ButtonDemo().setVisible(true); } }
3 布局管理
3.1 Java.awt包
常用的三种:FlowLayout BorderLayout GridLayout,还有CardLayout, GridBagLayout等
3.1 FlowLayout
3.2 BorderLayout 布局管理器
3.3 GridLayout 布局管理器
3.4 CardLayout 布局管理器
3.5 默认的布局管理器
4 事件处理
4.1 定义
4.2 事件适配器Adapter-----简化Listener
4.3 事件处理的步骤
4.4 创建监听器的6种方法
4.4.1 通过类本身实现监听器
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TestActionEventByAnonymous extends JFrame implements ActionListener{ JTextField txt = new JTextField( 20); JPanel pnl = new JPanel(); JButton b1 = new JButton("1"); JButton b2 = new JButton("2"); JButton b3 = new JButton("3"); JButton b4 = new JButton("4"); public void actionPerformed(ActionEvent e){ if(e.getSource() == b1) txt.setText("b1" + " Pressed"); else if(e.getSource() == b2) txt.setText("b2" + " Pressed"); } public TestActionEventByAnonymous(){ super("Nested Container"); pnl.setLayout(new GridLayout(2,2)); pnl.add(b1); pnl.add(b2); pnl.add(b3); pnl.add(b4); add(txt, BorderLayout.NORTH); add(pnl, BorderLayout.CENTER); b1.addActionListener(this); b2.addActionListener(this); setSize(200, 120); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); } public static void main(String args[]) { new TestActionEventByAnonymous(); } }
4.4.2 通过外部类实现监听器
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TestActionEventByAnonymous extends JFrame { JTextField txt = new JTextField( 20); JPanel pnl = new JPanel(); JButton b1 = new JButton("1"); JButton b2 = new JButton("2"); JButton b3 = new JButton("3"); JButton b4 = new JButton("4"); public TestActionEventByAnonymous(){ super("Nested Container"); pnl.setLayout(new GridLayout(2,2)); pnl.add(b1); pnl.add(b2); pnl.add(b3); pnl.add(b4); add(txt, BorderLayout.NORTH); add(pnl, BorderLayout.CENTER); ButtonEventListener btnListener = new ButtonEventListener(txt); ButtonEventListener btnListener2 = new ButtonEventListener(txt); b1.addActionListener(btnListener); b2.addActionListener(btnListener2); setSize(200, 120); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); } public static void main(String args[]) { new TestActionEventByAnonymous(); } } //外部类DialogEventListener,实现ActionListener接口 class ButtonEventListener implements ActionListener { JTextField txt; public ButtonEventListener(JTextField txt){ this.txt = txt; } @Override public void actionPerformed(ActionEvent e) { //创建JDialog窗口对象 String name = ((JButton)e.getSource()).getText(); txt.setText(name + " Pressed"); } }
4.4.3 通过内部类实现监听器
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TestActionEventByAnonymous extends JFrame { JTextField txt = new JTextField( 20); JPanel pnl = new JPanel(); JButton b1 = new JButton("1"); JButton b2 = new JButton("2"); JButton b3 = new JButton("3"); JButton b4 = new JButton("4"); //外部类DialogEventListener,实现ActionListener接口 class ButtonEventListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { //创建JDialog窗口对象 String name = ((JButton)e.getSource()).getText(); txt.setText(name + " Pressed"); } } public TestActionEventByAnonymous(){ super("Nested Container"); pnl.setLayout(new GridLayout(2,2)); pnl.add(b1); pnl.add(b2); pnl.add(b3); pnl.add(b4); add(txt, BorderLayout.NORTH); add(pnl, BorderLayout.CENTER); ButtonEventListener btnListener = new ButtonEventListener(); b1.addActionListener(btnListener); b2.addActionListener(btnListener); setSize(200, 120); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); } public static void main(String args[]) { new TestActionEventByAnonymous(); } }
4.4.4 通过局部类实现监听器
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TestActionEventByAnonymous extends JFrame { JTextField txt = new JTextField( 20); JPanel pnl = new JPanel(); JButton b1 = new JButton("1"); JButton b2 = new JButton("2"); JButton b3 = new JButton("3"); JButton b4 = new JButton("4"); public TestActionEventByAnonymous(){ super("Nested Container"); pnl.setLayout(new GridLayout(2,2)); pnl.add(b1); pnl.add(b2); pnl.add(b3); pnl.add(b4); add(txt, BorderLayout.NORTH); add(pnl, BorderLayout.CENTER); //局部类DialogEventListener,实现ActionListener接口 class ButtonEventListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { //创建JDialog窗口对象 String name = ((JButton)e.getSource()).getText(); txt.setText(name + " Pressed"); } } ButtonEventListener btnListener = new ButtonEventListener(); b1.addActionListener(btnListener); b2.addActionListener(btnListener); setSize(200, 120); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); } public static void main(String args[]) { new TestActionEventByAnonymous(); } }
4.4.5 通过匿名类实现监听器
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TestActionEventByAnonymous extends JFrame { JTextField txt = new JTextField( 20); JPanel pnl = new JPanel(); JButton b1 = new JButton("1"); JButton b2 = new JButton("2"); JButton b3 = new JButton("3"); JButton b4 = new JButton("4"); public TestActionEventByAnonymous(){ super("Nested Container"); pnl.setLayout(new GridLayout(2,2)); pnl.add(b1); pnl.add(b2); pnl.add(b3); pnl.add(b4); add(txt, BorderLayout.NORTH); add(pnl, BorderLayout.CENTER); //局部类DialogEventListener,实现ActionListener接口 class ButtonEventListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { //创建JDialog窗口对象 String name = ((JButton)e.getSource()).getText(); txt.setText(name + " Pressed"); } } b1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { txt.setText("b1" + " Pressed"); } }); b2.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { txt.setText("b2" + " Pressed"); } }); setSize(200, 120); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); } public static void main(String args[]) { new TestActionEventByAnonymous(); } }
4.4.6 通过lambda表达式实现监听器
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TestActionEventByAnonymous extends JFrame { JTextField txt = new JTextField( 20); JPanel pnl = new JPanel(); JButton b1 = new JButton("1"); JButton b2 = new JButton("2"); JButton b3 = new JButton("3"); JButton b4 = new JButton("4"); public TestActionEventByAnonymous(){ super("Nested Container"); pnl.setLayout(new GridLayout(2,2)); pnl.add(b1); pnl.add(b2); pnl.add(b3); pnl.add(b4); add(txt, BorderLayout.NORTH); add(pnl, BorderLayout.CENTER); //局部类DialogEventListener,实现ActionListener接口 class ButtonEventListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { //创建JDialog窗口对象 String name = ((JButton)e.getSource()).getText(); txt.setText(name + " Pressed"); } } b1.addActionListener(e->{ txt.setText("b1" + " Pressed"); }); b2.addActionListener(e->{ txt.setText("b2" + " Pressed"); }); setSize(200, 120); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); } public static void main(String args[]) { new TestActionEventByAnonymous(); } }
4.4.7 总结
4.5 事件与线程
5 应用示例
简单文本编辑器
import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.logging.*; import javax.swing.*; public class TextEditors { public static void main( String [] args){ javax.swing.SwingUtilities.invokeLater(new Runnable() { @Override public void run() { TextDAL dal = new FileTextDAL(); TextEditorFrame f = new TextEditorFrame(dal); f.setTitle( "简单的编辑器"); f.setSize( 800, 600 ); f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); f.setVisible(true); } }); } } class TextEditorFrame extends JFrame{ JTextPane text = new JTextPane(); JFileChooser fileChooser = new JFileChooser(); JColorChooser colorChoser = new JColorChooser(); JDialog about = new JDialog(); JMenuBar menuBar = new JMenuBar(); TextDAL dal = null; File file = null; Color color = Color.BLACK; TextEditorFrame(TextDAL dal){ this.dal = dal; initTextPane(); initMenu(); initAboutDialog(); initToolBar(); } void initTextPane(){ getContentPane().add(new JScrollPane(text)); } JMenu[] menus = new JMenu[]{new JMenu("File"), new JMenu("Edit"), new JMenu("Help") }; JMenuItem[][] menuItems = new JMenuItem[][]{ {new JMenuItem("New"), new JMenuItem("Open..."), new JMenuItem("Save..."), new JMenuItem("Exit")}, {new JMenuItem("Copy"), new JMenuItem("Paste"), new JMenuItem("Cut"), new JMenuItem("Color...")}, {new JMenuItem("About")} }; void initMenu(){ for(int i=0; i<menus.length; i++){ menuBar.add(menus[i]); for(int j=0; j<menuItems[i].length; j++){ menus[i].add(menuItems[i][j]); menuItems[i][j].addActionListener(action); } } this.setJMenuBar( menuBar ); } ActionListener action = new ActionListener(){ public void actionPerformed(ActionEvent evt){ JMenuItem mi = (JMenuItem)evt.getSource(); String id = mi.getText(); if(id.equals("New")){ text.setText(""); file = null; }else if(id.equals("Open...")){ if(file != null) fileChooser.setSelectedFile(file); int returnVal = fileChooser.showOpenDialog(TextEditorFrame.this); if(returnVal == JFileChooser.APPROVE_OPTION){ file = fileChooser.getSelectedFile(); openFile(); } }else if(id.equals("Save...")){ if(file != null) fileChooser.setSelectedFile(file); int returnVal = fileChooser.showSaveDialog(TextEditorFrame.this); if(returnVal == JFileChooser.APPROVE_OPTION) { file = fileChooser.getSelectedFile(); saveFile(); } }else if( id.equals("Exit")){ System.exit(0); }else if( id.equals("Cut")){ text.cut(); }else if( id.equals("Copy")){ text.copy(); }else if( id.equals("Paste")){ text.paste(); }else if( id.equals("Color...")){ color = JColorChooser.showDialog(TextEditorFrame.this, "", color ); text.setForeground(color); }else if( id.equals("About")){ about.setSize(100,50); about.setVisible(true); } } }; void saveFile(){ //保存文件,将字符写入文件 String content = text.getText(); dal.save(file, content); } void openFile(){ //读入文件,并将字符置入文本框中 String content = dal.read(file); text.setText(content); } void initAboutDialog(){ about.getContentPane().add( new JLabel("简单编辑器 V1.0") ); about.setModal( true ); about.setSize(100,50 ); } JToolBar toolBar = new JToolBar(); JButton[] buttons = new JButton[]{ new JButton("", new ImageIcon("copy.jpg")), new JButton("", new ImageIcon("cut.jpg")), new JButton("", new ImageIcon("paste.jpg")), }; void initToolBar(){ for(int i=0; i<buttons.length; i++) toolBar.add(buttons[i]); buttons[0].setToolTipText( "copy" ); buttons[0].addActionListener( new ActionListener(){ public void actionPerformed( ActionEvent e ){ text.copy(); } }); buttons[1].setToolTipText( "cut" ); buttons[1].addActionListener( new ActionListener(){ public void actionPerformed( ActionEvent e ){ text.cut(); } }); buttons[2].setToolTipText( "paste" ); buttons[2].addActionListener( new ActionListener(){ public void actionPerformed( ActionEvent e ){ text.paste(); } }); this.getContentPane().add( toolBar, BorderLayout.NORTH ); toolBar.setRollover(true); } } //------------- 关于数据存取、关于日志 --------------- interface TextDAL { String read(File file); void save(File file, String text); } class FileTextDAL implements TextDAL { @Override public String read(File file) { logger.log(Level.INFO, "read", "read..." + file.getPath()); try{ FileReader fr = new FileReader( file ); int len = (int) file.length(); char [] buffer = new char[len]; fr.read( buffer, 0, len ); fr.close(); return new String( buffer ); }catch(Exception e ){ e.printStackTrace(); logger.log(Level.SEVERE, null, e); } return ""; } @Override public void save(File file, String text) { logger.log(Level.INFO, "save", "save..." + file.getPath()); try{ FileWriter fw = new FileWriter( file ); fw.write( text ); fw.close(); }catch(Exception ex ){ ex.printStackTrace(); logger.log(Level.SEVERE, null, ex); } } //加点日志处理 Logger logger = Logger.getLogger( FileTextDAL.class.getName()); { try{ FileHandler handler = new FileHandler("TextEditorApp2.log");//可以用 %h/xxxx.log表示在用户主目录下 handler.setFormatter( new SimpleFormatter()); logger.addHandler(handler); }catch(IOException ex){} } }
图形用户界面(graphical user interface)
标签:
原文地址:http://www.cnblogs.com/penghuster/p/4861591.html