简体   繁体   中英

Refresh panel when a button in clicked

I have a MainPanel containing 2 other panels(show random number) and a Next Button. When I click the next button, I want to show another combination of numbers. I'm unable to achive that.

I'm average in swing. I know how to use Panel, Button and Listener(not sure). But how to link them no!!!.

My Code:

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);
    }    

}

You should not implement MouseListener You should implement ActionListener instead.

And at the end of your Test constructor you should add

randomNumber(); // Show the number when the application starts
next.addActionListener(this); // actionPerformed will now recieve signals from 'next' button

I cleaned up your code a bit. Here's your GUI.

游戏GUI

I added an action listener to your next button. I made sure your JFrame code was executed in the correct order.

Here's the code:

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();
        }

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM