单击按钮时刷新面板

时间:2015-10-09 18:20:53

标签: java swing

我有一个MainPanel包含2个其他面板(显示随机数)和一个Next Button。当我点击下一个按钮时,我想显示另一个数字组合。我无法实现这一点。

我的平均水平。我知道如何使用Panel,Button和Listener(不确定)。但是如何将它们联系起来!!!

我的代码:

public class Test extends JFrame implements MouseListener {


  JPanel mainPanel = new JPanel(new GridLayout(1, 1, 5, 5));
  JPanel panel1 = new JPanel();
  JPanel panel2 = new JPanel();
  JLabel label1 = new JLabel();
  JLabel label2 = new JLabel();
  JPanel buttonPanel = new JPanel();
  JButton next = new JButton();

public void randomNumber(){
    int randomNumber1 = 0;
    int randomNumber2 = 0;
    randomNumber1 = 1 + (int) (Math.random() * 9);
    randomNumber2 = 1 + (int) (Math.random() * 9);

    label1.setText(String.valueOf(randomNumber1));
    label2.setText(String.valueOf(randomNumber2));        
}


public Test(){

   panel1.setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));
   panel2.setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));
   panel1.setBackground(Color.red);
   panel2.setBackground(Color.yellow);    

   panel1.add(label1);
   panel2.add(label2);       

   next.setText("Next");

   mainPanel.add(panel1);
   mainPanel.add(panel2);
   buttonPanel.add(next);

   this.add(mainPanel, BorderLayout.NORTH);
   this.add(buttonPanel,BorderLayout.CENTER);

   initUI();
}

/**
 *
 */
public final void initUI()
{
    setTitle("Test");
    setSize(800, 400);
    setLocationRelativeTo(null);
    setVisible(true);
    this.setResizable(false);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public void actionPerformed(ActionEvent event) {
Object source = event.getSource();
  if (source instanceof JButton) randomNumber()  ;
}


/**
 *
 * @param args
 */
public static void main(String[] args){
      Test game = new Test();
      game.setVisible(true);
    }    

}

2 个答案:

答案 0 :(得分:1)

您不应该实施TFDBatchMove您应该实施MouseListener

ActionListener构造函数的末尾,您应该添加

Test

答案 1 :(得分:1)

我清理了一下你的代码。这是你的GUI。

Game GUI

我在你的下一个按钮上添加了一个动作监听器。我确保您的JFrame代码以正确的顺序执行。

以下是代码:

package com.ggl.testing;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Test27 implements Runnable {

    JPanel mainPanel = new JPanel(new GridLayout(1, 1, 5, 5));
    JPanel panel1 = new JPanel();
    JPanel panel2 = new JPanel();
    JLabel label1 = new JLabel();
    JLabel label2 = new JLabel();
    JPanel buttonPanel = new JPanel();
    JButton next = new JButton();

    @Override
    public void run() {
        JFrame frame = new JFrame();
        frame.setTitle("Test");
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        panel1.setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));
        panel2.setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));
        panel1.setBackground(Color.red);
        panel2.setBackground(Color.yellow);

        panel1.add(label1);
        panel2.add(label2);

        next.setText("Next");
        next.addActionListener(new RandomNumber());

        mainPanel.add(panel1);
        mainPanel.add(panel2);
        buttonPanel.add(next);

        frame.add(mainPanel, BorderLayout.NORTH);
        frame.add(buttonPanel, BorderLayout.CENTER);

        randomNumber();

        frame.pack();
        frame.setVisible(true);
    }

    public void randomNumber() {
        int randomNumber1 = 0;
        int randomNumber2 = 0;
        randomNumber1 = 1 + (int) (Math.random() * 9);
        randomNumber2 = 1 + (int) (Math.random() * 9);

        label1.setText(String.valueOf(randomNumber1));
        label2.setText(String.valueOf(randomNumber2));
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Test27());
    }

    public class RandomNumber implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent event) {
            randomNumber();
        }

    }
}