2014-08-28 82 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

相關問題