Java 在方法上使用 setTitle

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21032891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:04:58  来源:igfitidea点击:

using setTitle on a method

javaswing

提问by Drilon Blakqori

I need to set the title from a method (not constructor). I tried doing it like this, but it's not working:

我需要从方法(不是构造函数)设置标题。我尝试这样做,但它不起作用:

import javax.swing.*;
import java.awt.*;
public class PointGraphWriter extends JPanel
{
   public String title;

   public void setTitle(String name)
   {
       title = name;
   }
   public PointGraphWriter()
   {
       JFrame frame = new JFrame;
       int width= 300;
       frame.setSize(width*3/2,width);
       frame.setVisible(true);
       frame.setTitle(title);
       frame.setBackground(Color.white);
       frame.getContentPane;
       frame.add(this);
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }
}

with the main method :

使用主要方法:

public class TestPlot
{
    public static void main(String[] a)
    { 
        PointGraphWriter e = new PointGraphWriter();
        e.setTitle("Graph of y = x*x");
    }
}

采纳答案by rgettman

You changed the variable titlebut that doesn't affect the frame. You will need to call setTitleon the frame again.

您更改了变量,title但这不会影响框架。您将需要setTitle再次调用框架。

Keep an instance variable for the frame:

为框架保留一个实例变量:

private JFrame frame;

In the constructor, assign the new JFrameto the instance variable, so you can change its title later in setTitle:

在构造函数中,将 new 分配给JFrame实例变量,以便稍后可以更改其标题setTitle

public void setTitle(String name)
{
   title = name;
   frame.setTitle(name);
}

回答by Michael Yaworski

You have a method to alter the variable title, which is good. The problem you are having is that you are trying to set the title of the frame in the constructor method.

你有一种改变变量的方法title,这很好。您遇到的问题是您试图在构造函数方法中设置框架的标题。

In this code:

在这段代码中:

PointGraphWriter e = new PointGraphWriter();
e.setTitle("Graph of y = x*x");

eis constructed beforeyou use the setTitlemethod to change the titlevariable in the PointGraphWriterclass. Therefore, you are trying to set the title of the frame to be a nullString because the setTitlemethod is only called after the constructor method.

e您使用该setTitle方法更改类中的title变量之前构造PointGraphWriter。因此,您尝试将框架的标题设置为null字符串,因为该setTitle方法仅在构造函数方法之后调用。

You could do two things:

你可以做两件事:

  1. Set the title of the frame in the setTitlemethod:

    JFrame frame = new JFrame;
    public void setTitle(String name)
    {
        frame.setTitle(name);
    }
    
  2. Or you could change your constructor method to take in the title as an argument:

    public PointGraphWriter(String title)
    {
        JFrame frame = new JFrame;
        int width= 300;
        frame.setSize(width*3/2,width);
        frame.setVisible(true);
        frame.setTitle(title);
        frame.setBackground(Color.white);
        frame.getContentPane;
        frame.add(this);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    

    And then create PointGraphWriterlike this:

    PointGraphWriter e = new PointGraphWriter("Graph of y = x*x");
    
  1. setTitle方法中设置框架的标题:

    JFrame frame = new JFrame;
    public void setTitle(String name)
    {
        frame.setTitle(name);
    }
    
  2. 或者您可以更改构造函数方法以将标题作为参数:

    public PointGraphWriter(String title)
    {
        JFrame frame = new JFrame;
        int width= 300;
        frame.setSize(width*3/2,width);
        frame.setVisible(true);
        frame.setTitle(title);
        frame.setBackground(Color.white);
        frame.getContentPane;
        frame.add(this);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    

    然后PointGraphWriter像这样创建:

    PointGraphWriter e = new PointGraphWriter("Graph of y = x*x");