java编程常见类型题 --- GUI编程、同步线程模拟生产消费

网友投稿 273 2022-08-25

java编程常见类型题 --- GUI编程、同步线程模拟生产消费

java编程常见类型题 — GUI编程

GUI编程

import com.sun.corba.se.impl.protocol.giopmsgheaders.FragmentMessage;import javax.swing.*;import java.awt.event.*;public class e09 { public void createAndShow(){ // 设置主窗体 JFrame frame = new JFrame("文本输出"); frame.setSize(500,300); frame.setLocation(500,500); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); // 创建中间容器 JPanel jPanel = new JPanel(); JTextField textField = new JTextField(10); JTextArea textArea = new JTextArea(3,20); textField.addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { if (e.getKeyCode()==10){ textArea.setText(textField.getText()+"\n"+textField.getText()+"\n"+textField.getText()); } } }); // 添加按钮组件清空内容 JButton jButton = new JButton("清空"); // 为按钮添加监听事件 --- 注册监听到事件源 jButton.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { textField.setText(""); textArea.setText(""); } }); // 添加组件 jPanel.add(textField); jPanel.add(textArea); jPanel.add(jButton); frame.add(jPanel); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { e09 e = new e09(); e.createAndShow(); } }); }}

优化代码:

import javax.swing.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;public class e10 extends JFrame implements ActionListener { JTextArea textArea = new JTextArea(3,20); JTextField textField = new JTextField(10); JButton button = new JButton("清空"); public void showGUI() { // 初始化设置 this.setSize(500,300); this.setLocation(500,500); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); this.setLayout(new FlowLayout(FlowLayout.CENTER)); // 将监听器注册到事件源 button.addActionListener(this); textField.addActionListener(this); //添加组件 this.add(textField); this.add(textArea); this.add(button); } public static void main(String[] args) { e10 e = new e10(); e.showGUI(); } @Override //ActionEvent e 参数e表示触发的事件源 public void actionPerformed(ActionEvent e) { // 判断事件来源 if (e.getSource() == button){ // 处理按钮清空效果 textField.setText(""); textArea.setText(""); } if(e.getSource() == textField){ String str = textField.getText(); textArea.setText(str+"\n"+str+"\n"+str); } }}

同步线程模拟生产消费

import java.util.concurrent.atomic.AtomicInteger;/** * 采用Java 多线程技术,设计实现一个符合生产者和消费者问题的程序。 * 对一个对象(枪膛)进行操作,其最大容量是12颗子弹。 * 生产者线程是一个压入线程,它不断向枪膛中压入子弹; * 消费者线程是一个射出线程,它不断从枪膛中射出子弹 */public class e12{ // 仓库最大容量 static final int fulNum = 12; // 能够自动跟新的整型变量 static AtomicInteger bulletNum = new AtomicInteger(0); static String full = new String(); // 生产线程 static class creator implements Runnable{ @Override public void run() { while(true) { create(); } } // 生产 public static void create(){ synchronized (full){ if (bulletNum.intValue()0){ bulletNum.decrementAndGet(); //自增 System.out.println("消耗了子弹一枚"+bulletNum.intValue()); full.notifyAll(); } else { System.out.println("仓库已空"); try { full.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } } } // 测试 public static void main(String[] args) { new Thread(new creator()).start(); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } new Thread(new customer()).start(); }}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:java编程常见类型题 --- 水仙花数、邮资计算、集合+面向对象、序列化输入输出、自定义异常
下一篇:【Spark基础练习题一】
相关文章

 发表评论

暂时没有评论,来抢沙发吧~