当前位置: 代码迷 >> Java相关 >> [求助]关于面板布局问题
  详细解决方案

[求助]关于面板布局问题

热度:313   发布时间:2007-06-08 15:23:34.0
[求助]关于面板布局问题

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class lift extends JFrame{
public static void main(String []args){
SetFrame sf=new SetFrame();

sf.setSize(400,300);
sf.setTitle("电梯");
sf.setVisible(true);
sf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Action act=new Action(sf);

}
}
//电梯外部按扭的面板
class SetOutterButton extends JPanel{
JButton outbt[]=new JButton[10];
SetButtonPanel SetButton;
public SetOutterButton(){
setLayout(new GridLayout(6,2));
for(int i=0;i<10;i++){
outbt[i]=new JButton(""+(i+1));
add(outbt[i]);
}
}
}
//电梯内部按扭的面板
class SetInnerButton extends JPanel{
JButton inbt[]=new JButton[10];
SetButtonPanel SetButton;
public SetInnerButton(){
setLayout(new GridLayout(6,2));
for(int i=0;i<10;i++){
inbt[i]=new JButton(""+(i+1));
add(inbt[i]);
}
}
}
//电梯上方显示的面板
class SetShow extends JPanel{
JLabel jlb[]=new JLabel[10];
public SetShow(){
setLayout(new GridLayout(2,5));
for(int i=0;i<10;i++){
jlb[i]=new JLabel(""+(i+1)+"层");
jlb[i].setFont(new Font("",Font.BOLD,12));
add(jlb[i]);
}
}
}
//显示电梯进出人数的面板
class StringPanel extends JPanel{
JLabel num=new JLabel("选择人数");
CheckboxGroup cbg=new CheckboxGroup();

//这块面板谁能帮我设置下,我希望那个JLable独自一行 ,那十个checkbox分成两行
public StringPanel(){
num.setFont(new Font("宋体",Font.BOLD,20));
num.setForeground(Color.BLUE);
add(num);
add(new Checkbox("1",cbg,false));
add(new Checkbox("2",cbg,false));
add(new Checkbox("3",cbg,false));
add(new Checkbox("4",cbg,false));
add(new Checkbox("5",cbg,false));
add(new Checkbox("6",cbg,false));
add(new Checkbox("7",cbg,false));
add(new Checkbox("8",cbg,false));
add(new Checkbox("9",cbg,false));
add(new Checkbox("10",cbg,false));
}

}

class SetFrame extends JFrame{
Container con=getContentPane();
SetOutterButton outb=new SetOutterButton();
SetInnerButton inb = new SetInnerButton();
SetShow ss=new SetShow();
StringPanel sp=new StringPanel();
public SetFrame(){
con.setLayout(new BorderLayout(5,5));
con.add(ss,BorderLayout.NORTH);
con.add(inb,BorderLayout.WEST);
con.add(outb,BorderLayout.EAST);
con.add(sp,BorderLayout.CENTER);
}
}

