如何运行此文件删除main

时间:2013-03-16 16:18:03

标签: java swing jframe

大家好,我正在从这个网站学习java。我想制作java文件而不放置main,这样就可以很容易地与其他文件连接。我的意思是我想制作这个子文件。那么你们可以帮我从这个文件中删除main并运行它。

这是我的代码

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.JColorChooser;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/* ColorChooserDemo.java requires no other files. */
public class jptborder extends JPanel{

protected JColorChooser tcc;
protected JLabel banner;

public jptborder() {
super(new BorderLayout());

//Set up the banner at the top of the window
banner = new JLabel("Welcome To this border Example",
JLabel.CENTER);
banner.setForeground(Color.red);
//banner.setBackground(Color.blue);
banner.setOpaque(true);
banner.setFont(new Font("Arial", Font.BOLD, 24));
banner.setPreferredSize(new Dimension(450, 65));

JPanel bannerPanel = new JPanel(new BorderLayout());
bannerPanel.add(banner, BorderLayout.CENTER);
bannerPanel.setBorder(BorderFactory.createTitledBorder("Welcome"));

add(bannerPanel, BorderLayout.CENTER);

}
public static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("jptBorder");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Create and set up the content pane.
JComponent newContentPane = new jptborder();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);

//Display the window.
frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}

3 个答案:

答案 0 :(得分:2)

在另一个文件中创建另一个类并将main方法移入其中。

要了解详情:JLS 12. ExecutionThe Java Tutorial

<强> JPTBorderLauncher.java

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class JPTBorderLauncher extends JFrame {
   public JPTBorderLauncher() {
      super( "jptBorder" );
      setDefaultCloseOperation( EXIT_ON_CLOSE );
      add( new jptborder());
      pack();
      setVisible( true );
   }
   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         @Override public void run() { new JPTBorderLauncher(); }});
   }
}

<强> jptborder.java

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.JColorChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

public class jptborder
   extends
      JPanel
{
   protected JColorChooser tcc;
   protected JLabel        banner;

   public jptborder() {
      super( new BorderLayout() );
      banner = new JLabel( "Welcome To this border Example",
                           SwingConstants.CENTER );
      banner.setForeground( Color.red );
      banner.setOpaque( true );
      banner.setFont( new Font( "Arial", Font.BOLD, 24 ) );
      banner.setPreferredSize( new Dimension( 450, 65 ) );
      JPanel bannerPanel = new JPanel( new BorderLayout() );
      bannerPanel.add( banner, BorderLayout.CENTER );
      bannerPanel.setBorder( BorderFactory.createTitledBorder( "Welcome" ) );
      add( bannerPanel, BorderLayout.CENTER );
   }
}

结果:

enter image description here

答案 1 :(得分:1)

要实现您想要的功能,您应该将main方法保留在另一个类中。在那里你可以致电jptborder createAndShowGUI。例如:

public class MainClass
{
  public static void main(String st[])
  {
   javax.swing.SwingUtilities.invokeLater( new Runnable()
   {
     @Override
     public void run()
     {
       jptborder.createAndShowGUI();
     }
   });
  }
}

然后编译并运行MainClass

答案 2 :(得分:1)

为您的jptborder课程创建一个主文件和一个文件,然后从您的主要文章中引用您的课程。确保它们位于同一个包装中并且彼此可见。

public static void main(String args[]) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            JptBorder border = new JptBorder(); // To use your constructor
            JptBorder.createAndShowGUI(); to use your static method
        }
    });
}

其他所有内容都将出现在您的JptBorder类文件中。