输入框事件监听
package www.qyl.lesson01; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class TestActionEvent { public static void main(String[] args) { JFrame jFrame = new JFrame(); jFrame.setBounds(10,10,300,300); jFrame.setVisible(true); JTextField jTextField = new JTextField("test:"); jFrame.getContentPane().add(jTextField); jTextField.addActionListener(new MyActionEvent()); } } class MyActionEvent implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { JTextField jTextField = (JTextField)e.getSource(); //获得一些资源、返回一个对象 System.out.println(jTextField.getText()); //获得输入框中的内容 jTextField.setText(""); //设置输入框为 null } }
文本框输入文字回车后提交到后台
键盘监听
package www.qyl.lesson01; import javax.swing.*; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; public class TestKeyListener { public static void main(String[] args) { new KeyFrame(); } } class KeyFrame extends JFrame{ public KeyFrame(){ setBounds(10,10,300,300); setVisible(true); this.addKeyListener(new KeyListener() { @Override public void keyTyped(KeyEvent e) { } @Override public void keyPressed(KeyEvent e) { int keycode = e.getKeyCode(); //获得键盘按下的哪一个键 System.out.println(keycode); //打印目前按下的键(会输出一个静态属性值) if (keycode == KeyEvent.VK_SPACE){ //键盘上每个健对应一个静态属性值:VK_XX System.out.println("你按下了空格"); } } @Override public void keyReleased(KeyEvent e) { } }); } }
5、JDialog弹窗
package www.qyl.lesson01; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class TestDialog { public static void main(String[] args) { JFrame jFrame = new JFrame(); jFrame.setVisible(true); jFrame.setBounds(10,10,300,300); JButton button = new JButton("button1"); jFrame.getContentPane().add(button); button.addActionListener(new MyAction1()); } } class MyAction1 implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { new MyDialogDemo(); } } class MyDialogDemo { public MyDialogDemo(){ JDialog jDialog = new JDialog(); jDialog.setVisible(true); jDialog.setBounds(100,100,300,300); } }
点击按钮弹出JDialog窗口
6、Icon、ImageIcon标签
Icon
package www.qyl.lesson02; import javax.swing.*; import java.awt.*; public class IconDemo extends JFrame implements Icon { private int width,height; public IconDemo(){ } public IconDemo(int width,int height){ this.width = width; this.height = height; } public void init(){ IconDemo iconDemo = new IconDemo(15,15); JLabel label = new JLabel("icontest",iconDemo,SwingConstants.CENTER); Container container = getContentPane(); container.add(label); this.setBounds(50,50,100,100); this.setVisible(true); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new IconDemo().init(); } @Override public void paintIcon(Component c, Graphics g, int x, int y) { g.fillOval(x,y,width,height); //g:是画笔,g.fillOval画出一个图形 } @Override public int getIconWidth() { return this.width; } @Override public int getIconHeight() { return this.height; } }
ImageIcon
图片也可以放在按钮上,称为图片按钮
package www.qyl.lesson02; import com.sun.jndi.toolkit.url.UrlUtil; import javax.swing.*; import java.awt.*; import java.net.URL; public class ImageIconDemo extends JFrame { public ImageIconDemo(){ JLabel label = new JLabel("ImageIcon"); URL url = ImageIconDemo.class.getResource("hhy.png"); //获取本类路径下的图片路径,获取图片路径 ImageIcon imageIcon = new ImageIcon(url); label.setIcon(imageIcon); Container container = getContentPane(); container.add(label); setVisible(true); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setBounds(20,20,300,300); } public static void main(String[] args) { new ImageIconDemo(); } }
7、按钮
单选按钮
package www.qyl.lesson02; import javax.swing.*; public class TestradioBttton { public static void main(String[] args) { JFrame jf = new JFrame(); jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); jf.setBounds(10,10,300,300); jf.setVisible(true); JPanel jPanel = new JPanel(); jRadioButtonJiZhuPass.isSelected() JRadioButton jradio1 = new JRadioButton("男"); JRadioButton jradio2 = new JRadioButton("女"); //单选只能选择一个所以需要分组,一个组中只能选择一个 ButtonGroup group = new ButtonGroup(); group.add(jradio1); group.add(jradio2); //判断是否选中单选框 System.out.println(jradio1.isSelected()); //返回Boolean类型 jPanel.add(jradio1); jPanel.add(jradio2); jf.add(jPanel); } }