Java的两按钮反向运动课程设计结构严谨,内容充实,我会做!~

package caida.xinxi.TestThread1;import java.awt.BorderLayout;import java.awt.Color;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;@SuppressWarnings("serial")public class MoveButtonWindow extends JFrame implements ActionListener{JButton startButton,stopButton,exitButton;JButton rightButton,leftButton;MoveToRight right;MoveToLeft left;public MoveButtonWindow(){setTitle("测试多线程主窗口");JPanel centerPanel=new JPanel();JPanel controlPanel=new JPanel();add(centerPanel,BorderLayout.CENTER );add(controlPanel,BorderLayout.SOUTH );startButton=new JButton("开始/继续");startButton.addActionListener(this);stopButton=new JButton("停止");stopButton.addActionListener(this);exitButton=new JButton("退出");exitButton.addActionListener(this);controlPanel.add(this.startButton );controlPanel.add(this.stopButton );controlPanel.add(this.exitButton );centerPanel.setLayout(null);centerPanel.setBackground(Color.white );rightButton=new JButton("向右移动");rightButton.setBackground(Color.yellow );rightButton.setBounds(0, 5, 100, 30);leftButton=new JButton("向左移动");leftButton.setBackground(Color.red );leftButton.setBounds(395,90,100,30);centerPanel.add(rightButton);centerPanel.add(leftButton);right=new MoveToRight(rightButton);left=new MoveToLeft(leftButton);setBounds(100,100,500,200);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );this.setResizable(false);setVisible(true);validate();}@Overridepublic void actionPerformed(ActionEvent e) {if(e.getSource()==this.startButton ){if(!right.isAlive()){right=new MoveToRight(rightButton);}if(!left.isAlive()){left=new MoveToLeft(leftButton);}right.start();left.start();}else if(e.getSource()==this.stopButton){right.setBoo(false);left.setBoo(false);}else if(e.getSource()==this.exitButton){right.setBoo(false);left.setBoo(false);System.exit(0);}}public static void main(String[] args) {new MoveButtonWindow();}}class MoveToRight extends Thread{JButton button;boolean boo=true;MoveToRight(JButton button){this.button=button;}public void run(){while(true){int x=button.getBounds().x;x=x+5;if(x>400){x=5;}button.setLocation(x, 5);try {Thread.sleep(200);} catch (InterruptedException e) {e.printStackTrace();}if(!boo){return;}}}public boolean isBoo() {return boo;}public void setBoo(boolean boo) {this.boo = boo;}}class MoveToLeft extends Thread{JButton button;boolean boo=true;MoveToLeft(JButton button){this.button=button;}public void run(){while(true){int x=button.getBounds().x;x=x-5;if(x<-20){x=395;}button.setLocation(x, 90);try {Thread.sleep(200);} catch (InterruptedException e) {e.printStackTrace();}if(!boo){return;}}}public boolean isBoo() {return boo;}public void setBoo(boolean boo) {this.boo = boo;}}

只是简单的改了一下,在爱好的“其他”中加入了一个用来输入的文本框,不知道你想改成什么样子的呢?类的名字最好首字母大写。package game;import java.awt.*;import java.awt.event.*;import javax.swing.*;public class survry extends JFrame implements ActionListener { private JPanel jp = new JPanel(); private JRadioButton man = new JRadioButton("男",true); private JRadioButton woman = new JRadioButton("女"); ButtonGroup sexBG = new ButtonGroup(); JLabel sexSTR = new JLabel("你的性别:"); JLabel likeSTR = new JLabel("你的爱好:"); JLabel ageSTR = new JLabel("你的年龄:"); private JCheckBox[] jcbArray = {new JCheckBox("灌水"), new JCheckBox("游戏"), new JCheckBox("发呆"), new JCheckBox("旅游"), new JCheckBox("其他")}; private JRadioButton[] jrbArray = {new JRadioButton("小学毕业"), new JRadioButton("亭亭玉立", true), new JRadioButton("而立之年"), new JRadioButton("大展宏图"), new JRadioButton("涛声依旧")}; private JButton[] jbArray = {new JButton("提交"), new JButton("清空")}; private JLabel[] jlArray = {new JLabel("年龄段:"), new JLabel("兴趣爱好:"), new JLabel("调查的结果为:")}; private JTextField otherTF = new JTextField(); private JTextField jtf = new JTextField(); private ButtonGroup bg = new ButtonGroup(); boolean isViewOtherTF = false;public survry() { jp.setLayout(null); sexBG.add(man); sexBG.add(woman); man.setBounds(100, 20, 50, 30); woman.setBounds(150, 20, 50, 30); jp.add(man); jp.add(woman); sexSTR.setBounds(30, 20, 75, 30); jp.add(sexSTR); likeSTR.setBounds(30, 50, 75, 30); jp.add(likeSTR); ageSTR.setBounds(30, 80, 75, 30); jp.add(ageSTR); for (int i = 0; i < jcbArray.length; i++) { jcbArray[i].setBounds(60 * i + 100, 50, 60, 30); jp.add(jcbArray[i]); } otherTF.setBounds(410, 50, 100, 22); jp.add(otherTF); otherTF.setVisible(false); jcbArray[jcbArray.length - 1].addActionListener(this); for (int i = 0; i < jrbArray.length; i++) { jrbArray[i].setBounds(90 * i + 100, 80, 90, 30); jp.add(jrbArray[i]); bg.add(jrbArray[i]); } jbArray[0].setBounds(30, 110, 80, 30); jp.add(jbArray[0]); jbArray[1].setBounds(120, 110, 80, 30); jp.add(jbArray[1]); jbArray[0].addActionListener(this); jbArray[1].addActionListener(this); jtf.setBounds(120, 150, 500, 26); jp.add(jtf); jtf.setEditable(false); this.add(jp); this.setTitle("个人信息调查表"); this.setBounds(100, 100, 700, 280); this.setVisible(true); this.setResizable(false); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent e) { if(e.getSource() == jcbArray[jcbArray.length - 1]){ if(isViewOtherTF){ otherTF.setVisible(false); isViewOtherTF = false; }else{ otherTF.setVisible(true); isViewOtherTF = true; } }if (e.getSource() == jbArray[1]) { if(isViewOtherTF){ otherTF.setVisible(false); isViewOtherTF = false; } for (int i = 0; i < jcbArray.length; i++){ jcbArray[i].setSelected(false); } jtf.setText(""); otherTF.setText(""); sexBG.setSelected(man.getModel(),true); bg.setSelected(jrbArray[1].getModel(),true); } if (e.getSource() == jbArray[0]) { StringBuffer temp1 = new StringBuffer("你是一个"); StringBuffer temp2 = new StringBuffer(); for (int i = 0; i < 5; i++) { if (jrbArray[i].isSelected()) { temp1.append(jrbArray[i].getText()); } if (jcbArray[i].isSelected()) { if (i == 4) { temp2.append(otherTF.getText()); } else { temp2.append(jcbArray[i].getText() + ","); } } } if (temp2.length() == 0) { jtf.setText("难道你没有爱好?"); } else { temp1.append("的人,你比较喜欢"); temp1.append(temp2.substring(0, temp2.length() - 1)); jtf.setText(temp1.append("。").toString()); } } } public static void main(String[] args) { new survry(); }} 本回答由提问者推荐

