-3

大家好,我正在从这个站点学习 java。我想在不放 main 的情况下制作 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.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();
}
});
}
}
4

3 回答 3

2

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

要了解更多信息:JLS 12. 执行Java 教程

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 );
   }
}

结果:

在此处输入图像描述

于 2013-03-16T16:32:26.037 回答
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

于 2013-03-16T16:27:29.147 回答
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类文件中。

于 2013-03-16T16:27:35.130 回答