我在定位拆分窗格组件时遇到问题,它们没有正确调整大小以最小化和最大化窗口我如何将它们设置为适合框架的中心
这种代码和平的问题
jsp.setPreferredSize(new Dimension(300,300));
jsp1.setPreferredSize(getMaximumSize());
panel3.setPreferredSize(new Dimension(175,200));
splitPane.setPreferredSize(getMaximumSize());
jsp2.setPreferredSize(getMaximumSize());
jsp3.setPreferredSize(getMaximumSize());
jsp2.setMinimumSize(minimumSize);
jsp3.setMinimumSize(minimumSize);
这是我的代码
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
public class MyGui1 extends JPanel {
private static JLabel label1;
JMenuBar menuBar;
JMenu menu;
static JButton addfile;
JToolBar toolBar =null;
static int shut=0;
static int load=0;
protected JTextField textField;
protected static JPanel panel;
protected static JPanel panel1,panel2,panel3,splitpanel1,splitpanel2;
protected static JTextArea textarea;
protected JTextArea textarea1;
static JScrollPane jsp,jsp1,jsp2,jsp3;
static JFrame frame;
static JLabel jLabel=null;
private JSplitPane splitPane,splitPane1,splitpane2;
static File file=null;
static int gridvalue=0;
static GridBagConstraints grid=new GridBagConstraints();
public MyGui1() {
//to lay out the container's components in a rectangular grid
super(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridwidth = GridBagConstraints.REMAINDER;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridwidth = GridBagConstraints.REMAINDER;
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty = 1.0;
panel=new JPanel();
panel.setLayout(new BoxLayout(panel,BoxLayout.PAGE_AXIS));
panel2=new JPanel();
panel2.setLayout(new BoxLayout(panel2,BoxLayout.PAGE_AXIS));
panel3=new JPanel();
panel3.setLayout(new BoxLayout(panel3,BoxLayout.PAGE_AXIS));
jsp1=new JScrollPane(panel2);
jsp = new JScrollPane(panel);
splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
jsp, jsp1);
jsp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
jsp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
jsp1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
jsp1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
jsp.setPreferredSize(new Dimension(300,300));
jsp1.setPreferredSize(getMaximumSize());
panel3.setPreferredSize(new Dimension(175,200));
splitPane.setPreferredSize(getMaximumSize());
setBackground(Color.white);
//Provide minimum sizes for the two components in the split pane.
Dimension minimumSize = new Dimension(100, 100);
jsp.setMinimumSize(minimumSize);
jsp1.setMinimumSize(minimumSize);
splitpanel1=new JPanel();
splitpanel2=new JPanel();
splitpanel1.setLayout(new BoxLayout(splitpanel1,BoxLayout.PAGE_AXIS));
splitpanel2.setLayout(new BoxLayout(splitpanel2,BoxLayout.PAGE_AXIS));
jsp2=new JScrollPane(splitpanel1);
jsp3 = new JScrollPane(splitpanel2);
jsp2.setPreferredSize(getMaximumSize());
jsp3.setPreferredSize(getMaximumSize());
jsp2.setMinimumSize(minimumSize);
jsp3.setMinimumSize(minimumSize);
jsp2.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
jsp3.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
splitpane2=new JSplitPane(JSplitPane.VERTICAL_SPLIT,jsp2,jsp3);
panel3.add(splitpane2);
splitPane1=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,panel3,splitPane);
splitPane1.setPreferredSize(new Dimension(300,300));
add(splitPane1,c);
setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
label1=new JLabel(" ");
label1.setPreferredSize(new Dimension(280,20));
}
private static void createAndShowGUI() {
//Create and set up the window.
frame = new JFrame("Desktop");
frame.setLayout(new BorderLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//adding window listener for exit operation
frame.addWindowListener( new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
JFrame frame = (JFrame)e.getSource();
int result = JOptionPane.showConfirmDialog(
frame,
"Are you sure you want to exit the application?",
"Exit Application",
JOptionPane.YES_NO_OPTION);
if (result == JOptionPane.YES_OPTION)
{
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
else if(result==JOptionPane.NO_OPTION){
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
}
}
});
//Add contents to the window GUI part and to perform all operations
frame.add(new MyGui1());
//Display the window.
frame.pack();
//to keep the frame visible
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event dispatch thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
提前致谢