gpt4 book ai didi

java - 单击按钮时刷新面板

转载 作者:行者123 更新时间:2023-12-02 04:18:35 26 4
gpt4 key购买 nike

我有一个包含 2 个其他面板(显示随机数)和一个“下一步”按钮的 MainPanel。当我单击下一步按钮时,我想显示另一种数字组合。我无法实现这一目标。

我的挥杆水平一般。我知道如何使用面板、按钮和监听器(不确定)。但如何链接它们不!!!

我的代码:

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

}

最佳答案

我稍微清理了你的代码。这是您的 GUI。

Game GUI

我向您的下一个按钮添加了一个 Action 监听器。我确保您的 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();
}

}
}

关于java - 单击按钮时刷新面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33044832/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com