java图形界面里面怎么实现图片缩放,比如我有一个frame里面放了一个label,label上面有张图片,当frame大小改变时让图片一起变,图片原来是将label填满的,

解决方案 »

  1.   

    Image image = new ImageIcon("images/set2.jpg").getImage();
    image = image.getScaledInstance(300, 180, 0);JLabel label = new JLabel();
    label.setBounds(70, 75, 300, 200);
    label.setIcon(new ImageIcon(image));
      

  2.   


    package musicPlatform.client.login;import java.awt.*;
    import javax.swing.*;import musicPlatform.client.config.SaveMusicFileDirectory;import testSwing2.UsedToChangeMyFrame;public class SubstituteLoginFrame extends JFrame {
    private static final long serialVersionUID = 2022587044957057382L;
    private JButton jButton1;// 登录
    private JButton jButton2;// 注册
    private JLabel jLabel2;// 密码
    private JPasswordField jTextField2;// 密码
    private JTextField jTextField1;// 用户名
    private JLabel jLabel1;// 用户名 public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    new SubstituteLoginFrame();
    }
    });
    }
    private static void clientConfig() {
    String defaultPath = System.getProperty("user.home") + "\\" + "music";
    SaveMusicFileDirectory save = new SaveMusicFileDirectory();
    save.creatXML(defaultPath);
    } public SubstituteLoginFrame() {
    super();
    autoTest();// 检查服务器
    init();
    } /**
     * 首次开启客户端时检查连接状态
     */
    private void autoTest() {
    RespondLoginEnter res = new RespondLoginEnter();
    res.initialTestLinkage();
    }
    public void illegalCallingEntrance() {
    SubstituteLoginFrame lf = new SubstituteLoginFrame();
    lf.setLocationRelativeTo(null);
    lf.setVisible(true);
    } public void init() {
    Container c = this.getContentPane();
    JPanel lowerPanel = new MyPanelk();
    c.add(lowerPanel);
    GroupLayout lowerPanelLayout = new GroupLayout((JComponent) lowerPanel);
    lowerPanel.setLayout(lowerPanelLayout);
    lowerPanel.setLocale(new java.util.Locale("zh", "TW"));
    // lowerPanel.setPreferredSize(new Dimension(1100, 680));
    {
    jButton1 = new JButton();
    jButton1.setText("注册");
    }
    {
    jButton2 = new JButton();
    jButton2.setText("\u767b\u5f55");
    }
    {
    jLabel1 = new JLabel();
    jLabel1.setText("\u7528\u6237\u540d");
    jLabel1.setFont(new java.awt.Font("微软雅黑", 3, 11));
    jLabel1.setForeground(new java.awt.Color(255, 255, 255));
    }
    {
    jLabel2 = new JLabel();
    jLabel2.setText("\u5bc6\u7801");
    jLabel2.setFont(new java.awt.Font("微软雅黑", 3, 11));
    jLabel2.setForeground(new java.awt.Color(255, 255, 255));
    }
    {
    jTextField2 = new JPasswordField();
    jTextField2.addFocusListener(new FocusAdapter() {
    public void focusGained(FocusEvent evt) {
    jTextField2.selectAll();
    }
    });
    }
    {
    jTextField1 = new JTextField();
    jTextField1.addFocusListener(new FocusAdapter() {
    public void focusGained(FocusEvent evt) {
    jTextField1.selectAll();
    }
    });
    }
    lowerPanelLayout.setVerticalGroup(lowerPanelLayout
    .createSequentialGroup()
    .addContainerGap(63, 63)
    .addGroup(
    lowerPanelLayout
    .createParallelGroup(
    GroupLayout.Alignment.BASELINE)
    .addComponent(jTextField1,
    GroupLayout.Alignment.BASELINE,
    GroupLayout.PREFERRED_SIZE, 21,
    GroupLayout.PREFERRED_SIZE)
    .addComponent(jLabel1,
    GroupLayout.Alignment.BASELINE,
    GroupLayout.PREFERRED_SIZE,
    GroupLayout.PREFERRED_SIZE,
    GroupLayout.PREFERRED_SIZE))
    .addGap(19)
    .addGroup(
    lowerPanelLayout
    .createParallelGroup(
    GroupLayout.Alignment.BASELINE)
    .addComponent(jTextField2,
    GroupLayout.Alignment.BASELINE,
    GroupLayout.PREFERRED_SIZE, 21,
    GroupLayout.PREFERRED_SIZE)
    .addComponent(jLabel2,
    GroupLayout.Alignment.BASELINE,
    GroupLayout.PREFERRED_SIZE,
    GroupLayout.PREFERRED_SIZE,
    GroupLayout.PREFERRED_SIZE))
    .addGap(29)
    .addGroup(
    lowerPanelLayout
    .createParallelGroup(
    GroupLayout.Alignment.BASELINE)
    .addComponent(jButton2,
    GroupLayout.Alignment.BASELINE,
    GroupLayout.PREFERRED_SIZE, 22,
    GroupLayout.PREFERRED_SIZE)
    .addComponent(jButton1,
    GroupLayout.Alignment.BASELINE,
    GroupLayout.PREFERRED_SIZE, 22,
    GroupLayout.PREFERRED_SIZE))
    .addContainerGap(45, 45));
    lowerPanelLayout
    .setHorizontalGroup(lowerPanelLayout
    .createSequentialGroup()
    .addContainerGap(108, 108)
    .addGroup(
    lowerPanelLayout
    .createParallelGroup()
    .addGroup(
    lowerPanelLayout
    .createSequentialGroup()
    .addGroup(
    lowerPanelLayout
    .createParallelGroup()
    .addComponent(
    jLabel1,
    GroupLayout.Alignment.LEADING,
    GroupLayout.PREFERRED_SIZE,
    46,
    GroupLayout.PREFERRED_SIZE)
    .addGroup(
    GroupLayout.Alignment.LEADING,
    lowerPanelLayout
    .createSequentialGroup()
    .addComponent(
    jLabel2,
    GroupLayout.PREFERRED_SIZE,
    26,
    GroupLayout.PREFERRED_SIZE)
    .addGap(20)))
    .addPreferredGap(
    LayoutStyle.ComponentPlacement.RELATED,
    12,
    Short.MAX_VALUE)
    .addGroup(
    lowerPanelLayout
    .createParallelGroup()
    .addComponent(
    jTextField2,
    GroupLayout.Alignment.LEADING,
    GroupLayout.PREFERRED_SIZE,
    108,
    GroupLayout.PREFERRED_SIZE)
    .addComponent(
    jTextField1,
    GroupLayout.Alignment.LEADING,
    GroupLayout.PREFERRED_SIZE,
    108,
    GroupLayout.PREFERRED_SIZE)))
    .addGroup(
    GroupLayout.Alignment.LEADING,
    lowerPanelLayout
    .createSequentialGroup()
    .addPreferredGap(
    jLabel2,
    jButton1,
    LayoutStyle.ComponentPlacement.INDENT)
    .addComponent(jButton1,
    0, 67,
    Short.MAX_VALUE)
    .addGap(17)
    .addComponent(
    jButton2,
    GroupLayout.PREFERRED_SIZE,
    70,
    GroupLayout.PREFERRED_SIZE)))
    .addContainerGap(120, 120)); /**
     * 注册监听
     */
    {
    jButton1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
    // lf.setVisible(false);
    // System.out.println("这里可以显示注册窗口了");
    SubstituteLoginFrame.this.setVisible(false);
    // SubstituteLoginFrame.this.dispose();
    RespondLoginEnter res = new RespondLoginEnter();
    res.testLinkage();
    new LogonJFrame();
    InOn inon = new InOn();// 保存本登录窗口对象
    inon.backToLogin(SubstituteLoginFrame.this);
    }
    });
    }
    /**
     * 登录监听
     */
    {
    // jButton1.addActionListener(new
    // jButton1ActionListener(this));
    jButton2.addActionListener(new ActionListener() { // 注册按钮事件
    public void actionPerformed(ActionEvent event) { // 事件处理器
    String userName = jTextField1.getText();
    String password = jTextField2.getText();
    // 检查输入合法性
    if (ToolString.isInefficacy(userName)
    || ToolString.isInefficacy(password)) {
    JOptionPane.showMessageDialog(null, "   用户名和密码不能为空",
    "请重新输入", JOptionPane.ERROR_MESSAGE);
    return;
    }
    RespondLoginEnter respondLogin = new RespondLoginEnter();
    try {
    // 连接服务器
    respondLogin.linkage();
    // 包装用户名,密码:request=login★userName=a★userPassword=d★
    String nameAndPass = ToolString.packageLoginData(
    userName, password);
    // 发送用户名,密码
    respondLogin.sendMessage(nameAndPass);// 发送成功后客户端若等不到服务器数据,界面不响应
    // 获得服务器响应 收不到数据的处理?
    System.out
    .println("开始接收服务器数据:loginsuccess or loginfail!");
    String receivedMsg = respondLogin.receiveMsg();
    if (receivedMsg == null) {
    System.out.println("服务器响应超时!");// 能否显示?????????????????
    }
    // respondLogin.close(); //
    // 需要关闭????????????登錄成功也會關閉????????????????????????????
    if (!"loginsuccess".equals(receivedMsg)) {
    JOptionPane.showMessageDialog(null, "用户名或密码错误",
    "登录失败", JOptionPane.ERROR_MESSAGE);
    return;
    } else {
    // System.out.println("这里开始显示主页并关闭登录窗口");//
    // -------------------------------------------------------
    SubstituteLoginFrame.this.dispose();
    // 登录成功, 显示主页窗口
    new UsedToChangeMyFrame();
    }
    } catch (Exception e) {
    e.printStackTrace();
    } }
    });
    } this.setVisible(true);
    this.setResizable(false);
    String direct = System.getProperty("user.dir")
    + "\\src\\icon\\pro\\key1.gif";
    Image img = Toolkit.getDefaultToolkit().getImage(direct);
    this.setIconImage(img);
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    this.setLocation(screenSize.width / 2 - 400 / 2,
    screenSize.height / 2 - 250 / 2);
    this.setTitle("\u97f3\u4e50\u5e73\u53f0");
    // this.setSize(new Dimension(1100, 680));
    this.setSize(new Dimension(400, 250));
    }}class MyPanelk extends JPanel {
    /**
     * 
     */
    private static final long serialVersionUID = 1L; @Override
    public void paintComponent(Graphics g) { // 重绘函数
    Graphics2D g2 = (Graphics2D) g;
    super.paintComponent(g);
    String directory = System.getProperty("user.dir")
    + "\\src\\icon\\pro\\wow2.jpg";
    Image img = Toolkit.getDefaultToolkit().getImage(directory);
    g2.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this); // 在面板上绘制背景图片
    }
    }
     
      

  3.   

    Image的getScaledINnstance(int width, int height, int hints)方法
      

  4.   


    一句话就是自定义个JPanel,给它加个背景图,再add到JFrame就行了
    有用的部分自己挑挑吧  图片路径 src\icon\pro\wow2.jpg
    类路径src\musicPlatform\client\login
    效果图
      

  5.   

    JLabel无法自动缩放图片,自己实现一个组件,重写paintComponent方法
      

  6.   

    final JLabel jlabel = ...;
    jlabel.addComponentListener(new ComponentAdapter(){
            @Override public void componentResized(ComponentEvent e){
                ...
            }
        });