小编ndf*_*kfh的帖子

如何将MouseListener添加到框架

我想在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)

java swing jpanel jframe mouselistener

6
推荐指数
1
解决办法
4万
查看次数

当我改变字体时 JTextField 大小改变

当我更改 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)

java fonts swing jtextfield flowlayout

0
推荐指数
1
解决办法
5650
查看次数

标签 统计

java ×2

swing ×2

flowlayout ×1

fonts ×1

jframe ×1

jpanel ×1

jtextfield ×1

mouselistener ×1