简体   繁体   English

我似乎无法使这种继承起作用

[英]I can't seem to get this inheritance to work

I have a tabbed application. 我有一个选项卡式应用程序。 One of the tabs you can enter the name of the company and it should change it to whatever you typed into the other tab. 您可以在其中一个标签中输入公司名称,并且公司名称应更改为您在另一个标签中键入的名称。 Here are both classes. 这是两个类。

On this code its telling me to change About.setCompanyName(str); 在此代码上,它告诉我更改About.setCompanyName(str);。 to static 静态

The error that I am seeing is "Cannot make a static reference to the non-static method SetCompanyName(String) from the type About" 我看到的错误是“无法从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);
        }
    }
}

And here is "About" which contains my setter. 这是“关于”,其中包含我的二传手。 It asks me to make the method and variable static but I know this wont work because I am wanting to change it 它要求我将方法和变量设为静态,但是我知道这将无法工作,因为我想更改它

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);
    }   
}

On this line 在这条线上

About.SetCompanyName(str);

You're calling SetCompanyName statically (by using the class name "About"). 您正在静态调用SetCompanyName(通过使用类名“ About”)。 You should either make the method static (which is not the same as "final"; you seem to be confused about this) or create an instance of the About class first, like so: 您应该使方法静态(与“ final”不同;您似乎对此感到困惑),或者首先创建About类的实例,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM