Java点击button事件问题!求救!
编写一个小程序,类似选房间的,一共5个button,每点一个会出现一个新的frame,每个frame就是一个房间,每个房间是一样的,只有房间号码不一样,程序如下,两个类:...
编写一个小程序,类似选房间的,一共5个button,每点一个会出现一个新的frame,每个frame就是一个房间,每个房间是一样的,只有房间号码不一样, 程序如下,两个类:
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Test extends JFrame implements ActionListener{
JButton buttonRoom;
static RunTest room[] = new RunTest[5];
static int[] roomID = {1,2,3,4,5};
public static void main(String args[]) {
new Test().launchFrame();
}
public void launchFrame() {
setSize(300, 200);
this.setTitle("choose room");
setLocation(400, 300);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLayout(new FlowLayout());
setVisible(true);
for (int i = 0; i < roomID.length; i++) {
buttonRoom = new JButton("Room" + i);
room[i] = new RunTest(roomID[i]);
add(buttonRoom);
}
buttonRoom.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==buttonRoom){
switch(5){
case 1:
room[1] = new RunTest(roomID[1]);
case 2:
room[2] = new RunTest(roomID[2]);
case 3:
room[3] = new RunTest(roomID[3]);
case 4:
room[4] = new RunTest(roomID[4]);
case 5:
room[5] = new RunTest(roomID[5]);
}
}
}
}
import javax.swing.JFrame;
public class RunTest extends JFrame {
int roomID;
public RunTest(int roomID){
this.roomID = roomID;
}
public void setRoomID(int roomID){
this.roomID = roomID;
}
public int getRoomID(){
return roomID;
}
public void room() {
setSize(300, 200);
this.setTitle("room X");
setLocation(400, 300);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLayout(null);
setVisible(true);
}
}
写的不对,谁帮我改改啊,思路是用for loop 在JFrame里循环加入5个按钮,每点一个按钮就出现一个房间,房间时一样的,所以不想写5个房间,写一个应该就能表示了, 每个房间有个id,请求大侠们帮小弟改改 小弟感激不尽 谢谢! 展开
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Test extends JFrame implements ActionListener{
JButton buttonRoom;
static RunTest room[] = new RunTest[5];
static int[] roomID = {1,2,3,4,5};
public static void main(String args[]) {
new Test().launchFrame();
}
public void launchFrame() {
setSize(300, 200);
this.setTitle("choose room");
setLocation(400, 300);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLayout(new FlowLayout());
setVisible(true);
for (int i = 0; i < roomID.length; i++) {
buttonRoom = new JButton("Room" + i);
room[i] = new RunTest(roomID[i]);
add(buttonRoom);
}
buttonRoom.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==buttonRoom){
switch(5){
case 1:
room[1] = new RunTest(roomID[1]);
case 2:
room[2] = new RunTest(roomID[2]);
case 3:
room[3] = new RunTest(roomID[3]);
case 4:
room[4] = new RunTest(roomID[4]);
case 5:
room[5] = new RunTest(roomID[5]);
}
}
}
}
import javax.swing.JFrame;
public class RunTest extends JFrame {
int roomID;
public RunTest(int roomID){
this.roomID = roomID;
}
public void setRoomID(int roomID){
this.roomID = roomID;
}
public int getRoomID(){
return roomID;
}
public void room() {
setSize(300, 200);
this.setTitle("room X");
setLocation(400, 300);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLayout(null);
setVisible(true);
}
}
写的不对,谁帮我改改啊,思路是用for loop 在JFrame里循环加入5个按钮,每点一个按钮就出现一个房间,房间时一样的,所以不想写5个房间,写一个应该就能表示了, 每个房间有个id,请求大侠们帮小弟改改 小弟感激不尽 谢谢! 展开
1个回答
展开全部
已改好:如下
下面是Test .java
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Test extends JFrame implements ActionListener{
JButton buttonRoom;
static RunTest room[] = new RunTest[5];
static int[] roomID = {1,2,3,4,5};
public static void main(String args[]) {
new Test().launchFrame();
}
public void launchFrame() {
setSize(300, 200);
this.setTitle("choose room");
setLocation(200, 300);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLayout(new FlowLayout());
setVisible(true);
for (int i = 0; i < roomID.length; i++) {
buttonRoom = new JButton( i+"");
room[i] = new RunTest(roomID[i]);
add(buttonRoom);
buttonRoom.addActionListener(this);
}
}
public void actionPerformed(ActionEvent e) {
System.out.println("sss"+ e.getActionCommand());
int a=Integer.parseInt(e.getActionCommand());
// room[0] = new RunTest(a+1); 这是我自己写的,我觉得也行,下面是在你的基础上修改的
// room[0].room();
switch(a){
case 1:
room[0] = new RunTest(roomID[1]);room[0].room();break;
case 2:
room[1] = new RunTest(roomID[2]);room[1].room();break;
case 3:
room[2] = new RunTest(roomID[3]);room[2].room();break;
case 4:
room[3] = new RunTest(roomID[4]);room[3].room();break;
case 5:
room[4] = new RunTest(roomID[5]);room[4].room();break;
}
}
}
-----------------------------------------------------------------------------------------------------------
以下是RunTest .java
import javax.swing.JFrame;
public class RunTest extends JFrame {
int roomID;
public RunTest(int roomID){
this.roomID = roomID;
}
public void setRoomID(int roomID){
this.roomID = roomID;
}
public int getRoomID(){
return roomID;
}
public void room() {
setSize(400, 300);
this.setTitle("room"+roomID);
setLocation(600+roomID*4, 300+roomID*6);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLayout(null);
setVisible(true);
}
}
提醒两点,一是switch case用法后要加break;二是数组的坐标是从0开始的
下面是Test .java
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Test extends JFrame implements ActionListener{
JButton buttonRoom;
static RunTest room[] = new RunTest[5];
static int[] roomID = {1,2,3,4,5};
public static void main(String args[]) {
new Test().launchFrame();
}
public void launchFrame() {
setSize(300, 200);
this.setTitle("choose room");
setLocation(200, 300);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLayout(new FlowLayout());
setVisible(true);
for (int i = 0; i < roomID.length; i++) {
buttonRoom = new JButton( i+"");
room[i] = new RunTest(roomID[i]);
add(buttonRoom);
buttonRoom.addActionListener(this);
}
}
public void actionPerformed(ActionEvent e) {
System.out.println("sss"+ e.getActionCommand());
int a=Integer.parseInt(e.getActionCommand());
// room[0] = new RunTest(a+1); 这是我自己写的,我觉得也行,下面是在你的基础上修改的
// room[0].room();
switch(a){
case 1:
room[0] = new RunTest(roomID[1]);room[0].room();break;
case 2:
room[1] = new RunTest(roomID[2]);room[1].room();break;
case 3:
room[2] = new RunTest(roomID[3]);room[2].room();break;
case 4:
room[3] = new RunTest(roomID[4]);room[3].room();break;
case 5:
room[4] = new RunTest(roomID[5]);room[4].room();break;
}
}
}
-----------------------------------------------------------------------------------------------------------
以下是RunTest .java
import javax.swing.JFrame;
public class RunTest extends JFrame {
int roomID;
public RunTest(int roomID){
this.roomID = roomID;
}
public void setRoomID(int roomID){
this.roomID = roomID;
}
public int getRoomID(){
return roomID;
}
public void room() {
setSize(400, 300);
this.setTitle("room"+roomID);
setLocation(600+roomID*4, 300+roomID*6);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLayout(null);
setVisible(true);
}
}
提醒两点,一是switch case用法后要加break;二是数组的坐标是从0开始的
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询