class Action extends JFrame implements Runnable{
SetFrame sf;
Thread runner;
int floor=0;
int floor_end=1;
int person_num=0;
public Action(SetFrame stfm){
sf=stfm;
sf.ss.jlb[0].setForeground(Color.GREEN);


for(int i=0;i<10;i++){
sf.outb.outbt[i].addActionListener(new MyOutActionListener());
sf.outb.outbt[i].setActionCommand(""+(i+1));
}

for(int i=0;i<10;i++){
sf.inb.inbt[i].addActionListener(new MyInActionListener());
sf.inb.inbt[i].setActionCommand(""+(i+1));
}


}
class MyOutActionListener implements ActionListener{
public void actionPerformed(ActionEvent outae){
floor=Integer.parseInt(outae.getActionCommand());
sf.outb.outbt[floor-1].setForeground(Color.BLUE);
sf.sp.num.setText("门开了,请选选择进去人数");
threadStart();
}
}
class MyInActionListener implements ActionListener{
public void actionPerformed(ActionEvent ae){
floor=Integer.parseInt(ae.getActionCommand());
sf.inb.inbt[floor-1].setForeground(Color.BLUE);
sf.sp.num.setText("门开了,请选择出去人数");
threadStart();
}
}

public void threadStart(){
runner=new Thread(this);
runner.start();
}
public void run(){
while(true){
if(floor_end<floor){
up();
}else if(floor_end>floor){
down();
}else if(floor_end==floor){
stop();
}

}
}
public void up(){
pause(1000);
sf.ss.jlb[floor_end].setForeground(Color.GREEN);
sf.ss.jlb[floor_end-1].setForeground(Color.BLACK);
floor_end++;
}
public void down(){
pause(1000);
sf.ss.jlb[floor_end-2].setForeground(Color.GREEN);
sf.ss.jlb[floor_end-1].setForeground(Color.BLACK);
floor_end--;
}
public void stop(){
if(floor==1){
pause(1000);
floor_end=0;
sf.ss.jlb[0].setForeground(Color.GREEN);
sf.ss.jlb[1].setForeground(Color.BLACK);
floor_end=1;
}
sf.outb.outbt[floor-1].setForeground(Color.BLACK);
runner.stop();
}
public void pause(int time){
try{
runner.sleep(time);
}catch(InterruptedException ire){
System.err.println(ire.getMessage());
}
}
}
有谁能帮帮我吗?

搜索更多相关的解决方案: java  awt  import  面板  电梯  

----------------解决方案--------------------------------------------------------

