十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
public class Room {
成都创新互联从2013年开始,先为岳阳县等服务建站,岳阳县等地企业,进行企业商务咨询服务。为岳阳县企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
private int roomID;
private double roomArea;
private String address;
public int getRoomID() {
return roomID;
}
public void setRoomID(int roomID) {
this.roomID = roomID;
}
public double getRoomArea() {
return roomArea;
}
public void setRoomArea(double roomArea) {
this.roomArea = roomArea;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Room(){};
public Room(int roomID){
this.roomID = roomID;
};
public Room(int roomID, double roomArea){
this.roomID = roomID;
this.roomArea = roomArea;
}
public Room(int roomID, double roomArea, String address){
this.roomID = roomID;
this.roomArea = roomArea;
this.address = address;
}
public String getDetails() {
return "Room [房号:" + roomID + ", 面积:" + roomArea + ", 地址:" + address + "]";
}
public static void main(String[] args) {
Room room1 = new Room();
Room room2 = new Room(1);
Room room3 = new Room(2, 100);
Room room4 = new Room(3, 120, "china");
System.out.println(room1.getDetails() + "\r\n" + room2.getDetails() + "\r\n" + room3.getDetails() + "\r\n" + room4.getDetails());
}
}
这么简单的不能自己写吗
下面是WINDOW界面的代码,用JAVA SWING写的。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class PlusWhile {
//主面板
private JFrame mainTop = null;
private JPanel inPanel = null; //输入面板
private JPanel outPanel = null;//输出面板
private JButton count = null; //按钮
private JTextField inText = null; // 输入文本框
private JTextField result = null;//结果文本框
private JLabel resultOut = null;//输出提示
private JLabel remindIn = null;//输入提示
//初始化方法
private void init() {
//组件实例化
mainTop = new JFrame("阶乘计算器");
inPanel = new JPanel();
outPanel = new JPanel();
count = new JButton("计算");
inText = new JTextField(10);
result = new JTextField(10);
resultOut = new JLabel("计算的结果:");
remindIn = new JLabel("请输入要计算的正整数:");
//添加输入组件
inPanel.add(remindIn);
inPanel.add(inText);
inPanel.add(count);
//添加输出组件
outPanel.add(resultOut);
outPanel.add(result);
result.setEnabled(false);
//设置布局样式
mainTop.setLayout(new BorderLayout());
//主面板添加组件
mainTop.add(inPanel, BorderLayout.NORTH);
mainTop.add(outPanel, BorderLayout.SOUTH);
//设置监听
count.addActionListener(new ButtonListener());
//设置屏显位置
mainTop.setLocation(350 , 300);
mainTop.pack();
mainTop.setVisible(true);
mainTop.setAlwaysOnTop(true);
//设置关闭事件
mainTop.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//监听类
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
int inputNum = 0;
String input = inText.getText();
//若输入为非数字,提示错误
try {
inputNum = Integer.parseInt(input);
}catch(Exception e) {
inputNum = -1;
}
//输入数值小于0,提示错误
if(inputNum 0) {
inText.setText("请输入正确的数值!");
result.setText("");
}
else if(inputNum == 0) {//输入值为0处理
result.setText("1");
}
else {//输入值为正整数,计算阶乘
int temp = inputNum;
while(inputNum 1) {
temp *= --inputNum;
}
result.setText(temp + "");
}
}
}
//入口方法
public static void main(String[] args) {
new PlusWhile().init();
}
}
有什么不明白的继续补充,呵呵。 加点儿分不。
几年没有碰swing了,给你详细注释了。仔细看。希望对你有所帮助。
import java.awt.*;//java抽象窗口工具包
import java.awt.event.*;//java抽象窗口工具包组件所激发的各类事件的接口和类
public class Test5{//类名
Frame f;//定义一个Frame窗体
TextArea center;//文本域
Label la1,la2,la3;//三个标签
Panel east,south,north;//三块面板
Button b1,b2,b3,b4;//四个按钮
Choice l1;//下拉单选框
TextField t1;//文本域
// textfield只有一行可写
// textarea是一个区域,可以有很多行
public static void main(String[] args){//主函数,程序入口
Test mb = new Test();
mb.go();//调用go方法,初始化界面
}
private void go(){
f = new Frame("留言版程序");//标题
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent evt){
f.setVisible(false);
System.exit(0);
//System.exit(0) 0--正常结束程序 1--异常关闭程序
}
});
f.setBounds(0,0,600,400);//布局大小
f.setLayout(new BorderLayout());//显示方式
f.setResizable(false);
//下面都将控件按钮初始化,你懂得
north = new Panel();
south = new Panel();
east = new Panel();
center = new TextArea("留言内容:");
center.setEditable(false);
b1 = new Button("清屏");
b2 = new Button("至顶");
b3 = new Button("至尾");
la1 = new Label("留言版");
la2 = new Label("你");
la3 = new Label(" 地说:");
t1 = new TextField(20);
b4 = new Button("提交");
l1 =new Choice();
l1.add("微笑 ");
l1.add("生气 ");
l1.add("伤心 ");
f.add(BorderLayout.NORTH,north);//布局
f.add(BorderLayout.SOUTH,south);//布局
f.add(BorderLayout.EAST,east);//布局
f.add(BorderLayout.CENTER,center);//布局
north.add(BorderLayout.CENTER,la1);
south.add(la2);//把东西加到面板上
south.add(l1);
south.add(la3);
south.add(t1);//把东西加到面板上
south.add(b4);
east.setLayout(new GridLayout(9,0,1,10));
east.add(b1);
east.add(b2);
east.add(b3);
f.setVisible(true);//可视化
b4.addActionListener(new ActionListener(){//点击提交按钮产生事件
public void actionPerformed(ActionEvent e){
String text1,text2;
text1 = l1.getSelectedItem();
text2 = t1.getText();
t1.setText(null);
if(t1.getText() != ""){//将下拉单选框的内容和你输入在文本框的内容显示在中间面板上
center.append("\n");
center.append(text1);
center.append(text2);
}
}
});
b1.addActionListener(new ActionListener(){//清屏按钮事件,只留下中间面板显示:“留言内容:”,其余清楚
public void actionPerformed(ActionEvent e){
center.setText("留言内容:");
}
});
b2.addActionListener(new ActionListener(){//至顶按钮事件,光标焦点至顶
public void actionPerformed(ActionEvent e){
center.requestFocusInWindow();
center.setCaretPosition(8);
}
});
b3.addActionListener(new ActionListener(){//至尾按钮事件,光标焦点至尾
public void actionPerformed(ActionEvent e){
center.requestFocusInWindow();
center.setCaretPosition(center.getText().length());
}
});
}
}