[英]I can't seem to get this inheritance to work
我有一個選項卡式應用程序。 您可以在其中一個標簽中輸入公司名稱,並且公司名稱應更改為您在另一個標簽中鍵入的名稱。 這是兩個類。
在此代碼上,它告訴我更改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);
}
}
在這條線上
About.SetCompanyName(str);
您正在靜態調用SetCompanyName(通過使用類名“ About”)。 您應該使方法靜態(與“ final”不同;您似乎對此感到困惑),或者首先創建About類的實例,如下所示:
About myAboutObject = new About();
myAboutObject.SetCompanyName(str);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.