[CODE]import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class lift extends JFrame{
public static void main(String []args){
SetFrame sf=new SetFrame();

sf.setSize(400,300);
sf.setTitle("电梯");
sf.setVisible(true);
sf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Action act=new Action(sf);

}
}
//电梯外部按扭的面板
class SetOutterButton extends JPanel{
JButton outbt[]=new JButton[10];
SetButtonPanel SetButton;
public SetOutterButton(){
setLayout(new GridLayout(6,2));
for(int i=0;i<10;i++){
outbt[i]=new JButton(""+(i+1));
add(outbt[i]);
}
}
}
//电梯内部按扭的面板
class SetInnerButton extends JPanel{
JButton inbt[]=new JButton[10];
SetButtonPanel SetButton;
public SetInnerButton(){
setLayout(new GridLayout(6,2));
for(int i=0;i<10;i++){
inbt[i]=new JButton(""+(i+1));
add(inbt[i]);
}
}
}
//电梯上方显示的面板
class SetShow extends JPanel{
JLabel jlb[]=new JLabel[10];
public SetShow(){
setLayout(new GridLayout(2,5));
for(int i=0;i<10;i++){
jlb[i]=new JLabel(""+(i+1)+"层");
jlb[i].setFont(new Font("",Font.BOLD,12));
add(jlb[i]);
}
}
}
//显示电梯进出人数的面板
class StringPanel extends JPanel{
JLabel num=new JLabel("选择人数");
CheckboxGroup cbg=new CheckboxGroup();
//这块面板谁能帮我设置下,我希望那个JLable独自一行 ,那十个checkbox分成两行
public StringPanel(){
this.setLayout(new BorderLayout());
num.setFont(new Font("宋体",Font.BOLD,20));
num.setForeground(Color.BLUE);
JPanel up=new JPanel();
JPanel center=new JPanel(new GridLayout(2,5,1,5));
up.add(num);
//这个面板是空的,设置它占一个位置而已
JPanel down=new JPanel();
down.setPreferredSize(new Dimension(80,130));
center.add(new Checkbox("1",cbg,false));
center.add(new Checkbox("2",cbg,false));
center.add(new Checkbox("3",cbg,false));
center.add(new Checkbox("4",cbg,false));
center.add(new Checkbox("5",cbg,false));
center.add(new Checkbox("6",cbg,false));
center.add(new Checkbox("7",cbg,false));
center.add(new Checkbox("8",cbg,false));
center.add(new Checkbox("9",cbg,false));
center.add(new Checkbox("10",cbg,false));
this.add(up,BorderLayout.NORTH);
this.add(center,BorderLayout.CENTER);
this.add(down,BorderLayout.SOUTH);
}

}
class SetFrame extends JFrame{
Container con=getContentPane();
SetOutterButton outb=new SetOutterButton();
SetInnerButton inb = new SetInnerButton();
SetShow ss=new SetShow();
StringPanel sp=new StringPanel();
public SetFrame(){
con.setLayout(new BorderLayout(5,5));
con.add(ss,BorderLayout.NORTH);
con.add(inb,BorderLayout.WEST);
con.add(outb,BorderLayout.EAST);
con.add(sp,BorderLayout.CENTER);
}
}
class Action extends JFrame implements Runnable{
SetFrame sf;
Thread runner;
int floor=0;
int floor_end=1;
int person_num=0;
public Action(SetFrame stfm){
sf=stfm;
sf.ss.jlb[0].setForeground(Color.GREEN);


for(int i=0;i<10;i++){
sf.outb.outbt[i].addActionListener(new MyOutActionListener());
sf.outb.outbt[i].setActionCommand(""+(i+1));
}

for(int i=0;i<10;i++){
sf.inb.inbt[i].addActionListener(new MyInActionListener());
sf.inb.inbt[i].setActionCommand(""+(i+1));
}


}
class MyOutActionListener implements ActionListener{
public void actionPerformed(ActionEvent outae){
floor=Integer.parseInt(outae.getActionCommand());
sf.outb.outbt[floor-1].setForeground(Color.BLUE);
sf.sp.num.setText("门开了,请选选择进去人数");
threadStart();
}
}
class MyInActionListener implements ActionListener{
public void actionPerformed(ActionEvent ae){
floor=Integer.parseInt(ae.getActionCommand());
sf.inb.inbt[floor-1].setForeground(Color.BLUE);
sf.sp.num.setText("门开了,请选择出去人数");
threadStart();
}
}

public void threadStart(){
runner=new Thread(this);
runner.start();
}
public void run(){
while(true){
if(floor_end<floor){
up();
}else if(floor_end>floor){
down();
}else if(floor_end==floor){
stop();
}

}
}
public void up(){
pause(1000);
sf.ss.jlb[floor_end].setForeground(Color.GREEN);
sf.ss.jlb[floor_end-1].setForeground(Color.BLACK);
floor_end++;
}
public void down(){
pause(1000);
sf.ss.jlb[floor_end-2].setForeground(Color.GREEN);
sf.ss.jlb[floor_end-1].setForeground(Color.BLACK);
floor_end--;
}
public void stop(){
if(floor==1){
pause(1000);
floor_end=0;
sf.ss.jlb[0].setForeground(Color.GREEN);
sf.ss.jlb[1].setForeground(Color.BLACK);
floor_end=1;
}
sf.outb.outbt[floor-1].setForeground(Color.BLACK);
runner.stop();
}
public void pause(int time){
try{
runner.sleep(time);
}catch(InterruptedException ire){
System.err.println(ire.getMessage());
}
}
}[/CODE]

改好了,自己还是应该多看看API

其实很多东西都是很基础的.好好学吧,不要好高骛远


----------------解决方案--------------------------------------------------------
还有,我提的意见你一点都没有接受

1,类名lift应该改为Lift

2,不要用stop方法停止线程,而是自己设置一个标量,因为stop方法已经过时了

3,编码风格,你还是添加了那么多的监听器,其实一个就可以了

好好努力吧
----------------解决方案--------------------------------------------------------
[CODE]public Action(SetFrame stfm){
sf=stfm;
sf.ss.jlb[0].setForeground(Color.GREEN);

MyOutActionListener mout=new MyOutActionListener();
for(int i=0;i<10;i++){
sf.outb.outbt[i].addActionListener(mout);
sf.outb.outbt[i].setActionCommand(""+(i+1));
}
MyInActionListener min=new MyInActionListener();
for(int i=0;i<10;i++){
sf.inb.inbt[i].addActionListener(min);
sf.inb.inbt[i].setActionCommand(""+(i+1));
}


}[/CODE]

这个改一下会很难吗?

这样的话,你只生成了两个对象,而以前你却生成了二十个对象
----------------解决方案--------------------------------------------------------
斑竹真好,让我大开眼界。
----------------解决方案--------------------------------------------------------
  相关解决方案