0
点赞
收藏
分享

微信扫一扫

java编程产检类型题 --- 程序界面[库存查询]


程序界面 — 库存查询

java编程产检类型题 --- 程序界面[库存查询]_swing

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;

public class Exam5 {

public void createAndShow(){

JFrame frame = new JFrame("库存查询窗口"); // 创建顶层容器(窗口)
frame.setSize(400,300); // 设置容器大小
frame.setLocation(300,200); // 设置容器初始位置
frame.setVisible(true); // 设置容器可见
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置容器关闭方式

JPanel jPanel = new JPanel(); // 添加中间容器
JComboBox<String> jComboBox = new JComboBox<>(); // 添加下拉列表组件
jComboBox.addItem("请选择商品"); // 添加下拉列表元素
jComboBox.addItem("色拉油");
jComboBox.addItem("齐心汽水");
jComboBox.addItem("米酒");
jComboBox.addItem("冰淇淋");
jComboBox.addItem("蛋糕");

JTextField jTextField1 = new JTextField(5); // 添加文本组件
JLabel jLabel1 = new JLabel("单价"); // 添加标签组件
JTextField jTextField2 = new JTextField(5);
JLabel jLabel2 = new JLabel("库存");

jComboBox.addActionListener(e->{ // 配置下拉列表动作监听

String goods = (String) jComboBox.getSelectedItem(); // 返回当前所选项并转化为字符串的形式
if ("色拉油".equals(goods)){
jTextField1.setText("56");
jTextField2.setText("232");
}else if ("齐心汽水".equals(goods)){
jTextField1.setText("8");
jTextField2.setText("50");
}else if ("米酒".equals(goods)){
jTextField1.setText("10");
jTextField2.setText("109");
}else if ("冰淇淋".equals(goods)){
jTextField1.setText("20");
jTextField2.setText("48");
}else {
jTextField1.setText("90");
jTextField2.setText("30");
}

});

jPanel.add(jComboBox); // 组件的层层嵌套
jPanel.add(jLabel1);
jPanel.add(jTextField1);
jPanel.add(jLabel2);
jPanel.add(jTextField2);
frame.add(jPanel, BorderLayout.PAGE_START);

}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() { // 启动一个线程去运行
@Override
public void run() {
Exam5 e = new Exam5();
e.createAndShow();
}
});
}
}


举报

相关推荐

0 条评论