Java Can';不要从另一个类调用静态方法,即使它已导入
我的主文件:Java Can';不要从另一个类调用静态方法,即使它已导入,java,class,Java,Class,我的主文件: import java.awt.*; import javax.swing.*; import Windows; public class Main{ public static void main(String args[]){ Windows.createShowGUI(1); } } 另一个文件: package order_java; import java.awt.*; import javax.swing.*; publ
import java.awt.*;
import javax.swing.*;
import Windows;
public class Main{
public static void main(String args[]){
Windows.createShowGUI(1);
}
}
另一个文件:
package order_java;
import java.awt.*;
import javax.swing.*;
public class Windows {
public static void addComponentsToHomePane(Container pane){
//Refer to https://docs.oracle.com/javase/tutorial/uiswing/layout/border.html
//Declaring and linking logo and its label
ImageIcon logo = new ImageIcon("img/mindnew.png");
Image image = logo.getImage(); //Resize Image
Image newimg = image.getScaledInstance(100, 100, java.awt.Image.SCALE_SMOOTH); // scale it the smooth way
logo = new ImageIcon(newimg); // transform it back
JLabel logoLabel = new JLabel ("Supermind T-Shirts");
logoLabel.setFont(new Font("",Font.PLAIN,22)); //Set font style and size
logoLabel.setIcon(logo);
//Adding to the panel
pane.add(logoLabel, BorderLayout.PAGE_START);
//Nested FlowLayout Panel in main pane, refer to https://docs.oracle.com/javase/tutorial/uiswing/layout/flow.html
JPanel midPanel = new JPanel();
//Adding buttons to pane
JButton button1 = new JButton("Staff Member");
button1.setPreferredSize(new Dimension(150,40));
JButton button2 = new JButton("Customer");
button2.setPreferredSize(new Dimension(150,40));
midPanel.add(button1);
midPanel.add(new JLabel("or"));
midPanel.add(button2);
//Adding nested panel to main pane
pane.add(midPanel,BorderLayout.CENTER);
}
public static void addComponentsToPane(Container pane){
ImageIcon logo = new ImageIcon("img/mindnew.png");
Image image = logo.getImage(); //Resize Image
Image newimg = image.getScaledInstance(40, 40, java.awt.Image.SCALE_SMOOTH); // scale it the smooth way
logo = new ImageIcon(newimg); // transform it back
JLabel logoLabel = new JLabel ("");
logoLabel.setFont(new Font("",Font.PLAIN,20)); //Set font style and size
logoLabel.setIcon(logo);
//Set cart button
JButton btnCart = new JButton("");
ImageIcon cart = new ImageIcon("img/cart.png");
image = cart.getImage(); //Resize Image
newimg = image.getScaledInstance(30, 30, java.awt.Image.SCALE_SMOOTH); // scale it the smooth way
cart = new ImageIcon(newimg);
btnCart.setIcon(cart);
//PAGE_START
JPanel topPane=new JPanel();
topPane.setLayout(new BoxLayout(topPane,BoxLayout.X_AXIS));
topPane.add(logoLabel);
topPane.add(Box.createHorizontalGlue());
topPane.add(btnCart);
//PAGE_END add Back button
JPanel btmPane=new JPanel();
btmPane.setLayout(new BoxLayout(btmPane,BoxLayout.X_AXIS));
JButton btnBack = new JButton("");
btnBack.setPreferredSize(new Dimension(35,35));
ImageIcon back = new ImageIcon("img/back.png");
image = back.getImage(); //Resize Image
newimg = image.getScaledInstance(30, 30, java.awt.Image.SCALE_SMOOTH); // scale it the smooth way
back = new ImageIcon(newimg);
btnBack.setIcon(back);
btmPane.add(btnBack);
//Adding to the panel
pane.add(btmPane , BorderLayout.PAGE_END);
pane.add(topPane, BorderLayout.PAGE_START);
}
public static createShowGUI(int n){
//Set new frame
JFrame frame = new JFrame("Custom T-Shirt Shop");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
if (n==1) addComponentsToHomePane(frame.getContentPane()); //adding the panels
else addComponentsToPane(frame.getContentPane());
//Display the window
frame.setVisible(true);
}
}
编译时,出现以下错误:
The method createShowGUI(int) is undefined for the type Windows
是什么导致了这个问题?这过去只是一个文件,运行正常,除了createShowGUI方法是私有的,因为main在同一个类中,所以它是相同的。这些问题是在重构之后出现的。(我不知道还要在这里键入什么,我只是填写所需的字数。)您必须导入
order\u java.Windows
编辑: 公共静态createShowGUI(int n) 这里有点不对劲。。。您看到缺少的返回类型了吗?一定是这样
公共静态void createShowGUI(int n)
您必须删除
包订单\u java代码>,如果两者都在没有区别的未命名(=默认)包中。我应该清楚地表明它们在同一条路径上。你在方法的声明中丢失了void
。哦,这很有效..奇怪,在没有void的情况下重构之前它就起作用了。谢谢。显示的代码甚至不会编译Windows
。