本文介绍了为什么Java jFrame setResizable()很难采取行动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码由Netbean 6.8 GUI构建器自动生成。默认应用程序未包含在jFrame中!它只在jPanel中,我不知道如何使它无法重新调整大小。所以我添加了一个jFrame来包含所有这些,尽管我设置了它的setResizable();为假...它仍然重新调整大小!以下是代码:

This code is automatically generated by Netbean 6.8 GUI builder. The default application was not contained in a jFrame! It was in jPanel only which I didn't know how to make it none re-sizable. so I added a jFrame to contain all of them and although I set its setResizable(); to False...it still re-sizable! Here is the code:

    import org.jdesktop.application.Action;
    import org.jdesktop.application.ResourceMap;
    import org.jdesktop.application.SingleFrameApplication;
    import org.jdesktop.application.FrameView;
    import org.jdesktop.application.TaskMonitor;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.Timer;
    import javax.swing.Icon;
    import javax.swing.JDialog;
    import javax.swing.JFrame;

    public class BOSSConverterView extends FrameView {

    public BOSSConverterView(SingleFrameApplication app) {
    super(app);
    JDialog dlg = new JDialog();
    dlg.setUndecorated(true);
    dlg.setModal(true);
    dlg.setLayout(new BorderLayout());
    ImageIcon img = new ImageIcon(getClass().getResource("splash.png"));
    dlg.add(img, BorderLayout.CENTER);
    dlg.setLocationRelativeTo(null);
    dlg.setVisible(true);
    initComponents();
    ResourceMap resourceMap = getResourceMap();
    int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
    messageTimer = new Timer(messageTimeout, new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            statusMessageLabel.setText("");
        }
    });
    messageTimer.setRepeats(false);
    int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
    for (int i = 0; i < busyIcons.length; i++) {
        busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
    }
    busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
            statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
        }
    });
    idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
    statusAnimationLabel.setIcon(idleIcon);
    progressBar.setVisible(false);

    // connecting action tasks to status bar via TaskMonitor
    TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
    taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
        public void propertyChange(java.beans.PropertyChangeEvent evt) {
            String propertyName = evt.getPropertyName();
            if ("started".equals(propertyName)) {
                if (!busyIconTimer.isRunning()) {
                    statusAnimationLabel.setIcon(busyIcons[0]);
                    busyIconIndex = 0;
                    busyIconTimer.start();
                }
                progressBar.setVisible(true);
                progressBar.setIndeterminate(true);
            } else if ("done".equals(propertyName)) {
                busyIconTimer.stop();
                statusAnimationLabel.setIcon(idleIcon);
                progressBar.setVisible(false);
                progressBar.setValue(0);
            } else if ("message".equals(propertyName)) {
                String text = (String)(evt.getNewValue());
                statusMessageLabel.setText((text == null) ? "" : text);
                messageTimer.restart();
            } else if ("progress".equals(propertyName)) {
                int value = (Integer)(evt.getNewValue());
                progressBar.setVisible(true);
                progressBar.setIndeterminate(false);
                progressBar.setValue(value);
            }
        }
    });
}

@Action
public void showAboutBox() {
    if (aboutBox == null) {
        JFrame mainFrame = BOSSConverterApp.getApplication().getMainFrame();
        aboutBox = new BOSSConverterAboutBox(mainFrame);
        aboutBox.setLocationRelativeTo(mainFrame);
    }
    BOSSConverterApp.getApplication().show(aboutBox);
}

