java使JFrame的操作,居中 设置标题等

public static void main(String[] args) {  
JFrame frame = new JFrame("利用JFrame创建窗口"); // 创建指定标题的JFrame窗口对象  
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 关闭按钮的动作为退出窗口  
frame.setSize(400, 300);                          // 设置窗口大小  
Dimension displaySize = Toolkit.getDefaultToolkit().getScreenSize(); // 获得显示器大小对象  
Dimension frameSize = frame.getSize();             // 获得窗口大小对象  
if (frameSize.width > displaySize.width)  
frameSize.width = displaySize.width;           // 窗口的宽度不能大于显示器的宽度  

frame.setLocation((displaySize.width - frameSize.width) / 2,  
(displaySize.height - frameSize.height) / 2); // 设置窗口居中显示器显示  
frame.setVisible(true);                          // 设置窗口为可见的,默认为不可见  
}  

还可以用方法jFrame.setLocationRelativeTo(null) 不是对所有的都适用,

 

设置JFrame标题

Frame myJFrame = new JFrame("title"); //使用了构造函数

或采用继承的frame方法

public void setTitle(String title);
myJFrmae.setTitle("title");

设置左上角的图标
//Set the frame icon to an image loaded from a file.
frame.setIconImage(new ImageIcon(imgURL).getImage());
类ImageIcon一个 Icon 接口的实现,它根据 Image 绘制 Icon。可使用 MediaTracker 预载根据 URL、文件名或字节数组创建的图像,以监视该图像的加载状态。

jFrame.setIconImage(Toolkit.getDefaultToolkit().createImage("image/sunyanzi.png"));

jFrame.setIconImage(jFrame.getToolkit().getImage("C:\\yanzi.png")); //可以

JFrame设置背景图片
JFrame是由这么几部分组成:最底下一层JRootPane,上面是glassPane(一个JPanel)和layeredPane(一个JLayeredPane),而layeredPane又由contentPane(一个JPanel)和menuBar构成。
我们一般在JFrame上添加组件往往都是加在contentPane上面:

frame.getContentPane().add(btn);

要在JFrame上添加背景图片,常见做法是加在layeredPane上面,并将contentPane设置成透明的即可。

// 将图片添加到layeredPane
ImageIcon img = new ImageIcon("steve.jpg");
JLabel imgLabel = new JLabel(img);
frame.getLayeredPane().add(imgLabel, new Integer(Integer.MIN_VALUE));
imgLabel.setBounds(0,0,img.getIconWidth(), img.getIconHeight());
...
//
将contentPane设置成透明的
((JPanel)getContentPane()).setOpaque(false);
import   javax.swing.*;  
  import   java.awt.*;  
  import   java.awt.event.*;  
  public   class  bj  
  {  
          private   JFrame   frame   =   new   JFrame("背景图片测试");  
          private   JPanel   imagePanel   ;  
          private   ImageIcon   background;  
      public   bj()  
          {  
                  background   =   new   ImageIcon("ying.jpg");//背景图片  
                  JLabel   label   =   new   JLabel(background);//把背景图片显示在一个标签里面  
    //           把标签的大小位置设置为图片刚好填充整个面板  
                  label.setBounds(0,0,background.getIconWidth(),background.getIconHeight());  
    //           把内容窗格转化为JPanel,否则不能用方法setOpaque()来使内容窗格透明                
                  imagePanel   =   (JPanel)frame.getContentPane();  
                  imagePanel.setOpaque(false);  
    //           内容窗格默认的布局管理器为BorderLayout  
                  imagePanel.setLayout(new   FlowLayout());  
                  imagePanel.add(new   JButton("测试按钮"));  
                  frame.getLayeredPane().setLayout(null);  
    //           把背景图片添加到分层窗格的最底层作为背景                
                  frame.getLayeredPane().add(label,new   Integer(Integer.MIN_VALUE));          
                  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
                  frame.setSize(background.getIconWidth(),background.getIconHeight());  
                  frame.setVisible(true);  
          }  
             public   static   void   main(String[]   args)  
          {  
                  new   bj();  
          } 
}

在java swing中需要为容器添加图片,或者背景图片。

提供两种简单的解决方案,一种利用JPanel,另一种利用JLabel

JPanel(源代码)

package oo;  
  
import java.awt.Graphics;  
import java.awt.Image;  
import java.io.File;  
  
import javax.swing.ImageIcon;  
import javax.swing.JFrame;  
import javax.swing.JPanel;  
  
public class Drawing {  
  
    JFrame jframe = new JFrame();  
    public static JPanel GImage = null;  
  
    public Drawing() {  
        initFrame();  
    }  
  
    // 初始化窗口   
    public void initFrame() {  
        // 利用JPanel添加背景图片   
  
        GImage = new JPanel() {  
  
            protected void paintComponent(Graphics g) {  
                ImageIcon icon = new ImageIcon("image\\benbenla.jpg");  
                Image img = icon.getImage();  
                g.drawImage(img, 0, 0, icon.getIconWidth(),  
                        icon.getIconHeight(), icon.getImageObserver());  
                jframe.setSize(icon.getIconWidth(), icon.getIconHeight());  
  
            }  
  
        };  
        jframe.setTitle("测试背景图片");  
        jframe.add(GImage);  
        jframe.pack();  
        jframe.setVisible(true);  
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  
    }  
  
    public static void main(String[] args) {  
        new Drawing();  
  
    }  
  
}  
2.JLabel源代码

package swing.draw;  
  
import java.awt.Image;  
  
import javax.swing.ImageIcon;  
import javax.swing.JFrame;  
import javax.swing.JLabel;  
  
/** 利用JLabel来构建图片 */  
  
public class Drawing2 {  
    JLabel jlpic = new JLabel();  
    JFrame jframe = new JFrame();  
  
    public Drawing2() {  
  
        init1Frame();  
    }  
  
    public void init1Frame() {  
        ImageIcon icon = new ImageIcon("image\\benbenla.jpg");  
        icon.setImage(icon.getImage().getScaledInstance(icon.getIconWidth(),  
                icon.getIconHeight(), Image.SCALE_DEFAULT));  
        System.out.println(icon.getIconHeight() + "" + icon.getIconWidth());  
        jlpic.setBounds(0, 0, 1366, 768);  
        jlpic.setHorizontalAlignment(0);  
        jlpic.setIcon(icon);  
        jframe.setSize(1366, 768);  
        jframe.add(jlpic);  
        jframe.pack();  
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        jframe.setVisible(true);  
  
    }  
  
    public static void main(String args[]) {  
  
        new Drawing2();  
    }  
}  

 


 

 

原文地址:https://www.cnblogs.com/youxin/p/2465014.html