为什么我的Java swing应用程序行为不端?

时间:2011-02-01 12:15:42

标签: java swing layout window-resize

当我尝试最大化窗口时,原始窗口渲染仍然存在,而另一个最大化窗口出现使其变得混乱。


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];

    }





}

2 个答案:

答案 0 :(得分: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();
}

如果您删除其中一个,它就可以正常工作。

答案 1 :(得分:0)

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