我需要從方法(而不是構造函數)中設置標題。我試圖做這樣的,但它不工作:在方法上使用setTitle
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);
}
}
與主要方法:
public class TestPlot
{
public static void main(String[] a)
{
PointGraphWriter e = new PointGraphWriter();
e.setTitle("Graph of y = x*x");
}
}
有了'setTitle',你想改變'title'變量或框架的標題? – rgettman
@rgettman標題變量,然後是該變量的框架標題。 –
rgettman下面的答案完全正確。 – csmckelvey