
正文
一个小桌面的java代码 java桌面gui
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java: 创建一个桌子Table类,该类中有桌子名称,重量,桌面宽度,长度及桌子高度属性。
public class Table {
// 名称
private String name;
// 重量
private float weight;
// 宽度
private float width;
// 高度
private float height;
// 长度
private float length;
public Table() {
}
// 带4个参数的构造方法初始化所有数据成员
public Table(float weight, float width, float height, float length) {
super();
if (weight 0 || height 0 || width 0 || length 0) {
System.out.println("桌子的重量,宽度、长度和高度初始化时不能为负数");
} else {
this.weight = weight;
this.width = width;
this.height = height;
this.length = length;
}
}
// 计算桌面面积
public int area() {
return (int) (this.length * this.width);
}
public void display() {
System.out.println("名字:" + name + ";桌面长度:" + length + ";桌面宽度:" + width
+ ";重量:" + weight+";高度:"+height);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getWeight() {
return weight;
}
public void setWeight(float weight) {
if (weight 0) {
System.out.println("桌子的重量不能为负数");
} else {
this.weight = weight;
}
}
public float getWidth() {
return width;
}
public void setWidth(float width) {
if (width 0) {
System.out.println("桌子的宽度不能为负数");
} else {
this.width = width;
}
}
public float getHeight() {
return height;
}
public void setHeight(float height) {
if (height 0) {
System.out.println("桌子的高度不能为负数");
} else {
this.height = height;
}
}
public float getLength() {
return length;
}
public void setLength(float length) {
if (height 0) {
System.out.println("桌子的长度不能为负数");
} else {
this.length = length;
}
}
}
另外 5个属性 4个参数 初始化全部 不能理解
相关问答
Q1: 求一个简单的java代码:(图形界面)
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class vv extends JDialog {
private static final long serialVersionUID = 1L;
private JLabel l_Id = new JLabel("登陆账户", JLabel.CENTER);
private JLabel l_pw = new JLabel("登陆密码", JLabel.CENTER);
private JTextField t_Id = new JTextField(10);
private JPasswordField t_pw = new JPasswordField(10);
private JButton btnLogin;
private JButton btnClose;
public vv() {
super();
setResizable(false);
getContentPane().setBackground(new Color(225, 225, 225));
getContentPane().setLayout(null);
initialize();
}
protected void initialize() {
setTitle("系统登录");
l_Id.setBounds(48, 43, 53, 25);
t_Id.setBounds(110, 43, 150, 25);
l_pw.setBounds(48, 93, 53, 25);
t_pw.setBounds(110, 93, 150, 25);
getContentPane().add(l_Id);
getContentPane().add(l_pw);
getContentPane().add(t_Id);
getContentPane().add(t_pw);
btnLogin = new JButton();
btnLogin.setText("登 录");
btnLogin.setBounds(70, 142, 85, 28);
btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addBtnLoginActionListener();
}
});
getContentPane().add(btnLogin);
btnClose = new JButton();
btnClose.setText("关 闭");
btnClose.setBounds(175, 142, 85, 28);
btnClose.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
System.exit(-1);
}
});
getContentPane().add(btnClose);
}
private void addBtnLoginActionListener() {
String user = t_Id.getText();
String password = new String(t_pw.getPassword());
if (user.equals("")) {
JOptionPane.showMessageDialog(this, "帐号不可为空", "Caution",
JOptionPane.WARNING_MESSAGE);
return;
}
if (password.equals("")) {
JOptionPane.showMessageDialog(this, "密码不可为空", "Caution",
JOptionPane.WARNING_MESSAGE);
return;
}
String sql = "select * FROM login WHERE id = '" + user + "' and pw = '"
+ password + "'";
boolean success = false;
// TODO:数据校验 success = executeQuery(sql);
if (success) {
// TODO: 如果数据校验成功 显示主界面 并关闭登录界面
JOptionPane.showMessageDialog(this, "成功登录", "提示",
JOptionPane.INFORMATION_MESSAGE);
this.dispose();
} else {
JOptionPane.showMessageDialog(this, "帐号或密码错误!", "警告",
JOptionPane.WARNING_MESSAGE);
t_pw.requestFocus(); // 密码框选中
}
}
public Dimension getPreferredSize() {
return new Dimension(320, 170);
}
public void show() {
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension screen = tk.getScreenSize();
Dimension d = getSize();
this.setLocation((screen.width - d.width) / 2,
(screen.height - d.height) / 2);
// 输入密码后回车相当于点击了登录按钮
getRootPane().setDefaultButton(btnLogin);
t_pw.requestFocus();
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setSize(300, 220);
super.show();
}
public static void main(String[] args) {
vv loginFrame = new vv();
loginFrame.setVisible(true);
}
}
希望对你有帮助
Q2: 谁有Java桌面面板的教程,因为书上的代码错误,求高手给个能运行无错的代码
一个小桌面的java代码我运行一个小桌面的java代码的是对的。题主报的什么错?
BTW 题主把back.jpg放在正确的位置一个小桌面的java代码了吗?
Q3: Java程序:创建一个桌子(Table)类,该类中有桌子名称、重量、桌面宽度、长度和桌子高度属性,以及以下几个
package com.kaylves;
public class Table {
private String tableName;//名称
private int weight; //重量
private int length; //长度
private int height; //高度
private int width; //宽度
public Table(){
tableName="";
}
public Table(String tableName,int weight,int length,int height,int width){
this.tableName=tableName;
this.weight=weight;
this.length=length;
this.height=height;
this.width=width;
}
public int getArea(){
return length*width;
}
public void display(){
System.out.println("桌名称:"+this.tableName);
System.out.println("重量:"+this.weight);
System.out.println("长度:"+this.length);
System.out.println("高度:"+this.height);
System.out.println("宽度:"+this.width);
System.out.println("面积:"+this.getArea());
}
public void changeWeight(int w){
this.weight=w;
}
public static void main(String[] args){
Table table = new Table("小桌子",20,50,20,20);
int area=table.getArea();
table.changeWeight(30);
table.display();
}
}
Q4: 写了一段java代码,怎么让它以桌面程序的形式运行?
1.
创建一个记事本
2.
在记事本中输入
java love1
然后另存为love1.bat
3.
然后把这个bat文件和你的这个类编译后的class文件都丢到桌面上
4.
双击运行bat文件
PS:如果你要想做成桌面exe,可以使用exe4j来打包,不过exe一般是针对有界面的cs程序
Q5: 用java做一个桌面,能够读取windows桌面上的图标,并且能够点击打开,背景可以点击切换(3张
窗体自己布局,读取图标就直接遍历桌面的地址,比如C:\Users\Macro\Desktop(这个地址是我的桌面文件夹),打开就靠读取那快捷方式的指向或者文件的路径来打开.背景就利用设置背景语句来轮播,监听点击的地方来执行切换背景的代码.开机启动可以把程序写到到启动的注册表里.
只能给你说说思路,具体就靠自己了
一个小桌面的java代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java桌面gui、一个小桌面的java代码的信息别忘了在本站进行查找喔。







