我想在mt JFrame框架中添加一个mouselistener,但是当我执行frame.addMouseListener(this)时,我得到一个错误,我无法在静态方法中使用它
我正在创建一个应用程序来检测鼠标点击,然后在int点击中显示它
码
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class numberOfClicks implements MouseListener{
static int clicks = 0;
@Override
public void mouseClicked(MouseEvent e) {
clicks++;
}
static JTextField text = new JTextField();
static String string = clicks+" Clicks";
static JFrame frame = new JFrame("Click Counter");
public static void frame(){
Font f = new Font("Engravers MT", Font.BOLD, 23);
text.setEditable(false);
text.setBackground(Color.BLUE);
text.setFont(f);
text.setForeground(Color.GREEN);
text.setBorder(BorderFactory.createLineBorder(Color.BLUE));
text.setText(string);
frame.add(text, BorderLayout.SOUTH);
frame.setResizable(false); …Run Code Online (Sandbox Code Playgroud) 当我更改 JTextField 的字体时,实际的 JTextField 会调整大小,有什么帮助吗?
我正在研究一个计算器,显然基于代码有多糟糕你可以告诉我我是一个菜鸟
任何帮助,将不胜感激
代码:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JFrame;
javax.swing.JTextField;
public class Ken {
static JTextField text = new JTextField("0",37);
public static void frame(){
JFrame frame = new JFrame("MyCalc");
frame.setSize(460, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setResizable(false);
FlowLayout fl = new FlowLayout(0, 30, 20);
setLayout(fl);
frame.getContentPane().setLayout(fl);
Container con = frame.getContentPane();
con.setBackground(Color.LIGHT_GRAY);
Font f = new Font("MS UI Gothic", Font.BOLD, 40);
Font f2 = new Font("Engravers MT", …Run Code Online (Sandbox Code Playgroud)