Java 我在哪里实现我的功能?
我正在尝试用JavaSwing来练习我的OOP技能,但我现在被卡住了。我正在尝试制作一个类似于你在手机上看到的计算器gui。我不知道我应该如何实现每个按键的功能。现在我只想在按下按钮时在屏幕上显示数字(JLabel对象)。我还附上了一张到目前为止我所拥有的GUI的图片 我应该在一个单独的.java文件中实现这些函数吗?或者应该在Calculator.java或Keyboard.java文件中实现它们 另外,它们是如何实现的,因为如果我的按钮对象在Keyboard.java文件中,我不知道如何在Calculator.java文件中的JLabel对象上显示 Calculator.javaJava 我在哪里实现我的功能?,java,swing,oop,calculator,Java,Swing,Oop,Calculator,我正在尝试用JavaSwing来练习我的OOP技能,但我现在被卡住了。我正在尝试制作一个类似于你在手机上看到的计算器gui。我不知道我应该如何实现每个按键的功能。现在我只想在按下按钮时在屏幕上显示数字(JLabel对象)。我还附上了一张到目前为止我所拥有的GUI的图片 我应该在一个单独的.java文件中实现这些函数吗?或者应该在Calculator.java或Keyboard.java文件中实现它们 另外,它们是如何实现的,因为如果我的按钮对象在Keyboard.java文件中,我不知道如何在C
package calculator;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Calculator extends JFrame
{
public static void main(String[] args)
{
new Calculator();
}
public Calculator() //Calculator constructor??
{
setLayout(new GridLayout(2,1));
this.setSize(400,600);
this.setLocationRelativeTo(null); //center the window
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel display = new JLabel();
this.add(display);
Keyboard kb = new Keyboard();
this.add(kb);
this.setVisible(true);
}
}
package calculator;
import javax.swing.JPanel;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
public class Keyboard extends JPanel implements ActionListener
{
public Keyboard()
{
setLayout(new GridLayout(4,4));
JButton one = new JButton("1");
this.add(one);
JButton two = new JButton("2");
this.add(two);
JButton three = new JButton("3");
this.add(three);
JButton plus = new JButton("+");
this.add(plus);
JButton four = new JButton("4");
this.add(four);
JButton five = new JButton("5");
this.add(five);
JButton six = new JButton("6");
this.add(six);
JButton minus = new JButton("-");
this.add(minus);
JButton seven = new JButton("7");
this.add(seven);
JButton eight = new JButton("8");
this.add(eight);
JButton nine = new JButton("9");
this.add(nine);
JButton times = new JButton("x");
this.add(times);
JButton zero = new JButton("0");
this.add(zero);
JButton clear = new JButton("clear");
this.add(clear);
JButton equals = new JButton("=");
this.add(equals);
JButton divide = new JButton("/");
this.add(divide);
}
public void actionPerformed(ActionEvent e) {
}
}
Keyboard.java
package calculator;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Calculator extends JFrame
{
public static void main(String[] args)
{
new Calculator();
}
public Calculator() //Calculator constructor??
{
setLayout(new GridLayout(2,1));
this.setSize(400,600);
this.setLocationRelativeTo(null); //center the window
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel display = new JLabel();
this.add(display);
Keyboard kb = new Keyboard();
this.add(kb);
this.setVisible(true);
}
}
package calculator;
import javax.swing.JPanel;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
public class Keyboard extends JPanel implements ActionListener
{
public Keyboard()
{
setLayout(new GridLayout(4,4));
JButton one = new JButton("1");
this.add(one);
JButton two = new JButton("2");
this.add(two);
JButton three = new JButton("3");
this.add(three);
JButton plus = new JButton("+");
this.add(plus);
JButton four = new JButton("4");
this.add(four);
JButton five = new JButton("5");
this.add(five);
JButton six = new JButton("6");
this.add(six);
JButton minus = new JButton("-");
this.add(minus);
JButton seven = new JButton("7");
this.add(seven);
JButton eight = new JButton("8");
this.add(eight);
JButton nine = new JButton("9");
this.add(nine);
JButton times = new JButton("x");
this.add(times);
JButton zero = new JButton("0");
this.add(zero);
JButton clear = new JButton("clear");
this.add(clear);
JButton equals = new JButton("=");
this.add(equals);
JButton divide = new JButton("/");
this.add(divide);
}
public void actionPerformed(ActionEvent e) {
}
}
您应该在“Keyboard.java”文件本身中编写actionPerformed()方法 Keyboard.java
one.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
displaystring = displaystring + "1";
display.setText (displaystring);`
}
您必须通过键盘类的构造函数传递display的引用,并将其分配给该类的某个实例变量,以便您也可以在键盘类中更新display标签 您应该在'Keyboard.java'文件本身中编写actionPerformed()方法 Keyboard.java
one.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
displaystring = displaystring + "1";
display.setText (displaystring);`
}
您必须通过键盘类的构造函数传递display的引用,并将其分配给该类的某个实例变量,以便您也可以在键盘类中更新display标签 使用
MVC
模式,将进入actionPerformed(…)
方法的逻辑代码(或任何与此相关的逻辑代码)放入控制器类中的相应方法中
MVC
模式由三个类组成,模型
,视图
,和控件
,它们共同构成GUI
MVC
模式的简要概述,最初取自,但稍作修改:
MVC
模式的更详细、更深入的解释可以在上面的链接中找到,只需一个简单的谷歌搜索
我选择用于模型视图交互 使用
MVC
结构的程序示例:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
class Test {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
final Model model = new Model();
final Control control = new Control(model);
final View view = new View(model, control);
final JFrame frame = new JFrame("MVC Example");
frame.getContentPane().add(view);
frame.pack();
frame.setLocationByPlatform(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
static class Model extends Observable {
private int number;
void setNumber(int newValue) {
number = newValue;
setChanged();
notifyObservers(number);
}
int getNumber() {
return number;
}
}
static class View extends JPanel {
View(Model model, Control control) {
final JLabel label = new JLabel(Integer.toString(model.getNumber()));
final JButton button = new JButton("Click me!");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
control.buttonPressed();
}
});
setLayout(new BorderLayout());
add(label);
add(button, BorderLayout.SOUTH);
model.addObserver(new Observer() {
@Override
public void update(Observable o, Object arg) {
label.setText(Integer.toString((int) arg));
}
});
}
}
static class Control {
private Model model;
Control(Model model) {
this.model = model;
}
void buttonPressed() {
model.setNumber(model.getNumber() + 1);
}
}
}
关于代码的注释:
- 请查看和,以帮助您简化按钮的声明
- 在添加所有组件以使
适合其子项大小后,请在JFrame
上调用JFrame
,而不是设置pack()
的大小JFrame
- 不要扩展
,而是创建一个实例并进行必要的修改JFrame
- 在
(事件调度线程)上运行代码,以避免GUI中的“冻结”。有关更多信息,请参阅EDT
<> Ly>下一次你考虑投稿,为了更好的帮助,请尽快发布.
MVC
模式,将进入actionPerformed(…)
方法的逻辑代码(或与此相关的任何逻辑代码)放入控制器类中的相应方法中
MVC
模式由三个类组成,模型
,视图
,和控件
,它们共同构成GUI
MVC
模式的简要概述,最初取自,但稍作修改: