有没有办法在 public static void main(string args[]) 中不使用 static?
Is there a way to not use static in public static void main(string args[])?
打包挥杆训练;
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 方法。如果我将我的主要方法设为静态,我就不能在我的 运行() 中使用 layoutManager,所以我想知道如何才能通过。或者,也许是另一种让 layoutManager 在这种情况下工作的方法。
您需要在主方法中执行如下操作。
GridBagLayoutTest Gb1 = T.new GridBagLayoutTest();
在java中main
方法是通常启动应用程序的入口点。它不像我们创建的其他方法,我们可以根据需要和想法提供名称、参数、return 类型。由于 main
方法是特殊方法,所以它有一个 定义的签名 。
签名(必填:public
、static
、return 类型:void
、输入参数:String[]
方法名称:main
, main 全部小写)如下
public static void main(String[] args)
这是 JVM 在执行我们的应用程序期间读取的方法,我们应该在其中包含我们的初始化代码。
- 您可以创建静态
init()
方法并从 main
调用 init()
- 您可以创建 class 的对象并调用您拥有的方法以进一步执行您的程序。
这些只是 提示 如何在 main
中编写代码,但重要的是要遵守 main 方法的约定以便我们的应用程序执行。
如评论中所述,不,如果没有具有该确切签名的主要方法,您将无法执行 java class。
public static void main(String args[])
我已经稍微清理了你的代码。它仍然是您的代码,但更整洁。
您不需要每次需要特定背景或其他内容时都使用 subclass JPanel
、JButton
或 GridBagLayout
。只需实例化原始 class 并使用其已定义的方法来设置其属性。
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 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 方法。如果我将我的主要方法设为静态,我就不能在我的 运行() 中使用 layoutManager,所以我想知道如何才能通过。或者,也许是另一种让 layoutManager 在这种情况下工作的方法。
您需要在主方法中执行如下操作。
GridBagLayoutTest Gb1 = T.new GridBagLayoutTest();
在java中main
方法是通常启动应用程序的入口点。它不像我们创建的其他方法,我们可以根据需要和想法提供名称、参数、return 类型。由于 main
方法是特殊方法,所以它有一个 定义的签名 。
签名(必填:public
、static
、return 类型:void
、输入参数:String[]
方法名称:main
, main 全部小写)如下
public static void main(String[] args)
这是 JVM 在执行我们的应用程序期间读取的方法,我们应该在其中包含我们的初始化代码。
- 您可以创建静态
init()
方法并从main
调用init()
- 您可以创建 class 的对象并调用您拥有的方法以进一步执行您的程序。
这些只是 提示 如何在 main
中编写代码,但重要的是要遵守 main 方法的约定以便我们的应用程序执行。
如评论中所述,不,如果没有具有该确切签名的主要方法,您将无法执行 java class。
public static void main(String args[])
我已经稍微清理了你的代码。它仍然是您的代码,但更整洁。
您不需要每次需要特定背景或其他内容时都使用 subclass JPanel
、JButton
或 GridBagLayout
。只需实例化原始 class 并使用其已定义的方法来设置其属性。
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();
}
});
}
}