我创建了一个程序,该程序生成的数字多于我们在文本行中输入的数字。告诉我如何在该程序中添加业务逻辑层。
main.java
public class Main {
public static void main (String[]argc){
GraphicalUserInterface gui = new GraphicalUserInterface();
gui.setVisible(true);
gui.setResizable(false);
}
}
GraphicalUserInterface.java
import java.awt.*;
import javax.swing.*;
import java.util.concurrent.ThreadLocalRandom;
public class GraphicalUserInterface extends JFrame {
int anInt;
private JButton buttonMore = new JButton("Генерировать больше");
private JButton buttonLess = new JButton("Генерировать меньше");
private JTextField textField = new JTextField("", 5);
private JLabel labelOne = new JLabel("254");
private JLabel labelTwo = new JLabel("125");
private JLabel labelZero = new JLabel("");
public GraphicalUserInterface() {
super("Lab1_GUI");
this.setBounds(100, 100, 400, 150);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container container = this.getContentPane();
container.setLayout(new GridLayout(3, 10, 10, 10));
container.add(textField);
container.add(labelZero);
buttonMore.addActionListener(event -> {
anInt = Integer.parseInt(textField.getText());
labelOne.setText(String.valueOf
(ThreadLocalRandom.current().nextInt(anInt, 1000)));
});
container.add(buttonMore);
container.add(labelOne);
buttonLess.addActionListener(event -> {
anInt = Integer.parseInt(textField.getText());
labelTwo.setText(String.valueOf
(ThreadLocalRandom.current().nextInt(anInt)));
});
container.add(buttonLess);
container.add(labelTwo);
}
}
目前尚不清楚如何向其添加业务逻辑