在我的项目中,我将JFrame用作主应用程序窗口,并将JDesktopPane放置在此JFrame中。我将MigLayout设置为JDesktopPane,现在的问题是我已经能够在此JDesktopPane上显示JInternalFrame,但无法调整此JInternalFrame的大小,在这种情况下是否可以更改JInternalFrame的大小?谢谢。
package Design;
//Header Files
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import net.miginfocom.swing.MigLayout;
public class MainClass extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
MainClass frame = new MainClass();
Toolkit toolkit = Toolkit.getDefaultToolkit();
int xAxisSize = ((int) toolkit.getScreenSize().getWidth());
int yAxisSize = ((int) toolkit.getScreenSize().getHeight());
frame.setSize(xAxisSize, yAxisSize);
frame.setVisible(true);
frame.setVisible(true);
}
/**
* Create the frame.
*/
public MainClass() {
setAlwaysOnTop(true);
final JDesktopPane desktopPane = new JDesktopPane();
desktopPane.setLayout(new MigLayout("center panel",
"[100px:100px:1366px,grow,shrink 50,center]",
"[100px:100px:768px,grow,shrink 50,center]"));
desktopPane.setBorder(new LineBorder(new Color(0, 0, 0)));
desktopPane.setBackground(Color.WHITE);
setForeground(Color.WHITE);
setTitle("JFrame");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setPreferredSize(screenSize);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu mnNewMenu = new JMenu("Select");
menuBar.add(mnNewMenu);
JMenuItem mntmGrooups = new JMenuItem("Grooups");
mntmGrooups.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
GroupGUIForm form = new GroupGUIForm();//Here i am calling another jinternalframe
desktopPane.removeAll();
desktopPane.repaint();
form.pack();
form.setVisible(true);
desktopPane.add(form);
}
});
mnNewMenu.add(mntmGrooups);
JSeparator separator = new JSeparator();
mnNewMenu.add(separator);
JSeparator separator_2 = new JSeparator();
mnNewMenu.add(separator_2);
JSeparator separator_1 = new JSeparator();
mnNewMenu.add(separator_1);
JSeparator separator_3 = new JSeparator();
mnNewMenu.add(separator_3);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new MigLayout("", "[grow,shrink 50,fill]",
"[grow]"));
contentPane.add(desktopPane, "cell 0 0,grow");
}
}
package Design;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import net.miginfocom.swing.MigLayout;
public class GroupGUIForm extends JInternalFrame {
private JPanel groupMainContentPane;
private JTable jTableGroupNameList;
private JTextField textField;
private JTextField jTextFieldGroupName;
private JTextField jTextFieldGroupAlias;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GroupGUIForm frame = new GroupGUIForm();//main method
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public GroupGUIForm() {
setMaximizable(true);
setClosable(true);
setResizable(true);
setFont(new Font("SansSerif", Font.PLAIN, 12));
setVisible(true);
setTitle("Create & Modify Group Information");
setBounds(100, 100, 700, 450);
groupMainContentPane = new JPanel();
groupMainContentPane.setForeground(new Color(0, 0, 0));
groupMainContentPane.setBorder(new TitledBorder(null, "GroupForm", TitledBorder.LEADING, TitledBorder.TOP, null, Color.GRAY));
setContentPane(groupMainContentPane);
groupMainContentPane
.setLayout(new MigLayout("", "[][100px:100px:100px,grow,fill][225px:225px:225px,grow,fill][10px:10px:10px,grow,fill][][grow]", "[20px:20px:20px,grow,shrink 50][20px:20px:20px,grow,shrink 50][20px:20px:20px,grow,shrink 50][20px:20px:20px,grow,shrink 50][20px:20px:20px,grow,shrink 50][20px:20px:20px,grow,shrink 50][20px:20px:20px,grow,shrink 50][20px:20px:20px,grow,shrink 50][20px:20px:20px,grow,shrink 50][20px:20px:20px,grow,shrink 50][20px:20px:20px,grow,shrink 50][20px:20px:20px,grow,shrink 50][][][grow][]"));
JPanel jTablePanel = new JPanel();
jTablePanel.setBorder(new TitledBorder(null, "Available Group List",
TitledBorder.LEADING, TitledBorder.TOP, null, null));
groupMainContentPane.add(jTablePanel, "cell 5 1 1 15,grow");
jTablePanel.setLayout(new MigLayout("", "[235px]", "[][321px]"));
JLabel lblSearch = new JLabel("Search");
jTablePanel.add(lblSearch, "flowx,cell 0 0");
JScrollPane jTableGroupNameScrollPane = new JScrollPane();
jTablePanel.add(jTableGroupNameScrollPane, "cell 0 1,grow");
jTableGroupNameList = new JTable();
jTableGroupNameList.setFont(new Font("SansSerif", Font.PLAIN, 11));
jTableGroupNameList
.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
jTableGroupNameList.setName("");
jTableGroupNameList.setAutoCreateRowSorter(true);
jTableGroupNameList.setBorder(new LineBorder(new Color(0, 0, 0)));
jTableGroupNameList.setModel(new DefaultTableModel(new Object[][] {
{ "John", null }, { "Bill", null }, { "Edmond", null },
{ "Adam", null }, { "", null },
{ null, null }, { null, null }, { null, null }, { null, null },
{ null, null }, { null, null }, { null, null }, { null, null },
{ null, null }, { null, null }, { null, null }, { null, null },
{ null, null }, { null, null }, { null, null }, },
new String[] { "Avaliable Group Names", "" }));
jTableGroupNameList.getColumnModel().getColumn(1).setPreferredWidth(15);
jTableGroupNameList.getColumnModel().getColumn(1).setMinWidth(0);
jTableGroupNameList.getColumnModel().getColumn(1).setMaxWidth(0);
jTableGroupNameScrollPane.setViewportView(jTableGroupNameList);
textField = new JTextField();
jTablePanel.add(textField, "cell 0 0");
textField.setColumns(20);
JLabel lblNewLabel = new JLabel("Group Name:");
groupMainContentPane.add(lblNewLabel, "cell 1 3,alignx trailing");
jTextFieldGroupName = new JTextField();
groupMainContentPane.add(jTextFieldGroupName, "cell 2 3,growx");
jTextFieldGroupName.setColumns(10);
JLabel lblNewLabel_1 = new JLabel("Group Alilas:");
groupMainContentPane.add(lblNewLabel_1, "cell 1 6,alignx trailing");
jTextFieldGroupAlias = new JTextField();
groupMainContentPane.add(jTextFieldGroupAlias, "cell 2 6,growx");
jTextFieldGroupAlias.setColumns(10);
JLabel lblNewLabel_2 = new JLabel("Group Under:");
groupMainContentPane.add(lblNewLabel_2, "cell 1 9,alignx trailing");
JComboBox jComboBoxGroupUnder = new JComboBox();
groupMainContentPane.add(jComboBoxGroupUnder, "cell 2 9,growx");
JPanel jButtonsPanel = new JPanel();
groupMainContentPane
.add(jButtonsPanel, "cell 1 12 5 1,grow,dock south");
jButtonsPanel.setLayout(new MigLayout("",
"[55px][53px][63px][65px][][][]", "[23px]"));
JButton btnBack = new JButton("Back");
jButtonsPanel.add(btnBack, "cell 1 0,alignx left,aligny top");
JButton btnNew = new JButton("New");
jButtonsPanel.add(btnNew, "cell 2 0,alignx left,aligny top");
JButton btnDelete = new JButton("Delete");
jButtonsPanel.add(btnDelete, "cell 3 0,alignx left,aligny top");
JButton btnSubmit = new JButton("Submit");
jButtonsPanel.add(btnSubmit, "cell 6 0,alignx left,aligny top");
}
public JLabel getLblNewLabel() {
return getLblNewLabel();
}
public JPanel getGroupMainContentPane() {
return groupMainContentPane;
}
public JTable getTable() {
return jTableGroupNameList;
}
}
最佳答案
问题是您正在JDesktopPane上设置布局。只需通过设置大小和位置将表格放在桌面窗格的中央即可。 JDesktopPane的要点是允许用户移动和调整组件大小,在窗格上设置MiGLayout可以克服这一点。
另外,您的SSCCE可能很长,其中包含很多垃圾。这是您的SSCCE可能看起来像的示例:
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import net.miginfocom.swing.MigLayout;
public class MainClass {
public static void main(String[] args) {
JFrame frame = new JFrame ();
frame.setVisible(true);
frame.setAlwaysOnTop(true);
final JDesktopPane desktopPane = new JDesktopPane();
desktopPane.setLayout(new MigLayout("center panel",
"[100px:100px:1366px,grow,shrink 50,center]",
"[100px:100px:768px,grow,shrink 50,center]"));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JInternalFrame form = new JInternalFrame();
form.setResizable(true);
form.setVisible(true);
desktopPane.add(form);
frame.add(desktopPane);
}
}