2011-02-01 80 views
2

当我尝试最大化窗口时,orinigal窗口呈现仍然存在,而另一个最大化的窗口出现使其变得凌乱。为什么我的Java swing应用程序行为异常?


import java.awt.BorderLayout; 
import java.awt.FlowLayout; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.GridLayout; 
import java.awt.Insets; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTabbedPane; 
import javax.swing.JTable; 
import javax.swing.table.DefaultTableModel; 
import javax.swing.*; 
import javax.swing.table.TableColumnModel; 


/** 
* @author ad * 
*/ 
public class Blotter { 

    private JFrame topFrame; 
    private JPanel mainContentPanel; 

    private JList unsubscribedFields; 
    private JList subscribedFields; 
    private JButton butSubscribe; 
    private JButton butUnsubscribe; 
    private JButton butApply; 
    private JButton butOk; 
    private JButton butCancel; 
    private JPanel panConfirm; 
    private JPanel panToggle; 
    private JPanel panBottom; 


    private JPanel panLeftList; 
    private JPanel panRightList; 
    private JPanel panSubcribe; 
    private JPanel panUnsubscribe; 


    /** 
    * @param args 
    */ 
    public Blotter(){ 
     topFrame = new JFrame("Subscription Fields"); 
     mainContentPanel = new JPanel(new BorderLayout()); 
     /* 
     butSubscribe = new JButton("-->"); 
     butUnsubscribe= new JButton("<--"); 
     butApply = new JButton("Apply"); 
     butOk = new JButton("OK"); 
     butCancel = new JButton("Cancel");*/ 
     createAndBuildGui(); 
    } 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     Blotter b = new Blotter(); 
     try { 
      UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     b.createAndBuildGui(); 
     b.fillGUI(); 
    } 


    private void fillGUI() { 
     String[] someRow = {"S110","200","100","42","32"}; 

    } 

    public void createAndBuildGui() 
    { 



     panConfirm = new JPanel(new GridLayout(1,3,5,5)); 
     panToggle = new JPanel(new GridBagLayout()); 
     panBottom = new JPanel(new FlowLayout()); 



     butApply = new JButton("Apply"); 
     butOk = new JButton("OK"); 
     butCancel = new JButton("Cancel"); 


     unsubscribedFields = new JList(); 
     subscribedFields = new JList(); 
     butSubscribe = new JButton(">>>"); 
     butUnsubscribe = new JButton("<<<"); 


     panSubcribe = new JPanel(new BorderLayout()); 
     panUnsubscribe = new JPanel(new BorderLayout()); 
     panLeftList = new JPanel(new BorderLayout()); 
     panRightList = new JPanel(new BorderLayout()); 


     // GridBagConstraints(int gridx, int gridy, int gridwidth, 
     // int gridheight, double weightx, double weighty, 
     // int anchor, int fill, Insets insets, int ipadx, int ipady) 


     panLeftList.add(unsubscribedFields, BorderLayout.CENTER); 
     panRightList.add(subscribedFields, BorderLayout.CENTER); 
     panSubcribe.add(butSubscribe, BorderLayout.SOUTH); 
     panUnsubscribe.add(butUnsubscribe, BorderLayout.NORTH); 
     panToggle.add(panLeftList, 
       new GridBagConstraints(0, 0, 1, 2, 0.5, 1, GridBagConstraints.CENTER, 
         GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0)); 

     panToggle.add(panRightList, 
       new GridBagConstraints(2, 0, 1, 2, 0.5, 1, GridBagConstraints.CENTER, 
         GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0)); 

     panToggle.add(panSubcribe, 
       new GridBagConstraints(1, 0, 1, 1, 0, 0.5, GridBagConstraints.SOUTH, 
         GridBagConstraints.NONE, new Insets(0, 2, 4, 4), 0, 0)); 
     panToggle.add(panUnsubscribe, 
       new GridBagConstraints(1, 1, 1, 1, 0, 0.5, GridBagConstraints.NORTH, 
         GridBagConstraints.NONE, new Insets(0, 2, 4, 4), 0, 0)); 



     // Building bottom OK Apply Cancel panel. 
     panConfirm.add(butApply, 0); 
     panConfirm.add(butOk, 1); 
     panConfirm.add(butCancel, 2); 

     panBottom = new JPanel(new FlowLayout(FlowLayout.RIGHT)); 
     panBottom.add(panConfirm, BorderLayout.EAST); 



     // building main content panel 
     mainContentPanel.add(panToggle,BorderLayout.CENTER); 
     mainContentPanel.add(panBottom, BorderLayout.SOUTH); 
     topFrame.add(mainContentPanel); 


     topFrame.pack(); 
     topFrame.setVisible(true); 

     topFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    } 

    protected void createPriceTable() { 
     // More fields : "Quantity Done","Quantity Open","PInst","State","Cancel State","Executing System","WaveID","Source System","TraderID","Average Price","LastExecutionPrice","ClientOrderTag","OldQuantityDone" 
     String[] columnNames = {"OrderId","Account","Symbol","Side","Quantity",}; 
     Object[][] o = new Object[0][columnNames.length]; 

    } 





} 
+0

我运行你的代码,这是什么“凌乱的事情”给你。 Swing做了什么代码说。 – PeterMmm 2011-02-01 12:25:28

+0

当我最大化时,原始窗口的小图像依然存在。另一个最大化的窗口即将出现。当用鼠标调整大小时,我想要一个调整大小的窗口。 – ada 2011-02-01 12:34:13

+0

@PeterMmm,创建并添加两次所有组件。看到我的答案。 – aioobe 2011-02-01 12:35:13

回答

0

这取决于你所使用的布局管理器。例如,BorderLayout知道在更改帧大小时重新呈现元素。不幸的是,你没有提到哪个窗口适用于你,哪些不适用,但是我在代码中看到了GridBagLayout。可能这种布局(更好地说这种布局的使用)可以防止类似的行为。

5

您打电话给createAndBuildGui()两次:一次在main,一次在构造函数中。

public Blotter(){ 
    topFrame = new JFrame("Subscription Fields"); 
    mainContentPanel = new JPanel(new BorderLayout()); 
    // ... 
    createAndBuildGui(); <-------- 
} 

 

public static void main(String[] args) { 
    Blotter b = new Blotter(); 
    // ... 
    b.createAndBuildGui(); <-------- 
    b.fillGUI(); 
} 

如果去掉其中之一,它工作得很好。

相关问题