| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 733 人关注过本帖
标题:[求助]关于面板布局问题
只看楼主 加入收藏
eaglesnail
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2007-6-5
收藏
 问题点数:0 回复次数:4 
[求助]关于面板布局问题

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 面板 电梯 
2007-06-08 15:23
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 

[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

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


可惜不是你,陪我到最后
2007-06-08 15:37
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
还有,我提的意见你一点都没有接受

1,类名lift应该改为Lift

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

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

好好努力吧

可惜不是你,陪我到最后
2007-06-08 15:38
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
[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]

这个改一下会很难吗?

这样的话,你只生成了两个对象,而以前你却生成了二十个对象

可惜不是你,陪我到最后
2007-06-08 15:40
xuxianyue123
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2007-6-9
收藏
得分:0 
斑竹真好,让我大开眼界。
2007-06-09 16:13
快速回复:[求助]关于面板布局问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.023357 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved