套装挥杆训练;
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 方法,您将无法执行 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();
}
});
}
}
关于java - 有没有办法在 public static void main(string args[]) 中不使用 static ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36244663/