一个很简单的连词游戏
左右各4个按钮 左边的是英文单词 右边的是中文解释
只要把左边的单词与右边匹配的中文释义连起来就好了
对GUI这块不了解 有高手能随便帮忙写上一个吗 或者给个类似的东西参考一下也行
多谢了

解决方案 »

  1.   


    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.HeadlessException;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.Box;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    public class Tian_ci extends JFrame{

    private JButton bt_eng;
    private JButton bt_chi;


    public Tian_ci() throws HeadlessException {
    super();
    init();

    this.add(bt_chi,BorderLayout.WEST);
    this.add(bt_eng,BorderLayout.EAST);
    this.add(Box.createHorizontalStrut(100));
    this.setSize(300, 150);


    this.setLocationRelativeTo(null);
    this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void init(){
    bt_chi=new JButton("英文");
    bt_chi.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) {
    if(e.getSource().equals(bt_chi)){

    Graphics g = getGraphics();
    g.setColor(Color.blue);
    g.drawLine(50, 100, 220, 100);


    }

    }




    });


    bt_eng=new JButton("English");



    }


    public static void main(String[] args) {
    // TODO 自动生成方法存根
    new Tian_ci(); }}
    简单写下,希望对你有所帮助。
      

  2.   

    太感谢了
    不过 如果我需要写很多按钮的时候,就不能用BorderLayout实现了是么
    是不是要用FlowLayout? 我试了一下 好像也不怎么对 能告诉我是怎样做的吗
    谢谢!!
      

  3.   

    可以用,把多个按钮用flowLayout放在一个panel里,再把panel放到borderLayout布局里。
      

  4.   

    能麻烦写个代码说明一下吗 我刚刚接触GUI 是个新手 谢谢~~~
      

  5.   

    借用下5楼bobolnear的代码,布局可以如下(具体的按钮事件没写):import java.awt.BorderLayout; 
    import java.awt.Color; 
    import java.awt.Graphics; 
    import java.awt.GridLayout;
    import java.awt.HeadlessException; 
    import java.awt.event.ActionEvent; 
    import java.awt.event.ActionListener; import javax.swing.Box; 
    import javax.swing.JButton; 
    import javax.swing.JFrame; 
    import javax.swing.JPanel;
    public class Tian_ci extends JFrame{ private JButton bt_en1; 
    private JButton bt_en2;
    private JButton bt_en3;
    private JButton bt_en4;
    private JButton bt_ch1; 
    private JButton bt_ch2; 
    private JButton bt_ch3; 
    private JButton bt_ch4; private JPanel panel_ch;
    private JPanel panel_en;public Tian_ci() throws HeadlessException { 
    super(); 
    init(); this.add(panel_ch,BorderLayout.WEST); 
    this.add(panel_en,BorderLayout.EAST); 
    this.add(Box.createHorizontalStrut(100)); 
    this.setSize(300, 150); 
    this.setLocationRelativeTo(null); 
    this.setVisible(true); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } public void init(){ 
    bt_ch1=new JButton("英文1"); 
    /*bt_ch1.addActionListener(new ActionListener(){ 
    public void actionPerformed(ActionEvent e) { 
    if(e.getSource().equals(bt_ch1)){ 

    Graphics g = getGraphics(); 
    g.setColor(Color.blue); 
    g.drawLine(50, 100, 220, 100); 


    }); */
    bt_ch2=new JButton("英文2"); 
    bt_ch3=new JButton("英文3"); 
    bt_ch4=new JButton("英文4");bt_en1=new JButton("English1"); 
    bt_en2=new JButton("English2");
    bt_en3=new JButton("English3");
    bt_en4=new JButton("English4");panel_ch = new JPanel(new GridLayout(4,1));panel_ch.add(bt_ch1,"1");
    panel_ch.add(bt_ch2,"2");
    panel_ch.add(bt_ch3,"3");
    panel_ch.add(bt_ch4,"4");panel_en = new JPanel(new GridLayout(4,1));panel_en.add(bt_en1,"1");
    panel_en.add(bt_en2,"2");
    panel_en.add(bt_en3,"3");
    panel_en.add(bt_en4,"4");} 
    public static void main(String[] args) { 
    // TODO 自动生成方法存根 
    new Tian_ci(); } }
      

  6.   

    1.选择使用合适的布局 可以用Grid或GridBag 以上有提到
    2.选择使用JToggleButton 可以保持按下状态 用以区分选择
    3.逻辑判断与操作启动基本都在按钮响应方法中执行