【问题标题】:Concat a Title "GetTitle().Concat(GetTitle()));"连接标题“GetTitle().Concat(GetTitle()));”
【发布时间】:2015-09-10 03:23:48
【问题描述】:

今天我尝试在 Java 上做一个“窗口”的例子。我尝试连接标题,但我的“GetTitle()”不起作用!任何人都可以帮助我吗?

以及为什么“公共类 MiVentana 扩展 JFrame {”和“MiVentana Frame = new MiVentana("Titulo");”说警告?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MiVentana extends JFrame {

public MiVentana (String Titulo){
    this.setTitle(Titulo);
    this.setSize(300,400);
    this.setLocation(160,80);
    this.setLayout(new FlowLayout());
    this.ConfigurarVentana();
    this.setVisible(true);
}

public void ConfigurarVentana(){
    JPanel panel = new JPanel();
    JButton boton = new JButton ("OK");
    boton.addActionListener(new EscuchadorBoton());
    panel.add(boton);
    this.add(panel);
}

class EscuchadorBoton implements ActionListener{
    public void actionPerformed(ActionEvent a){
        this.setTitle(this.getTitle().concat(this.getTitle()));
    }

}
public static void main(String[] args) {
    MiVentana Frame = new MiVentana("Titulo");
    //frameTest.setVisible(true);
            }
    }

编辑:我正在开发 Ubuntu 14.04 IDE Eclipse 3.8

【问题讨论】:

  • 您收到的错误和警告是什么?
  • 试试MiVentana.this.getTitle()
  • @JavaNut13 它给了我一个错误(SetTitle 在同一行),我不写这个“当我按下按钮“OK”“Titulo”必须与另一个“ Titulo”所以“TituloTitulo”
  • @sᴜʀᴇsʜᴀᴛᴛᴀ MiVentana 说“可序列化的类 MiVentana 没有声明 long 类型的静态最终 serialVersionUID 字段”。而Frame“局部变量frame的值没有被使用”。
  • @Dash95 看看我的回答。

标签: java swing jframe awt concat


【解决方案1】:

ActionListener 中使用this 是指EscuchadorBoton 侦听器,而不是MiVentana 的实例——您的JFrame

使用MiVentana.this 应该指的是窗口,而不是侦听器,您将能够使用它来获取和设置标题。

This post 更好地描述了正在发生的事情 - 基本上你希望 this 来自封闭类,而不是封闭类。

基本上不是这样做:

this.setTitle(this.getTitle().concat(this.getTitle()));

你需要这样做:

MiVentana.this.setTitle(MiVentana.this.getTitle().concat(MiVentana.this.getTitle()));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多