/** This method is called from within the constructor to
 * initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is
 * always regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    mainFrame = new javax.swing.JFrame();
    mainPanel = new javax.swing.JPanel();
    jTabbedPane1 = new javax.swing.JTabbedPane();
    jDesktopPane1 = new javax.swing.JDesktopPane();
    jFormattedTextField1 = new javax.swing.JFormattedTextField();
    jFormattedTextField2 = new javax.swing.JFormattedTextField();
    jDesktopPane2 = new javax.swing.JDesktopPane();
    jScrollPane1 = new javax.swing.JScrollPane();
    jTextArea1 = new javax.swing.JTextArea();
    jScrollPane2 = new javax.swing.JScrollPane();
    jTextArea2 = new javax.swing.JTextArea();
    jButton1 = new javax.swing.JButton();
    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    statusPanel = new javax.swing.JPanel();
    javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
    statusMessageLabel = new javax.swing.JLabel();
    statusAnimationLabel = new javax.swing.JLabel();
    progressBar = new javax.swing.JProgressBar();
    menuBar = new javax.swing.JMenuBar();
    javax.swing.JMenu fileMenu = new javax.swing.JMenu();
    openFileMenuItem = new javax.swing.JMenuItem();
    javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
    javax.swing.JMenu helpMenu = new javax.swing.JMenu();
    javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();

    mainFrame.setName("mainFrame"); // NOI18N
    mainFrame.setResizable(false);

    mainPanel.setMaximumSize(new java.awt.Dimension(400, 400));
    mainPanel.setName("mainPanel"); // NOI18N

    org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(bossconverter.BOSSConverterApp.class).getContext().getResourceMap(BOSSConverterView.class);
    jTabbedPane1.setBackground(resourceMap.getColor("jTabbedPane1.background")); // NOI18N
    jTabbedPane1.setName("jTabbedPane1"); // NOI18N

    jDesktopPane1.setBackground(resourceMap.getColor("jDesktopPane1.background")); // NOI18N
    jDesktopPane1.setName("jDesktopPane1"); // NOI18N

    jFormattedTextField1.setText(resourceMap.getString("jFormattedTextField1.text")); // NOI18N
    jFormattedTextField1.setName("jFormattedTextField1"); // NOI18N
    jFormattedTextField1.setBounds(10, 120, 560, -1);
    jDesktopPane1.add(jFormattedTextField1, javax.swing.JLayeredPane.DEFAULT_LAYER);

    jFormattedTextField2.setText(resourceMap.getString("jFormattedTextField2.text")); // NOI18N
    jFormattedTextField2.setName("jFormattedTextField2"); // NOI18N
    jFormattedTextField2.setBounds(10, 40, 560, -1);
    jDesktopPane1.add(jFormattedTextField2, javax.swing.JLayeredPane.DEFAULT_LAYER);

    jTabbedPane1.addTab(resourceMap.getString("jDesktopPane1.TabConstraints.tabTitle"), jDesktopPane1); // NOI18N

    jDesktopPane2.setBackground(resourceMap.getColor("jDesktopPane2.background")); // NOI18N
    jDesktopPane2.setName("jDesktopPane2"); // NOI18N

    jScrollPane1.setName("jScrollPane1"); // NOI18N

    jTextArea1.setColumns(20);
    jTextArea1.setRows(5);
    jTextArea1.setName("jTextArea1"); // NOI18N
    jScrollPane1.setViewportView(jTextArea1);

    jScrollPane1.setBounds(10, 30, 760, 98);
    jDesktopPane2.add(jScrollPane1, javax.swing.JLayeredPane.DEFAULT_LAYER);

    jScrollPane2.setName("jScrollPane2"); // NOI18N

    jTextArea2.setColumns(20);
    jTextArea2.setRows(5);
    jTextArea2.setName("jTextArea2"); // NOI18N
    jScrollPane2.setViewportView(jTextArea2);

    jScrollPane2.setBounds(10, 160, 760, 97);
    jDesktopPane2.add(jScrollPane2, javax.swing.JLayeredPane.DEFAULT_LAYER);

    jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
    jButton1.setName("jButton1"); // NOI18N
    jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            tester(evt);
        }
    });
    jButton1.setBounds(610, 270, 93, 29);
    jDesktopPane2.add(jButton1, javax.swing.JLayeredPane.DEFAULT_LAYER);

    jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
    jLabel1.setName("jLabel1"); // NOI18N
    jLabel1.setBounds(30, 140, 330, 16);
    jDesktopPane2.add(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

    jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
    jLabel2.setName("jLabel2"); // NOI18N
    jLabel2.setBounds(20, 10, 330, 16);
    jDesktopPane2.add(jLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER);

    jTabbedPane1.addTab(resourceMap.getString("jDesktopPane2.TabConstraints.tabTitle"), jDesktopPane2); // NOI18N

    org.jdesktop.layout.GroupLayout mainPanelLayout = new org.jdesktop.layout.GroupLayout(mainPanel);
    mainPanel.setLayout(mainPanelLayout);
    mainPanelLayout.setHorizontalGroup(
        mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
        .add(org.jdesktop.layout.GroupLayout.TRAILING, jTabbedPane1)
    );
    mainPanelLayout.setVerticalGroup(
        mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
        .add(org.jdesktop.layout.GroupLayout.TRAILING, mainPanelLayout.createSequentialGroup()
            .add(jTabbedPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 375, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(36, Short.MAX_VALUE))
    );

    statusPanel.setName("statusPanel"); // NOI18N

    statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N

    statusMessageLabel.setName("statusMessageLabel"); // NOI18N

    statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
    statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

    progressBar.setName("progressBar"); // NOI18N

    org.jdesktop.layout.GroupLayout statusPanelLayout = new org.jdesktop.layout.GroupLayout(statusPanel);
    statusPanel.setLayout(statusPanelLayout);
    statusPanelLayout.setHorizontalGroup(
        statusPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
        .add(statusPanelSeparator, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 798, Short.MAX_VALUE)
        .add(statusPanelLayout.createSequentialGroup()
            .addContainerGap()
            .add(statusMessageLabel)
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 602, Short.MAX_VALUE)
            .add(progressBar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
            .add(statusAnimationLabel)
            .addContainerGap())
    );
    statusPanelLayout.setVerticalGroup(
        statusPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
        .add(statusPanelLayout.createSequentialGroup()
            .add(statusPanelSeparator, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .add(statusPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                .add(statusMessageLabel)
                .add(statusAnimationLabel)
                .add(progressBar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
            .add(3, 3, 3))
    );

    menuBar.setName("menuBar"); // NOI18N

    fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
    fileMenu.setName("fileMenu"); // NOI18N

    openFileMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.META_MASK));
    openFileMenuItem.setText(resourceMap.getString("openFileMenuItem.text")); // NOI18N
    openFileMenuItem.setName("openFileMenuItem"); // NOI18N
    openFileMenuItem.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            openFileMenuItemMouseClicked(evt);
        }
    });
    fileMenu.add(openFileMenuItem);

    javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(bossconverter.BOSSConverterApp.class).getContext().getActionMap(BOSSConverterView.class, this);
    exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
    exitMenuItem.setName("exitMenuItem"); // NOI18N
    fileMenu.add(exitMenuItem);

    menuBar.add(fileMenu);

    helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
    helpMenu.setName("helpMenu"); // NOI18N

    aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
    aboutMenuItem.setName("aboutMenuItem"); // NOI18N
    helpMenu.add(aboutMenuItem);

    menuBar.add(helpMenu);

    mainFrame.setJMenuBar(menuBar);

    org.jdesktop.layout.GroupLayout mainFrameLayout = new org.jdesktop.layout.GroupLayout(mainFrame.getContentPane());
    mainFrame.getContentPane().setLayout(mainFrameLayout);
    mainFrameLayout.setHorizontalGroup(
        mainFrameLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
        .add(0, 838, Short.MAX_VALUE)
        .add(mainFrameLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(mainFrameLayout.createSequentialGroup()
                .addContainerGap()
                .add(mainPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap()))
        .add(mainFrameLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(mainFrameLayout.createSequentialGroup()
                .addContainerGap()
                .add(statusPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap()))
    );
    mainFrameLayout.setVerticalGroup(
        mainFrameLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
        .add(0, 531, Short.MAX_VALUE)
        .add(mainFrameLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(mainFrameLayout.createSequentialGroup()
                .addContainerGap()
                .add(mainPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(100, Short.MAX_VALUE)))
        .add(mainFrameLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(mainFrameLayout.createSequentialGroup()
                .add(474, 474, 474)
                .add(statusPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
    );

    setComponent(mainPanel);
    setMenuBar(menuBar);
    setStatusBar(statusPanel);
}// </editor-fold>

private void tester(java.awt.event.MouseEvent evt) {
        }

private void openFileMenuItemMouseClicked(java.awt.event.MouseEvent evt) {

}

// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JDesktopPane jDesktopPane1;
private javax.swing.JDesktopPane jDesktopPane2;
private javax.swing.JFormattedTextField jFormattedTextField1;
private javax.swing.JFormattedTextField jFormattedTextField2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTabbedPane jTabbedPane1;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextArea jTextArea2;
private javax.swing.JFrame mainFrame;
private javax.swing.JPanel mainPanel;
private javax.swing.JMenuBar menuBar;
private javax.swing.JMenuItem openFileMenuItem;
private javax.swing.JProgressBar progressBar;
private javax.swing.JLabel statusAnimationLabel;
private javax.swing.JLabel statusMessageLabel;
private javax.swing.JPanel statusPanel;
// End of variables declaration

private final Timer messageTimer;
private final Timer busyIconTimer;
private final Icon idleIcon;
private final Icon[] busyIcons = new Icon[15];
private int busyIconIndex = 0;

private JDialog aboutBox;
}

我为长代码道歉,我不确定哪个部分可能是对于这种情况很重要,哪一个不是。

I apologize for the long code, I wasn't sure which part might be important to this case and which one is not.

如果你知道这里有什么问题,请告诉我

If you know what is the problem here, please let me know

推荐答案

你不应该创建一个额外的JFrame,你的面板已经在一个框架
你应该使用类似的东西而不是创建一个新的框架:

you shouldn't be creating an extra JFrame, your panel was already in a frameYou should have used something like this instead of creating a new frame:

getFrame().setResizeable(false);

这篇关于为什么Java jFrame setResizable()很难采取行动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 06:10