为什么Java jFrame setResizable()很难采取行动?

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

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") //  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); }//  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; } 

我为长代码道歉,我不确定哪个部分可能对这个案件很重要,哪个部分不重要。

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

默认应用程序未包含在jFrame中! 它只在jPanel中,我不知道如何使它无法重新resize。 所以我添加了一个jFrame

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

 getFrame().setResizeable(false); 

你的代码有错误; 我简单的自包含测试程序显示setResizable(false)工作得很好:

 package com.businesslink.core.tst.ld.dct; import java.awt.*; import javax.swing.*; public class SwgTestFrame1b extends JFrame { public SwgTestFrame1b() { super("SwgTestFrame1b"); setBackground(Color.white); setResizable(false); pack(); } static public void main(String[] args) { new SwgTestFrame1b().show(); } } 

快速搜索您的代码,无法在mainFrame上调用show或setVisible; 看起来您只显示与mainFrame相关的对话框。 现在setResizable只在mainFrame上完成,不会影响任何作为mainFrame子元素的对话框。 它也不会影响FrameView使用的框架。


PS,只是FTR,我绝对厌恶不可resize的窗口,它们是GUI工具包或懒惰程序员的标志。

fill code((java.awt.Frame)root).setResizable(false); 在应用程序类中,完全在方法configureWindow中 。 (应用程序类是实例化View类的类。)它应该有所帮助