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
using setTitle on a method
提问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 title
but that doesn't affect the frame. You will need to call setTitle
on the frame again.
您更改了变量,title
但这不会影响框架。您将需要setTitle
再次调用框架。
Keep an instance variable for the frame:
为框架保留一个实例变量:
private JFrame frame;
In the constructor, assign the new JFrame
to 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");
e
is constructed beforeyou use the setTitle
method to change the title
variable in the PointGraphWriter
class. Therefore, you are trying to set the title of the frame to be a null
String because the setTitle
method is only called after the constructor method.
e
在您使用该setTitle
方法更改类中的title
变量之前构造PointGraphWriter
。因此,您尝试将框架的标题设置为null
字符串,因为该setTitle
方法仅在构造函数方法之后调用。
You could do two things:
你可以做两件事:
Set the title of the frame in the
setTitle
method:JFrame frame = new JFrame; public void setTitle(String name) { frame.setTitle(name); }
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
PointGraphWriter
like this:PointGraphWriter e = new PointGraphWriter("Graph of y = x*x");
在
setTitle
方法中设置框架的标题:JFrame frame = new JFrame; public void setTitle(String name) { frame.setTitle(name); }
或者您可以更改构造函数方法以将标题作为参数:
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");