gpt4 book ai didi

java - 为什么我的 Java swing 应用程序行为异常?

转载 作者:行者123 更新时间:2023-11-30 06:36:26 26 4
gpt4 key购买 nike

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


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();
}

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

关于java - 为什么我的 Java swing 应用程序行为异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4862428/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com