簡體   English   中英

為什么我的Java swing應用程序行為異常?

[英]Why is my Java swing application misbehaving?

當我嘗試最大化窗口時,原始的窗口渲染將保留,而另一個最大化的窗口則顯示為混亂。


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

    }





}

您兩次調用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();
}

如果您刪除其中之一,則效果很好。

這取決於您使用的布局管理器。 例如,BorderLayout知道在更改幀大小時可以重新渲染元素。 不幸的是,您沒有提到哪個窗口適合您,哪個窗口卻沒有,但是我在您的代碼中看到了GridBagLayout。 此布局(最好是使用此布局)可能會阻止類似的行為。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM