1

JWS を使用してアプリケーションを起動しています。JFrame のサイズをリセットする必要がありますが、サイズを変更しないでください。私は試してみましたが、どれが機能し frame.setSize(new Dimension(500, 500));frame.setResizable(false);いません。フォーラムを調べて、利用可能な他のオプションを試しました。私の完全なコードは

/**
 * 
 */
package com.bus.oprs;

import java.util.Properties;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Insets;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.plaf.basic.BasicBorders;
import javax.swing.plaf.basic.BasicBorders.ButtonBorder;

import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.MalformedURLException;

// classes of the web-start API, used in this example.
import javax.imageio.ImageIO;
import javax.jnlp.BasicService;
import javax.jnlp.ServiceManager;
import javax.jnlp.UnavailableServiceException;

/**
 * @author sreekanth
 * @date Aug 29, 2012 11:33:16 AM
 * @version 1.0
 */
public class ApsrtcOprsJNLP extends JFrame {

    private static final long serialVersionUID = 8221867230431906706L;
    private Color titleColor = new Color(241,248,255);
    final private JPanel panel = new JPanel();
    final Properties properties = new Properties();
    /**
     * @throws IOException, URISyntaxException 
     */
    public ApsrtcOprsJNLP() throws IOException, URISyntaxException {
    super();
    launchApplication();
    }


    private void loadProperties() {
    try {
        InputStream inputStreamUrl = this.getClass().getResourceAsStream("/AppResources.properties");
        properties.load(inputStreamUrl);
    } catch (Exception e) {
        e.printStackTrace();
    }
    }

    private void setButtonProperties(JButton button){
    try {
        Color buttonColor =  new Color(102,153,255);
        button.setBackground(buttonColor);
        button.setForeground(Color.WHITE);
        button.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    } catch (Exception e) {
        e.printStackTrace();
    }
    }

    private void launchApplication() throws IOException, URISyntaxException {
    JLabel clickMessageLabel = null;
    JLabel label = null;
    Dimension dimension = null;
    try {
        final BasicService basicService = (BasicService) ServiceManager.lookup("javax.jnlp.BasicService");
        dimension = new Dimension(400, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.loadProperties();
        setTitle(properties.getProperty("app.title"));

        UIManager.put("InternalFrame.activeTitleBackground", new javax.swing.plaf.ColorUIResource(titleColor));  
        JFrame.setDefaultLookAndFeelDecorated(true); 
        panel.setBackground(titleColor);

        label = new JLabel(properties.getProperty("app.welcome.msg"));
        Font newLabelFont = new Font(label.getFont().getName(), Font.BOLD, label.getFont().getSize());

        label.setHorizontalAlignment(SwingConstants.CENTER);
        panel.add(label);
        label.setFont(newLabelFont);

        clickMessageLabel = new JLabel(properties.getProperty("app.click.link.txt"));
        clickMessageLabel.setHorizontalAlignment(SwingConstants.CENTER);
        panel.add(clickMessageLabel);
        clickMessageLabel.setFont(newLabelFont);

        String[] jButton = properties.getProperty("urls").split(",");
        String[] jLabel = properties.getProperty("urlLabels").split(",");

        for (int i = 0; i < jLabel.length; i++) {
            JLabel labels = new JLabel(jLabel[i]);
            panel.add(labels);
            JButton button = new JButton(jButton[i]);
            this.setButtonProperties(button);
            button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
            try {
                URL url = new URL(ae.getActionCommand());
                basicService.showDocument(url);
                setVisible(false);
                dispose();
            } catch (MalformedURLException murle) {
                JOptionPane.showMessageDialog(panel,
                    murle.getMessage());
            }
            }
        });
            panel.add(button);
        }

        panel.setBorder(new EmptyBorder(8, 8, 8, 8));
        setSize(dimension);
        setResizable(false);
        getContentPane().add(panel);        
        pack();

        setLocationRelativeTo(null);
    } catch (UnavailableServiceException use) {
        use.printStackTrace();
        System.exit(-1);
    } finally {
        label = null;
        clickMessageLabel = null; dimension = null;
    }
    }
    /** 
     * Construct the GUI and display it.
     *  
     * @throws IOException 
     * @throws URISyntaxException
     */
    public static void main(String[] args) throws IOException, URISyntaxException {
    BufferedImage image = null;
    ApsrtcOprsJNLP app = new ApsrtcOprsJNLP();
    try {
        image = ImageIO.read(app.getClass().getResource("/logo.png"));
        app.setPreferredSize(new Dimension(image.getWidth(null), image.getHeight(null)));
    } catch (IOException e) {
        e.printStackTrace();
    }
    app.setIconImage(image);
    app.setVisible(true);
    }
}

JNLP :

<?xml version="1.0" encoding="UTF-8" ?>
<jnlp spec="1.0+"
      href="http://localhost:9999/apsrtc-oprs-jnlp/launch-app.jnlp">
  <information>
    <title>APSRTC-OPRS</title>
    <vendor>APSRTC</vendor>
    <description kind="one-line">
      APSRTC-OPRS WEB START
    </description>
    <icon kind="shortcut" href="logo.png" width="128" height="128"/>
    <shortcut online="false">
      <desktop/>
    </shortcut>
  </information>
  <resources>
    <j2se version="1.6+" href="http://java.sun.com/products/autodl/j2se" />
    <jar href="http://localhost:9999/apsrtc-oprs-jnlp/oprs-jnlp.jar" main="true" />
    <applet-desc
       name='apsrtc-oprs-jnlp'
       main-class='com.abhibus.oprs.ApsrtcOprsJNLP'
       width="800"
       height="800">
   </applet-desc>
  </resources>
 <application-desc main-class="com.abhibus.oprs.ApsrtcOprsJNLP"/>
</jnlp>

ありがとう

4

1 に答える 1

1

pack()を呼び出すと、そのウィンドウに配置されたUIコンテンツに合わせるために必要な最小サイズに変更されます。この線を削除すると、寸法は設定した場所にとどまるはずです。

private void launchApplication() {
    /* other code omitted */
    panel.setBorder(new EmptyBorder(8, 8, 8, 8));
    setSize(dimension);
    setResizable(false);
    getContentPane().add(panel);        
    //pack();
}
于 2012-10-14T12:25:13.230 に答える