2014-08-28 76 views
0

我有一个选项卡式应用程序。其中一个选项卡可以输入公司的名称,它应该将其更改为输入到其他选项卡中的任何内容。这里有两个类。我似乎无法得到这个继承工作

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

,我看到的是错误

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

在这里,“不能从类型关于使静态参考非静态方法SetCompanyName(字符串)”是“关于”,它包含了我二传手。它要求我做的方法和静态变量,但我知道这不会工作,因为我想改变它

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

回答

2

在此行中

About.SetCompanyName(str); 

你静态调用SetCompanyName(通过使用类名称“关于”)。您应该做的方法静态的(这是不一样的“最终”,你似乎混淆了这一点)或第一次创建关于类的一个实例,像这样:

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

你撞钉头我的朋友。我很困惑最终和静态...非常感谢 – helloWorldIsAlliKnow 2014-08-28 04:47:45

+0

@helloWorldIsAlliKnow如果你想使用类和方法与外部启动该类的任何对象你需要使该方法静态或静态类。 – Krishna 2014-08-28 05:42:29

相关问题