这位兄台,程序不是这样搞的,而且我觉的你的这个东西不难啊,怎么会有这种想法呢,找人帮你做,你要是遇到什么问题了,我想有很多人愿意帮你的,你这样没人会帮你的!自己动手写写吧,你不写永远不会,相信自己!

呵呵。。。。。。。。。。。。。

可以依靠百度Hi联系我你的问题有时间可以解决你面临的问题针对你的需求我们可以提供一套适用于初学者的源码如果你有同样的要求也可以通知我ES:\\A24D1C24792C1C37106CE217EC25A253交易提醒:预付定金是陷阱交易提醒:勿轻信百度名中的联系方式

代码:package FrameText;import java.awt.*;import java.awt.event.*;import javax.swing.*;public class FrameDemo extends JFrame { JButton jbtwo ,jbHello, jbBye; public FrameDemo() { setLayout(new GridLayout(3, 1));// 3行1列布局 JPanel jp1 = new JPanel();// 第一行 JButton jb1 = new JButton("第一个按钮"); JButton jb2 = new JButton("第二个按钮"); JButton jb3 = new JButton("第三个按钮"); jp1.add(jb1); jp1.add(jb2); jp1.add(jb3); add(jp1); JPanel jp2 = new JPanel();// 第二行 JButton jbtwo = new JButton("第四个按钮"); jp2.add(jbtwo); add(jp2); JPanel jp3 = new JPanel();// 第三行 jbHello = new JButton("第五个按钮"); jbBye = new JButton("第六个按钮"); jp3.add(jbHello); jp3.add(jbBye); add(jp3); setSize(380, 180); setTitle("窗口"); setLocationRelativeTo(null); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); } //点击按钮后响应 public static void main(String[] args) { new FrameDemo(); } }

用布局管理器

是用Swing写个JFrame吗?

你的程序我帮你改完了,你看看吧.(运行后能达到你的要求)import java.awt.*;import java.applet.Applet;import javax.swing.*;import java.awt.event.*;public class NewClass extends Applet implements ActionListener{  JButton jr1;  JButton jr2;  String result="";  int n;  JPanel jp1=new JPanel(); MyPanel jp2=new MyPanel(); public void init(){  setSize(300,300);  setLayout(new BorderLayout());  jr1=new JButton("circle");       jr2=new JButton("square");  jp1.add(jr1);    jp1.add(jr2);  jp1.setOpaque(false);  add(jp1,BorderLayout.NORTH);  add(jp2,BorderLayout.CENTER);  jr1.addActionListener(this);     jr2.addActionListener(this); }     public void actionPerformed(ActionEvent e){                  if(e.getSource()==jr1){            n=1;   repaint();  }  if(e.getSource()==jr2 ){       n=2;   repaint();  } } class MyPanel extends JPanel{  public void paint(Graphics g){           showGraphics(g);      }    public void showGraphics( Graphics g){   switch(n){               case 1:                    g.setColor(Color.blue);                    g.drawOval(30,30,160,160);                    break;               case 2:                    g.setColor(Color.orange);                    g.fillRoundRect(30,30,160,160,20,120);                    break;           }   } }} 追问 非常赞!!!但是请问一下Jpanel是什么意思? add(jp1,BorderLayout.NORTH); add(jp2,BorderLayout.CENTER);这句啥用呢?~3~你真是帮了我大忙!!!我从8点写到12点半还在苦恼!!大神等等加好友哟!!! 追答 JPanel是javax.swing.JPanel中的类,是java.awt.Panel类的轻量化类.add(jp1,BorderLayout.NORTH);是把jp1放在整个页面的北面(整个页面的布局是边界布局)add(jp2,BorderLayout.CENTER);是把jp2放在整个页面的中间(整个页面的布局是边界布局) 本回答由提问者推荐