Java 赢得的面板和复选框';我没有出现
在我的教科书中,我一直在学习如何创建一个窗口应用程序以及如何使用Java中的布局管理器。我已经使用textpad输入了代码,就像书中显示的一样,但并不是所有内容都显示在我的应用程序中。该应用程序是一个预订聚会室的程序。它显示8个面板,用于预订吸烟和非吸烟房间。有一个客人人数选择列表,两个吸烟和非吸烟复选框,两个姓名和电话号码文本字段,一个预订房间按钮。我已经输入了代码,它是如何在书中显示的。唯一显示的是2文本字段、选项列表和book room按钮。谁能告诉我我做错了什么。没有编译错误。 这是我的名为Rooms的外部类:Java 赢得的面板和复选框';我没有出现,java,swing,Java,Swing,在我的教科书中,我一直在学习如何创建一个窗口应用程序以及如何使用Java中的布局管理器。我已经使用textpad输入了代码,就像书中显示的一样,但并不是所有内容都显示在我的应用程序中。该应用程序是一个预订聚会室的程序。它显示8个面板,用于预订吸烟和非吸烟房间。有一个客人人数选择列表,两个吸烟和非吸烟复选框,两个姓名和电话号码文本字段,一个预订房间按钮。我已经输入了代码,它是如何在书中显示的。唯一显示的是2文本字段、选项列表和book room按钮。谁能告诉我我做错了什么。没有编译错误。 这是我的
public class Rooms{
//declaring variables
int numSmoking;
int numNonSmoking;
boolean occupied[];
//method for rooms
public Rooms(int non, int sm){
occupied = new boolean[sm+non];
//for loop for array occupied
for(int i=0; i < (sm+non); i++)
occupied[i] = false; //set each occupied room to false or empty
//initialize the number of smoking and non smoking rooms
numSmoking = sm;
numNonSmoking = non;
}//end method rooms
//method to book room
public int bookRoom(boolean smoking){
int begin, end;
int roomNumber=0;
//if statement for non smoking and smoking room booking
if(!smoking){
begin = 0;
end = numNonSmoking;
}//end if
else{
begin = numNonSmoking;
end = numSmoking+numNonSmoking;
}//end else
for (int i=begin; i < end; i++){
if(!occupied[i]){//if room not occupied
occupied[i] = true;
roomNumber = i+1;
i = end;//to exit loop
}//end if
}//end for
return roomNumber;
}//end method bookroom
}//ends class
公共教室{
//声明变量
国际货币市场;
国际禁止吸烟;
布尔占位符[];
//房间设计方法
公共房间(国际非、国际sm){
占用=新布尔值[sm+非];
//对于已占用阵列的循环
对于(int i=0;i<(sm+非);i++)
已占用[i]=false;//将每个已占用的房间设置为false或空
//初始化吸烟室和非吸烟室的数量
nummoking=sm;
不吸烟=不吸烟;
}//末端方法室
//预订房间的方法
公共内部书房(吸烟){
int开始,结束;
int roomNumber=0;
//非吸烟区和吸烟室预订的if声明
如果(!吸烟){
开始=0;
结束=不吸烟;
}//如果结束
否则{
开始=不吸烟;
结束=numSmoking+NUMNONSMOOKING;
}//结束其他
for(int i=begin;i
下面是我的主要类Module5example2:
import javax.swing.JOptionPane;
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
public class Module5example2 extends Frame implements
ActionListener{//
//creating color
Color lightRed = new Color(255, 90, 90);
Color lightGreen = new Color(140, 215, 40);
//variables
Rooms room = new Rooms(5, 3);
Panel roomPanel = new Panel();
TextArea roomDisplay[] = new TextArea[9];
Panel buttonPanel = new Panel();
Button bookButton = new Button("Book Room");
Panel inputPanel = new Panel();
Label custNameLabel = new Label("Name");
TextField nameField = new TextField(15);
Label custPhoneLabel = new Label("Phone Number");
TextField phoneField = new TextField(15);
Label numLabel = new Label("Number in Party");
Choice numberOfGuests = new Choice();
CheckboxGroup options = new CheckboxGroup();
Checkbox nonSmoking = new Checkbox("Nonsmoking",false,options);
Checkbox smoking = new Checkbox("Smoking",false,options);
Checkbox hidden = new Checkbox("",true,options);
//reservation method
public Module5example2(){// (B Reserv) constructor method
//set layouts for frame and three panels
this.setLayout(new BorderLayout());
roomPanel.setLayout(new GridLayout(2,4,10,10));
buttonPanel.setLayout(new FlowLayout());
inputPanel.setLayout(new FlowLayout());
//add components to room panel
for (int i=1; i<9; i++){ //(C for)
roomDisplay[i] = new TextArea(null,3,5,3);
if(i<6){
roomDisplay[i].setText("Room " + i + " Nonsmoking");
}
else{
roomDisplay[i].setText("Room " + i + " Smoking");
roomDisplay[i].setEditable(false);
roomDisplay[i].setBackground(lightGreen);
roomPanel.add(roomDisplay[i]);
}//end else
}//(C for closed)end for loop
//add components to button panel
buttonPanel.add(bookButton);
//add components to input label
inputPanel.add(custNameLabel);
inputPanel.add(nameField);
inputPanel.add(custPhoneLabel);
inputPanel.add(phoneField);
inputPanel.add(numLabel);
inputPanel.add(numberOfGuests);
for (int i = 8; i <= 20; i++)
numberOfGuests.add(String.valueOf(i));
inputPanel.add(nonSmoking);
inputPanel.add(smoking);
//add panels to frame
add(buttonPanel, BorderLayout.SOUTH);
add(inputPanel, BorderLayout.CENTER);
add(inputPanel, BorderLayout.NORTH);
bookButton.addActionListener(this);
//overriding the windowClosing() method will alow the user to click
//the close button
addWindowListener(
new WindowAdapter(){//
public void windowClosing(WindowEvent e){//
System.exit(0);
}//close window closing method
}// close window adapter
);
}//(end constructor method
//main
public static void main(String[] args){
Module5example2 f = new Module5example2();
f.setBounds(200,200,600,300);
f.setTitle("Reserve a Party Room");
f.setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(hidden.getState()){
JOptionPane.showMessageDialog(null," You must select Nonsmoking or
Smoking.",
"Error", JOptionPane.ERROR_MESSAGE);
}
else{
int available = room.bookRoom(smoking.getState());
if (available > 0){//(end 2nd if)
roomDisplay[available].setBackground(lightRed);
roomDisplay[available].setText(
roomDisplay[available].getText()+
"\n" +
nameField.getText() +
" " +
phoneField.getText() +
"\nparty of" +
numberOfGuests.getSelectedItem()
);
clearFields();
}
else{ //room is not available
if (smoking.getState())
JOptionPane.showMessageDialog(null, "Smoking is full.","Error",
JOptionPane.INFORMATION_MESSAGE);
else
JOptionPane.showMessageDialog(null, "Nonsmoking is full.",
"Error",
JOptionPane.INFORMATION_MESSAGE);
hidden.setState(true);
}
}
}
//reset the text field choice components
void clearFields(){
nameField.setText("");
phoneField.setText("");
numberOfGuests.select(0);
nameField.requestFocus();
hidden.setState(true);
}//end void clearfield
}//(A Class closed)end class rexervations
import javax.swing.JOptionPane;
导入java.awt.*;
导入java.awt.event.*;
导入java.awt.Color;
公共类Module5example2扩展了框架实现
ActionListener{//
//创造色彩
颜色浅红色=新颜色(255,90,90);
颜色浅绿色=新颜色(140、215、40);
//变数
房间=新房间(5,3);
配电盘室配电盘=新配电盘();
TextArea roomDisplay[]=新建TextArea[9];
面板按钮面板=新面板();
按钮bookButton=新按钮(“预订室”);
面板输入面板=新面板();
标签custnamelab=新标签(“名称”);
TextField nameField=新的TextField(15);
Label custPhoneLabel=新标签(“电话号码”);
TextField phoneField=新的TextField(15);
Label numLabel=新标签(“参与方编号”);
Choice numberOfGuests=新选项();
CheckboxGroup选项=新建CheckboxGroup();
复选框不吸烟=新复选框(“不吸烟”,假,选项);
复选框冒烟=新复选框(“冒烟”,假,选项);
复选框隐藏=新复选框(“”,true,选项);
//预约方式
public Module5example2(){//(B Reserv)构造函数方法
//设置框架和三个面板的布局
此.setLayout(新的BorderLayout());
roomPanel.setLayout(新网格布局(2,4,10,10));
buttonPanel.setLayout(新的FlowLayout());
inputPanel.setLayout(新的FlowLayout());
//将构件添加到房间配电盘
对于(int i=1;i您忘记将roomPanel添加到JFrame。您需要将面板添加到JFrame才能显示它。
仅仅创建一个JPanel实例并设置属性是没有帮助的
因此,您的代码需要修改为:
//add panels to frame
add(buttonPanel, BorderLayout.SOUTH);
add(inputPanel, BorderLayout.CENTER);
add(inputPanel, BorderLayout.NORTH);
add(roomPanel,BorderLayout.CENTER); //<----- You missed this.
//将面板添加到框架
添加(按钮面板,边界布局。南部);
添加(inputPanel,BorderLayout.CENTER);
添加(输入面板,BorderLayout.NORTH);
添加(roomPanel、BorderLayout.CENTER);//你的教科书有多长时间了?这段代码使用了Frame
,Panel
等等,它们是AWT的一部分,很久以前就被JFrame
,JPanel
等取代了,它们是Swing的一部分。这就是它不起作用的原因吗?在我的书中,它没有导入java.AWT.Color,我必须输入它才能得到它颜色。我使用的是text pad 7和最新的Java sdk和JRE。我使用的是Java编程:综合概念和技术第三版。2006年版权所有。它没有提到任何关于JFrame或JPanel的内容。了解这方面的最佳书籍是什么?不,代码在使用AWT时是一致的,所以这不是一个问题;混合了Swing和JPanelAWT组件可能会导致问题。哦,好吧。我只是尽我最大的努力了解它们是如何工作的。现在它只显示了3个房间面板,吸烟室从6到8。至少我现在有进展了…谢谢你的帮助。我会解决它的。
if(i<6){
roomDisplay[i].setText("Room " + i + " Nonsmoking");
}