JFrame frame = new JFrame("...");
frame.setResizable(false);

解决方案 »

  1.   


    在JFrame中用JPanel画了一个图形,为什么程序运行后没有显示出来,当改变窗口后,才显示?
    这个问题知道吗?
      

  2.   

    我给你一个例子!
    主程序:
    import javax.swing.UIManager;
    import java.awt.*;/**
     * <p>Title: </p>
     * <p>Description: </p>
     * <p>Copyright: Copyright (c) 2003</p>
     * <p>Company: </p>
     * @author not attributable
     * @version 1.0
     */public class Application1 {
      boolean packFrame = false;  //Construct the application
      public Application1() {
        Frame1 frame = new Frame1();
        //Validate frames that have preset sizes
        //Pack frames that have useful preferred size info, e.g. from their layout
        if (packFrame) {
          frame.pack();
        }
        else {
          frame.validate();
        }
        //Center the window
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = frame.getSize();
        if (frameSize.height > screenSize.height) {
          frameSize.height = screenSize.height;
        }
        if (frameSize.width > screenSize.width) {
          frameSize.width = screenSize.width;
        }
        frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
        frame.setVisible(true);
      }
      //Main method
      public static void main(String[] args) {
        try {
          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch(Exception e) {
          e.printStackTrace();
        }
        new Application1();
      }
    }
    JFrame:。。
    import java.awt.*;
    import java.awt.Graphics;
    import java.awt.event.*;
    import javax.swing.*;public class Frame1 extends JFrame {
      JPanel contentPane;
      FlowLayout flowLayout1 = new FlowLayout();  //Construct the frame
      public Frame1() {
        enableEvents(AWTEvent.WINDOW_EVENT_MASK);
        try {
          this.setSize(new Dimension(400, 300));
         this.setTitle("Frame Title");
        }
        catch(Exception e) {
          e.printStackTrace();
        }
      }
      public void paint(Graphics g)
    {
       g.drawString("233",23,34);
        g.drawOval(40,60,80,120);

      protected void processWindowEvent(WindowEvent e) {
        super.processWindowEvent(e);
        if (e.getID() == WindowEvent.WINDOW_CLOSING) {
          System.exit(0);
        }
      }}