Java 有没有办法不在公共静态void main(字符串args[])中使用static?
包装摇摆训练Java 有没有办法不在公共静态void main(字符串args[])中使用static?,java,swing,static,main,public,Java,Swing,Static,Main,Public,包装摇摆训练 import static java.awt.Color.BLACK; import java.awt.GridBagConstraints; import static java.awt.GridBagConstraints.CENTER; import static java.awt.GridBagConstraints.NORTH; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.L
import static java.awt.Color.BLACK;
import java.awt.GridBagConstraints;
import static java.awt.GridBagConstraints.CENTER;
import static java.awt.GridBagConstraints.NORTH;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.LayoutManager;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class JFrameTest extends JFrame{
public JFrameTest(){
setSize(800,800);
setTitle("Hello :D");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(true);
setVisible(true);
}
public class GridBagLayoutTest extends GridBagLayout{
public GridBagLayoutTest(){
setLayout(new GridBagLayout());
};
};
public static class JPanelTest extends JPanel{
public JPanelTest() {
setBackground(BLACK);
setOpaque(true);
}
}
public static class JButtonTest extends JButton{
public JButtonTest(){
};
};
public void main(String args[]){
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
JFrameTest T = new JFrameTest();
JPanelTest Jp1 = new JPanelTest();
JButtonTest Jb1 = new JButtonTest();
GridBagLayoutTest Gb1 = new GridBagLayoutTest();
GridBagConstraints c = new GridBagConstraints();
c.ipadx = 100;
c.ipady = 100;
c.gridheight = 1;
c.gridwidth = 1;
c.weightx = 1;
c.weighty = 1;
c.insets = (new Insets(0,0,0,500));
Jb1.setLayout((LayoutManager) c);
T.add(Jp1);
Jp1.add(Jb1);
}
});
}
}
编译后,我收到一条消息说我没有main方法。如果我将main方法设置为静态,我将无法在run()中使用layoutManager,因此我想知道如何才能通过此过程。或者,也许是让layoutManager在这种情况下工作的另一种方法。您需要在main方法中执行以下操作
GridBagLayoutTest Gb1 = T.new GridBagLayoutTest();
您需要在main方法中执行以下操作
GridBagLayoutTest Gb1 = T.new GridBagLayoutTest();
在java
main
中,方法是通常启动应用程序的入口点。它不像我们创建的其他方法,我们可以根据需要和想法提供名称、参数、返回类型。由于main
方法是一种特殊方法,因此它有一个定义的签名
main的签名(必填:public
,static
,返回类型:void
,输入参数:String[]
,方法名称:main
,所有字母小写)如下所示
public static void main(String[] args)
这是JVM在应用程序执行期间读取的方法,我们应该在其中包含初始化代码
init()
方法,并从main
调用init()
这些只是一些提示,说明了如何在
main
中编写代码,但为了让我们的应用程序执行,尊重main方法的约定是很重要的。在javamain
中,方法通常是启动应用程序的入口点。它不像我们创建的其他方法,我们可以根据需要和想法提供名称、参数、返回类型。由于main
方法是一种特殊方法,因此它有一个定义的签名
main的签名(必填:public
,static
,返回类型:void
,输入参数:String[]
,方法名称:main
,所有字母小写)如下所示
public static void main(String[] args)
这是JVM在应用程序执行期间读取的方法,我们应该在其中包含初始化代码
init()
方法,并从main
调用init()
这些只是一些提示,说明了如何在
main
中编写代码,但为了让我们的应用程序执行,尊重main方法的约定是很重要的。如注释中所述,不,如果没有具有该确切签名的main方法,就无法执行java类
public static void main(String args[])
我已经读了一点你的代码。它仍然是您的代码,但更整洁。
每次需要特定背景或任何内容时,不需要对JPanel
、JButton
或GridBagLayout
进行子类化。只需实例化原始类并使用其已定义的方法来设置其属性
import java.awt.Color; // no static import needed
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class JFrameTest extends JFrame {
public JFrameTest() {
setSize(800,800);
setTitle("Hello :D");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(true);
initComponents(); // <- Include your components in the main frame
setVisible(true);
}
private void initComponents() {
// Use meaningful names for your variables
// Respect Java naming conventions. No variable name start with a capital letter.
JPanel panel = new JPanel();
panel.setBackground(Color.BLACK);
panel.setOpaque(true);
panel.setLayout(new GridBagLayout()); // no need for static access
JButton button = new JButton();
GridBagConstraints gbc = new GridBagConstraints(); // this is not a Layout. It represents constrains to be used in the GribBagLayout on adding an element
gbc.ipadx = 100;
gbc.ipady = 100;
gbc.gridheight = 1;
gbc.gridwidth = 1;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.insets = (new Insets(0,0,0,500));
panel.add(button, gbc);
add(panel); // <- this.add(panel) where this is your instance of JFrameTest
}
public static void main(String args[]){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new JFrameTest();
}
});
}
}
import java.awt.Color;//不需要静态导入
导入java.awt.GridBagConstraints;
导入java.awt.GridBagLayout;
导入java.awt.Insets;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.SwingUtilities;
公共类JFrameTest扩展了JFrame{
公共JFrameTest(){
设置大小(800800);
setTitle(“你好:D”);
setDefaultCloseOperation(关闭时退出);
setLocationRelativeTo(空);
可设置大小(真);
initComponents();//如注释中所述,不,如果没有具有该确切签名的主方法,则无法执行java类
public static void main(String args[])
我已经清理了一点你的代码。它仍然是你的代码,但更整洁。
每次需要特定背景或任何内容时,您不需要为JPanel
、JButton
或GridBagLayout
创建子类。只需实例化原始类并使用其已定义的方法设置其属性即可
import java.awt.Color; // no static import needed
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class JFrameTest extends JFrame {
public JFrameTest() {
setSize(800,800);
setTitle("Hello :D");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(true);
initComponents(); // <- Include your components in the main frame
setVisible(true);
}
private void initComponents() {
// Use meaningful names for your variables
// Respect Java naming conventions. No variable name start with a capital letter.
JPanel panel = new JPanel();
panel.setBackground(Color.BLACK);
panel.setOpaque(true);
panel.setLayout(new GridBagLayout()); // no need for static access
JButton button = new JButton();
GridBagConstraints gbc = new GridBagConstraints(); // this is not a Layout. It represents constrains to be used in the GribBagLayout on adding an element
gbc.ipadx = 100;
gbc.ipady = 100;
gbc.gridheight = 1;
gbc.gridwidth = 1;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.insets = (new Insets(0,0,0,500));
panel.add(button, gbc);
add(panel); // <- this.add(panel) where this is your instance of JFrameTest
}
public static void main(String args[]){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new JFrameTest();
}
});
}
}
import java.awt.Color;//不需要静态导入
导入java.awt.GridBagConstraints;
导入java.awt.GridBagLayout;
导入java.awt.Insets;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.SwingUtilities;
公共类JFrameTest扩展了JFrame{
公共JFrameTest(){
设置大小(800800);
setTitle(“你好:D”);
setDefaultCloseOperation(关闭时退出);
setLocationRelativeTo(空);
可设置大小(真);
initComponents();//你说的“我不能在我的run()中使用layoutManager”是什么意思?对我来说应该没问题。但是是的,main
必须是静态的才能成为有效的入口点。首先创建一个“main”类,该类有一个public static void main(String[]args)
方法,使用它来创建“main”的实例类。使用“main”类来实际构建和启动您的程序。一般来说,出于多种原因,您应该避免从JFrame
扩展,但因为它将您锁定在单个用例中,您不能将gridbagstraints
类型的c
强制转换为LayoutManager
。如果您愿意发布和复制/粘贴到您收到的实际错误消息中,该消息使您认为“无法使用LayoutManager”(而不是关于缺少main的消息)。根据您的标题,不,您无法从main方法中避免静态关键字。JVM默认情况下不知道调用哪个实例方法,所以它总是查找main方法,因为它不知道哪个实例,所以main方法必须是静态的。顺便说一句-不要扩展JFrame
,GridBagLayout
,JPanel
或JButton
。如果用作(标准的、未修改的)实例,它们中的每一个都可以正常工作。W