Basically, this is the structure of my Window.
The actual window looks like this:
My problem is that I want the MainPanel
with the ComboBox
and and the rest to fit the JFrame
window. Stretch horizontally and stick to the top. But I've tried all kinds of layouts (Box, GridBag, Grid). It just remains centered. How do I achieve this?
Here is the MVCC:
import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.*;
import javax.swing.UIManager.LookAndFeelInfo;
public class simplelayout{
JFrame mainFrame;
JPanel mainPanel;
JPanel innerPanel;
JPanel createDomainPanel;
JPanel listDomainPanel = new JPanel(new GridBagLayout());
String comboBoxItems[]={"Create Domain","List Domains"};
@SuppressWarnings("rawtypes")
JComboBox listCombo;
JButton goBtn;
CardLayout layout;
JTextField domainName = new JTextField();
JLabel successMessage = new JLabel("");
public simplelayout(){
initialize();
setCardLayout();
}
@SuppressWarnings({ "rawtypes", "unchecked" })
private void setCardLayout() {
innerPanel = new JPanel();
//innerPanel.setSize(600, 400);
layout = new CardLayout(10,10);
innerPanel.setLayout(layout);
//Default Panel
JPanel defaultPanel = new JPanel(new FlowLayout());
defaultPanel.add(new JLabel("Welcome to Amazon Simple DB"));
//Create Domain Panel
createDomainPanel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
//createDomainPanel.setSize(600, 300);
gbc.fill=GridBagConstraints.HORIZONTAL;
gbc.gridx=0;
gbc.gridy=0;
createDomainPanel.add(new JLabel("Enter the name of the domain"), gbc);
gbc.gridx=0;
gbc.gridy=1;
createDomainPanel.add(domainName, gbc);
JPanel result = new JPanel(new FlowLayout());
result.add(successMessage);
gbc.anchor=GridBagConstraints.LAST_LINE_START;
gbc.gridx=0;
gbc.gridy=2;
createDomainPanel.add(result, gbc);
//List Domain Panel
listDomainPanel.add(new JLabel("List of Domains"), gbc);
//Add to innerPanel
innerPanel.add(defaultPanel, "Default");
innerPanel.add(createDomainPanel, "Create Domain");
innerPanel.add(listDomainPanel, "List Domain");
layout.show(innerPanel, "Default");
DefaultComboBoxModel panelName = new DefaultComboBoxModel();
panelName.addElement("Create Domain");
panelName.addElement("List Domain");
listCombo = new JComboBox(panelName);
listCombo.setSelectedIndex(0);
JScrollPane listComboScroll = new JScrollPane(listCombo);
goBtn = new JButton("Go");
mainPanel.setLayout(new GridBagLayout());
GridBagConstraints gbc1 = new GridBagConstraints();
gbc1.fill=GridBagConstraints.HORIZONTAL;
gbc1.anchor=GridBagConstraints.FIRST_LINE_START;
gbc1.gridx = 0;
gbc1.gridx = 0;
mainPanel.add(listComboScroll, gbc1);
gbc1.gridy = 1;
mainPanel.add(goBtn, gbc1);
gbc1.gridy = 2;
mainPanel.add(innerPanel, gbc1);
GridBagConstraints gbc2 = new GridBagConstraints();
gbc2.fill=GridBagConstraints.HORIZONTAL;
gbc2.anchor=GridBagConstraints.FIRST_LINE_START;
JScrollPane scr = new JScrollPane(mainPanel);
mainPanel.setSize(mainFrame.getPreferredSize());
mainFrame.add(scr, gbc);
mainFrame.setVisible(true);
mainFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
}
private void initialize() {
mainFrame = new JFrame("Amazon Simple DB");
mainFrame.setSize(700, 500);
mainFrame.setLayout(new GridBagLayout());
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainPanel = new JPanel();
//mainPanel.setLayout(new GridLayout(8,1));
mainFrame.setVisible(true);
}
public static void main(String[] args) {
try {
//UIManager.put("nimbusBase", new Color(178,56,249));
//UIManager.put("nimbusBlueGrey", new Color(102,212,199));
//UIManager.put("control", new Color(212,133,102));
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception e) {
// If Nimbus is not available, you can set the GUI to another look and feel.
}
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new simplelayout();
}
});
}
}
Try to add a mainFrame.pack();
in the initialize()
and the setCardLayout()
methods, Remove all mainFrame.setSize()
and mainPanel.setSize()
methods.
听起来您想使用应用程序框架的默认BorderLayout
,将combobox和button组件放置在BorderLayout.PAGE_START
位置的嵌套面板中,将innerPanel
置于CENTER
位置。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.