我似乎无法让这个继承工作

时间:2014-08-28 04:29:25

标签: java inheritance setter

我有一个标签式应用程序。其中一个选项卡可以输入公司名称,它应该将其更改为您在其他选项卡中输入的内容。这是两个班级。

在这段代码上,它告诉我改变About.setCompanyName(str);静态

我看到的错误是"无法从类型About"

中对非静态方法SetCompanyName(String)进行静态引用
package CourseProject;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;

import javax.swing.*;

public class Options extends JPanel{

    private JLabel changeLabel;
    private JTextField changeName;
    private JButton setName;
    private JButton exitButton;

    public Options(){
        GridBagLayout gridbag = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.NORTH;
        setBackground(Color.WHITE);
        super.setLayout(gridbag);
        c.insets = new Insets(10, 10, 10, 10);

        changeLabel = new JLabel("Change Company Name:");
        changeName = new JTextField("", 10);
        setName = new JButton("Set New Name");
        exitButton = new JButton("Exit");       

        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = 2;
        add(changeLabel, c);        

        c.gridx = 0;
        c.gridy = 1;    
        add(changeName, c);     

        c.gridx = 0;
        c.gridy = 2;
        c.gridwidth = 1;
        add(setName, c);
        setName.addActionListener(new setNameAction());

        c.gridx = 1;
        c.gridy = 2;    
        add(exitButton, c);
        exitButton.addActionListener(new exitApp());
        exitButton.setSize(40,40);      
    }
    class setNameAction implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {

            String str;
            str = changeName.getText();
            About.SetCompanyName(str);
            changeName.setText("");
        }

    }
    class exitApp implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            System.exit(0);
        }
    }
}

这是"关于"其中包含我的二传手。它要求我使方法和变量静态但我知道这不会工作,因为我想要改变它

package CourseProject;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JLabel;
import javax.swing.JPanel;

public class About extends JPanel{

    private JLabel programInfoLabel;
    private JLabel programInfo;
    private JLabel programmerLabel;
    private JLabel programmer;
    private JLabel companyLabel;
    JLabel company;
    public String companyName = "enter a company name in options";

    public About() {        

        GridBagLayout gridbag = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.NORTH;
        setBackground(Color.WHITE);
        super.setLayout(gridbag);
        c.insets = new Insets(10, 10, 10, 10);

        programInfoLabel = new JLabel("Program Information:");
        programInfo = new JLabel("This is the CIS355A course project application");
        programmerLabel = new JLabel("Programmer:");
        programmer = new JLabel("Kevin Rankin");
        companyLabel = new JLabel("Company Name:");
        company = new JLabel(companyName);

        c.gridx = 0;
        c.gridy = 0;    
        add(programInfoLabel, c);

        c.gridx = 1;
        c.gridy = 0;    
        add(programInfo, c);

        c.gridx = 0;
        c.gridy = 1;
        add(programmerLabel, c);

        c.gridx = 1;
        c.gridy = 1;
        add(programmer, c);

        c.gridx = 0;
        c.gridy = 2;
        add(companyLabel, c);

        c.gridx = 1;
        c.gridy = 2;
        add(company, c);
    }
    public void SetCompanyName(String str){
        company.setText(str);
    }   
}

1 个答案:

答案 0 :(得分:2)

在这一行

About.SetCompanyName(str);

您静态调用SetCompanyName(使用类名"关于")。您应该使方法静态(与#34;最终";您似乎对此感到困惑)不一样或首先创建About类的实例,如下所示:

About myAboutObject = new About();
myAboutObject.SetCompanyName(str);