setVisible(true);
setBackground(Color.WHITE);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
vgaPanel vgaPanel = new vgaPanel();
add(vgaPanel, BorderLayout.WEST);
regPanel regPanel = new regPanel();
add(regPanel, BorderLayout.EAST);
JPanel northJPanel = new JPanel();
northJPanel.setPreferredSize(new Dimension(800, 0));
add(northJPanel, BorderLayout.NORTH);
JPanel buttonJPanel = new JPanel();
buttonJPanel.setPreferredSize(new Dimension(800, 40));
buttonJPanel.add(new JButton("Run"));
buttonJPanel.add(new JButton("Step"));
buttonJPanel.add(new JButton("Restart"));
add(buttonJPanel, BorderLayout.SOUTH);
class vgaPanel extends JPanel{
public vgaPanel() {
setLayout(null);
//setSize(400, 600);
setPreferredSize(new Dimension(640, 300));
setBackground(Color.RED);
class regPanel extends JPanel{
public regPanel() {
setLayout(null);
//setSize(400, 600);
setPreferredSize(new Dimension(160, 300));
setBackground(Color.GREEN);
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Fram fram = new Fram();
Jframe默认布局为:BorderLayout
Jpanel默认布局为:FlowLayout
默认布局会屏蔽掉我们的设置大小(如setSize)
Jpanel中使用setPreferredSize更改大小,如果想使用自己的布局方式,必须setLayout(null);
先贴代码:package xiya;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Dimension;import java.awt.FlowLayout;import javax.swing.JButton;import javax.swing.JFrame;import javax.
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import javax.swing.ButtonGroup;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.
JFrame
;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.
JPanel
;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class MyButton extends JButton implements MouseListener, ActionListener {
private static final long serialVersionUID = 6261021704484837560L;
private JPopupMenu popMenu;
private ButtonGroup bg;
private JRadioButton SetDark;// 置为脏房,待打扫
private JRadioButton SetClean;// 置为应该打扫的房间
private JRadioButton SetUsing;// 置为正在使用的房间
private JRadioButton SetHouseNum;// 设置房号
private JRadioButton SetEmpty;//置为空房
private JRadioButton Delete;//删除
private JRadioButton Bujiaoyajin;//补交押金
private String HouseAr, HouseNu;
private Color Housecolor;
private int ID;
private double SingelSal;
public double getSingelSal() {
return SingelSal;
public void setSingelSal(double singelSal) {
SingelSal = singelSal;
private HashMap<String, MyButton> tempMap;
private Customer customer;
public MyButton(int ID,HashMap<String, MyButton> tempMap)
super.setIcon(new ImageIcon("Blue Victorian.JPG"));
this.ID = ID;
this.tempMap = tempMap;
init();
public MyButton() {
super.setIcon(new ImageIcon("Blue Victorian.JPG"));
init();
public void init()
Housecolor = Color.GREEN;
super.setBackground(Housecolor);
popMenu = new JPopupMenu();
bg = new ButtonGroup();
SetDark = new JRadioButton("置为脏房");
SetClean = new JRadioButton("置为闲置房");
SetUsing = new JRadioButton("添加留客");
SetHouseNum = new JRadioButton("初始房态");
SetEmpty = new JRadioButton("置为空房");
Delete = new JRadioButton("删除本房");
Bujiaoyajin = new JRadioButton("补交押金");
// 添加action监听
SetDark.addActionListener(this);
SetClean.addActionListener(this);
SetUsing.addActionListener(this);
SetHouseNum.addActionListener(this);
SetEmpty.addActionListener(this);
Delete.addActionListener(this);
Bujiaoyajin.addActionListener(this);
// 加入buttongroup,实现单选
bg.add(SetClean);
bg.add(SetDark);
bg.add(SetUsing);
bg.add(SetHouseNum);
bg.add(SetEmpty);
bg.add(Delete);
bg.add(Bujiaoyajin);
//bg.add(Delete);
// 设置相应房态颜色
popMenu.add(SetDark);
popMenu.add(SetClean);
popMenu.add(SetUsing);
popMenu.add(SetHouseNum);
popMenu.add(SetEmpty);
popMenu.add(Bujiaoyajin);
//popMenu.add(Delete);
// 设置jbutton的固定
大小
setMaximumSize(new Dimension(80, 60));
setMinimumSize(new Dimension(80, 60));
setPreferredSize(new Dimension(80, 60));
this.addMouseListener(this);
public void mouseClicked(MouseEvent arg0) {}
public void mouseEntered(MouseEvent arg0) {}
public void mouseExited(MouseEvent arg0) {}
public void mouseReleased(MouseEvent arg0) {}
// 为jbuuton添加popmenu
public void mousePressed(MouseEvent arg0) {
if (arg0.getButton() == MouseEvent.BUTTON3) {
popMenu.show(arg0.getComponent(), arg0.getX(), arg0.getY());
//定位该button在map中的位置
public MyButton indexof()
MyButton b = null;
Set<String> set = this.tempMap.keySet();
Iterator<String> iter = set.iterator();
while(iter.hasNext())
String id = iter.next();
if(this.ID == Integer.parseInt(id.trim()))
b = this.tempMap.get(id);
return b;
//getcustomer的信息
public Customer getCustomer()
return this.customer;
// 为选项添加监听
public void actionPerformed(ActionEvent e) {
if(e.getSource() == Bujiaoyajin)
System.out.println("fgfhjhk");
if (e.getSource() == SetDark) {
Housecolor = Color.yellow;
this.setBackground(Housecolor);
indexof().setHouseColor(Housecolor);
Write();
if (e.getSource() == SetClean) {
Housecolor = Color.gray;
this.setBackground(Housecolor);
indexof().setHouseColor(Housecolor);
Write();
if (e.getSource() == Delete) {
tempMap.remove(ID);
Write();
if (e.getSource() == SetEmpty) {
Housecolor = Color.green;
this.setBackground(Housecolor);
indexof().setHouseColor(Housecolor);
Write();
if (e.getSource() == SetUsing) {
new GetCustomerInfo();
this.setBackground(Housecolor);
if (e.getSource() == SetHouseNum) {
new GetHouseInfo();
public int getID() {
return ID;
public void setID(int id) {
ID = id;
//保存到本地
public void Write()
try {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("d:/ButtonsInfo.shf"));
oos.writeObject(this.tempMap);
oos.flush();
oos.close();
} catch (Exception e) {
e.printStackTrace();
//为外层程序提供
public void setHouseAr(String str)
this.HouseAr = str;
public String getHouseAr()
return this.HouseAr;
public void setHouseNu(String str)
this.HouseNu = str;
public String getHouseNu()
return this.HouseNu;
public void setHouseColor(Color cl)
this.Housecolor = cl;
public Color getHouseColor()
return this.Housecolor;
// 设置房号的窗口
class GetHouseInfo extends
JFrame
implements ActionListener {
private static final long serialVersionUID = 4824740927444846504L;
private
JPanel
p, p1, p2, p3,p4;
private JLabel HouseArea, HouseNum,SingleSal;
private JTextField HouseA, HouseN,SinglS;
private JButton Entrue, Cansal;
private String A,N;
public GetHouseInfo() {
super.setTitle("初始房态");
p = new
JPanel
();
p1 = new
JPanel
();
p2 = new
JPanel
();
p3 = new
JPanel
();
p4 = new
JPanel
();
HouseArea = new JLabel("设定房区:");
HouseNum = new JLabel("设定房号:");
SingleSal = new JLabel("设定单价:");
HouseA = new JTextField(10);
HouseN = new JTextField(10);
SinglS = new JTextField(10);
Entrue = new JButton("确定");
Cansal = new JButton("取消");
Entrue.addActionListener(this);
Cansal.addActionListener(this);
p.setLayout(new GridLayout(4, 1));
p1.add(HouseArea);
p1.add(HouseA);
p2.add(HouseNum);
p2.add(HouseN);
p3.add(Entrue);
p3.add(Cansal);
p4.add(SingleSal);
p4.add(SinglS);
p.add(p1);
p.add(p2);
p.add(p4);
p.add(p3);
this.add(p);
this.setLocation(450, 300);
this.setAlwaysOnTop(true);
this.setVisible(true);
this.pack();
public String getHouseA()
return this.A;
public String getHouseN()
return this.N;
@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent arg0) {
if (arg0.getSource() == Entrue) {
A = HouseAr = HouseA.getText();
N = HouseNu = HouseN.getText();
SingelSal = new Double(SinglS.getText().trim()).doubleValue();
MyButton.this.setText(HouseAr + "\n" + HouseNu);
indexof().setHouseNu(A+N);
Write();
this.hide();
if (arg0.getSource() == Cansal) {
this.hide();
//留客信息录入
class GetCustomerInfo extends
JFrame
implements ActionListener {
private static final long serialVersionUID = 4824740927444846504L;
private
JPanel
p, p1, p2, p3,p4,p5,p6;
private JLabel Name, SumCash,SumDays,Sex,TelePhone;
private JTextField HouseA, HouseN,SumD,telePhone;
private JButton Entrue, Cansal;
private JLabel sex;
private ButtonGroup bg;
private JRadioButton man,waman;
private String sexStr;
public GetCustomerInfo() {
super.setTitle("录入顾客信息");
p = new
JPanel
();
p1 = new
JPanel
();
p2 = new
JPanel
();
p3 = new
JPanel
();
p4 = new
JPanel
();
p5 = new
JPanel
();
p6 = new
JPanel
();
Name = new JLabel("旅客姓名:");
sex = new JLabel("性别: ");
TelePhone = new JLabel("联系方式:");
SumCash = new JLabel("旅客押金:");
SumDays = new JLabel("留住天数:");
HouseA = new JTextField(10);
telePhone = new JTextField(10);
HouseN = new JTextField(10);
SumD = new JTextField(10);
Entrue = new JButton("确定");
Cansal = new JButton("取消");
bg = new ButtonGroup();
man = new JRadioButton("男");
waman = new JRadioButton("女");
bg.add(man);
bg.add(waman);
p6.add(sex);
p6.add(man);
p6.add(waman);
Entrue.addActionListener(this);
Cansal.addActionListener(this);
man.addActionListener(this);
waman.addActionListener(this);
p.setLayout(new GridLayout(6, 1));
p1.add(Name);
p1.add(HouseA);
p2.add(SumCash);
p2.add(HouseN);
p3.add(Entrue);
p3.add(Cansal);
p4.add(SumDays);
p4.add(SumD);
p5.add(TelePhone);
p5.add(telePhone);
p.add(p1);
p.add(p5);
p.add(p6);
p.add(p2);
p.add(p4);
p.add(p3);
this.add(p);
this.setLocation(450, 300);
this.setVisible(true);
this.pack();
@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent arg0) {
if(arg0.getSource() == man)
sexStr = "男";
if(arg0.getSource() == waman)
sexStr = "女";
if (arg0.getSource() == Entrue) {
customer = new Customer(HouseA.getText(),sexStr,telePhone.getText(),
new Integer(SumD.getText().trim()).intValue(),
new Integer(HouseN.getText().trim()).intValue());
Housecolor = Color.blue;
MyButton.this.setBackground(Housecolor);
indexof().setHouseColor(Housecolor);
if(new Double(HouseN.getText().trim()).doubleValue() < SingelSal)
JOptionPane.showMessageDialog(null, "押金不足!");
Write();
this.hide();
if (arg0.getSource() == Cansal) {
this.hide();
public static void main(String[] s) {
JFrame
frame = new
JFrame
();
JPanel
panel = new
JPanel
();
MyButton mb = new MyButton();
panel.add(mb);
frame.add(panel);
frame.setSize(600, 600);
frame.setVisible(true);
frame.setDefaultCloseOperation(
JFrame
.EXIT_ON_CLOSE);
Java图片缩小与放大特效,// 图像缩小与放大演示
public class ScaleImageDemo extends
JFrame
{
private
JPanel
panel = new
JPanel
(); //面板panel用于容纳图像放大、缩孝还原按钮
private JButton jbFile = new JButton("打开图像文件"); //打开图像文件按钮
private JButton jbZoomIn = new JButton("放大"); //图像放大按钮
private JButton jbZoomOut = new JButton("缩小"); //图像缩小按钮
private JButton jbReset = new JButton("还原"); //图像还原按钮
ScalePane showImagePane = new ScalePane(); //创建showImagePane对象用于绘制图像
Container content = getContentPane(); //获得窗口的容器
//构造函数
public ScaleImageDemo() {
super("图像的缩小与放大"); //调用父类构造器设置窗口标题栏
//为按钮添加动作监听器
jbFile.addActionListener(new ButtonActionListener());
jbZoomIn.addActionListener(new ButtonActionListener());
jbZoomOut.addActionListener(new ButtonActionListener());
jbReset.addActionListener(new ButtonActionListener());
//把图像放大按钮、图像缩小按钮、图像还原按钮加入panel面板
panel.add(jbZoomIn);
panel.add(jbZoomOut);
panel.add(jbReset);
//把showImagePane文件选择组合框、控制面板、状态栏标签加入到窗口内容窗格
content.add(showImagePane, BorderLayout.CENTER);
content.add(jbFile, BorderLayout.NORTH);
content.add(panel, BorderLayout.SOUTH);
setSize(260, 200); //设置窗口
大小
setVisible(true); //设置窗口可见
setDefaultCloseOperation(
JFrame
.EXIT_ON_CLOSE); //关闭窗口时退出程序
Java调用Zip类批量压缩多个文件,此前有一个是压缩单个文件,也可参考,相关代码中可找到此源码。
public class ZipDemo extends
JFrame
{
JFileChooser fileChooser; //文件选择器
JList fileList; //待压缩的文件列表
Vector files; //文件数据(待压缩文件)
JButton jbAdd; //增加文件按钮
JButton jbDelete; //删除文件按钮
JButton jbZip; //压缩按钮
JTextField target; //目标文件文本域
public ZipDemo(){
super("用ZIP压缩多个文件"); //调用父类构造函数
fileChooser=new JFileChooser(); //实例化文件选择器
files=new Vector(); //实例化文件数据Vector
fileList=new JList(files); //实例化已选择文件列表
jbAdd=new JButton("增加"); //实例化按钮组件
jbDelete=new JButton("删除");
jbZip=new JButton("压缩");
target=new JTextField(18);
JPanel
panel=new
JPanel
(); //实例化面板,用于容纳按钮
panel.add(jbAdd); //增加组件到面板上
panel.add(jbDelete);
panel.add(jbZip);
JPanel
panel2=new
JPanel
();
panel2.add(new JLabel("目标文件"));
panel2.add(target);
JScrollPane jsp=new JScrollPane(fileList);
Container container=getContentPane(); //得到容器
container.add(panel2,BorderLayout.NORTH); //增加组件到容器
container.add(jsp,BorderLayout.CENTER);
container.add(panel,BorderLayout.SOUTH);
jsp.setBorder(BorderFactory.createEmptyBorder(10,10,10,10)); //设置边界
public clasjavax.swing 类
JFrame
java.lang.Object java.awt.Component java.awt.Container java.awt.Window java.awt.Frame javax.swing.
JFrame
s Demo1 {
public static void main(String[] args) {
// 创建
JFrame
JFrame
frame = new
JFrame
...
import javax.swing.*; //导入Swing包
import java.awt.event.*; //导入事件包
import java.awt.*;
//继承
JFrame
类
public class BuJu1 extends
JFrame
JButton jb1=new JButton("第一个按钮"); //创建第一个按钮
JButton jb2=new JButton("第二个按钮"); //创建第二个按钮
JButton jb3=new JButton("第三个按钮"); //创建第三个按钮
JButton jb4=new JButton("第四个按钮"); //创建第四个按钮
JButton jb5=new JButton("第五个按钮"); //创建第五个按钮
JPanel
jp=new
JPanel
(); //创建一个面板
//定义构造器
public BuJu1()
this.setTitle("使用流布局管理器"); //设置窗口名称
jp.setLayout(new FlowLayout()); //设置面板的布局为流布局
jp.add(jb1); //将按钮添加到面板中
jp.add(jb2);
jp.add(jb3);
jp.add(jb4);
jp.add(jb5);
this.add(jp); //将面板添加到窗口中
this.setBounds(300,250,300,200); //设置窗口的
大小
和位置
this.setVisible(true); //设置窗口是可见的
public static void main(String args[])
BuJu1 s=new BuJu1();
一面:算法题:长度为n的数组
里
放了n+1个
大小
在[1,n]的数,必然至少有一个重复的数,找出来
二面:概率题:求一根绳子被切两刀能组成一个三角形的概率。
三面主管面:FM推导,deepfm原理,graph embedding,问了之前的一些项目。
四面交叉面:模型上线时应该注意的事,如果请求过高模型服务挂了怎么办,tensorflow和torch的区别,如何降低模型复杂度。
第二个:百度
原生商业推广部
一面:算法题:快排非递归,旋转有序数组找某个值
二面:算法题:
使用
JFrame
作为主窗口,将计算器的各个组件添加到
JFrame
中。 使用
JPanel
来组织计算器的布局,将数字键、运算符键、清除键等分别放在不同的
JPanel
中。 使用JButton作为数字键、运算符键、清除键等的组件。 使用JTextField作为显示计算结果的组件。 使用ActionListener来监听按钮的点击事件,根据不同的按钮执行不同的操作。 在ActionListener中,使用if语句或switch语句来判断用户点击的是哪个按钮,然后执行相应的操作,例如进行加减乘除运算、清空文本框等。 在进行运算时,可以使用Java的数学库来进行计算。 最后,记得将计算器的各个组件添加到
JFrame
中,并设置
JFrame
的
大小
、标题等属性。
public class Calculator extends
JFrame
implements ActionListener {
private JTextField display;
private JButton[] buttons;
private String[] buttonLabels = {
"7", "8", "9", "+",
"4", "5", "6", "-",
"1", "2", "3", "*",
"0", ".", "=", "/"
private
JPanel
buttonPanel;
public Calculator() {
super("Calculator");
setDefaultCloseOperation(
JFrame
.EXIT_ON_CLOSE);
display = new JTextField(20);
display.setEditable(false);
buttons = new JButton[buttonLabels.length];
buttonPanel = new
JPanel
(new GridLayout(4, 4));
for (int i = 0; i < buttonLabels.length; i++) {
buttons[i] = new JButton(buttonLabels[i]);
buttons[i].addActionListener(this);
buttonPanel.add(buttons[i]);
getContentPane().add(display, BorderLayout.NORTH);
getContentPane().add(buttonPanel, BorderLayout.CENTER);
pack();
setVisible(true);
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals("C")) {
display.setText("");
} else if (command.equals("=")) {
String expression = display.getText();
double result = evaluate(expression);
display.setText(Double.toString(result));
} else {
display.setText(display.getText() + command);
private double evaluate(String expression) {
// TODO: Implement the evaluation logic using Java's math library
return 0.0;
public static void main(String[] args) {
new Calculator();
在这个示例代码中,我们使用了
JFrame
作为主窗口,并将计算器的各个组件添加到
JFrame
中。我们使用了
JPanel
来组织计算器的布局,并将数字键、运算符键、清除键等分别放在不同的
JPanel
中。我们使用了JButton作为数字键、运算符键、清除键等的组件,并使用ActionListener来监听按钮的点击事件。在ActionListener中,我们使用if语句或switch语句来判断用户点击的是哪个按钮,然后执行相应的操作,例如进行加减乘除运算、清空文本框等。在进行运算时,我们可以使用Java的数学库来进行计算。最后,我们将计算器的各个组件添加到
JFrame
中,并设置
JFrame
的
大小
、标题等属性。