我正在创建一个程序,它将包含一个MainFrame,其中包含一个顶部的面板,它包含两个并排的按钮(有点像一个非常独特的工具栏),然后在下面将是左侧的主面板(flipPanel)区域右边的另一个面板将是一个表单(flipFormPanel)。 flipPanel位于名为scrollPane的滚动窗格内,scrollPane位于MainFrame内的左侧区域。表单中的项目将被传递到名为itemPanel的新面板中,每次按下“确定”按钮时,该面板将被放置在flipPanel中。我希望itemPanel跨越flipPanel的宽度,但只有大约100像素高。
我无法让scrollPane填充MainFrame的左侧区域。在scrollPane填充此区域后,我需要flipPanel来填充scrollPane,然后使用scrollPane的整个宽度添加itemPanel,但只有100像素高。
表单flipFormPanel也没有按照我的意愿调整大小。它需要宽250像素,并且具有从工具栏底部到MainFrame底部的高度。
我不确定为什么scrollPane和flipFormPanel没有在MainFrame中正确调整大小,并且想知道你们是否可以帮助我。我是Swing的新手,我还在学习,所以如果你有任何指示,请不要阻止它们。
以下是我希望最终定位的图片,以防万一我没有充分解释:https://www.dropbox.com/s/nrqy7bsaabyqip5/FlipProgramLayout.png?dl=0
这是MainFrame:
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainFrame extends JFrame {
private ToolBar toolBar;
private FlipFormPanel flipFormPanel;
private LogFormPanel logFormPanel;
private FlipPanel flipPanel;
private LogPanel logPanel;
private MenuBarPanel menuBar;
private JPanel panelCards;
private JPanel flipCard;
private JPanel logCard;
private JPanel formCards;
private JPanel flipFormCard;
private JPanel logFormCard;
final static String FPANEL = "Flip Panel";
final static String FFORM = "Flip Form";
final static String LPANEL = "Log Panel";
final static String LFORM = "Log Form";
public MainFrame() {
super("Flipping Log");
setSize(960, 540);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
toolBar = new ToolBar();
flipFormPanel = new FlipFormPanel();
logFormPanel = new LogFormPanel();
flipPanel = new FlipPanel();
flipPanel.setPreferredSize(new Dimension(500, 500));
logPanel = new LogPanel();
logPanel.setPreferredSize(new Dimension(500, 500));
createCards();
layoutComponents();
// ToolBar to Switch Panels
toolBar.choosePanel(new PanelListener() {
public void panelChosen(String panel) {
if(panel.equals("Current Flips")) {
showCurrentFlips();
}
else if(panel.equals("Flipping Log")) {
showFlippingLog();
}
}
});
// Flip Form Panel
flipFormPanel.setFlipFormListener(new FlipFormListener() {
public void flipFormEventOccurred(FlipFormEvent e) {
String item = e.getItem();
String buyPrice = e.getBuyPrice();
String sellPrice = e.getSellPrice();
String quantity = e.getQuantity();
String pcBuyPrice = e.getPcBuyPrice();
String pcSellPrice = e.getPcSellPrice();
flipPanel.addItemPanel(item, buyPrice, sellPrice, quantity,
pcBuyPrice, pcSellPrice);
flipPanel.revalidate();
flipPanel.repaint();
}
});
}
public void showCurrentFlips() {
CardLayout c1 = (CardLayout)panelCards.getLayout();
CardLayout c2 = (CardLayout)formCards.getLayout();
c1.show(panelCards, "Flip Panel");
c2.show(formCards, "Flip Form");
}
public void showFlippingLog() {
CardLayout c1 = (CardLayout)panelCards.getLayout();
CardLayout c2 = (CardLayout)formCards.getLayout();
c1.show(panelCards, "Log Panel");
c2.show(formCards, "Log Form");
}
public void createCards(){
// Create the panel that contains the cards
panelCards = new JPanel(new CardLayout());
panelCards.add(flipPanel, "Flip Panel");
panelCards.add(logPanel, "Log Panel");
Dimension dim = new Dimension();
dim = panelCards.getPreferredSize();
dim.width = 500;
dim.height = 500;
panelCards.setPreferredSize(dim);
formCards = new JPanel(new CardLayout());
formCards.add(flipFormPanel, "Flip Form");
formCards.add(logFormPanel, "Log Form");
dim = formCards.getPreferredSize();
dim.width = 500;
dim.height = 500;
formCards.setPreferredSize(dim);
}
public void layoutComponents(){
setLayout(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
gc.fill = GridBagConstraints.NONE;
// First Row
gc.gridy = 0;
gc.weightx = 3.0;
gc.weighty = 0;
gc.gridwidth = 2;
gc.anchor = GridBagConstraints.CENTER;
gc.insets = new Insets(0, 0, 0, 0);
add(toolBar, gc);
// Second Row
gc.gridy = 1;
gc.weighty = 2.0;
gc.gridwidth = 1;
gc.gridx = 0;
gc.weightx = 1.0;
gc.anchor = GridBagConstraints.CENTER;
gc.insets = new Insets(0, 0, 0, 0);
add(panelCards, gc);
gc.gridx = 1;
gc.weightx = 0;
gc.anchor = GridBagConstraints.NORTHEAST;
gc.insets = new Insets(40, 0, 0, 0);
add(formCards, gc);
}
}
这是FlipFormPanel:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class FlipFormPanel extends JPanel {
private JComboBox itemCombo;
private JFormattedTextField buyPriceField;
private JFormattedTextField sellPriceField;
private JFormattedTextField quantityField;
private JFormattedTextField pcBuyPriceField;
private JFormattedTextField pcSellPriceField;
private JLabel itemLabel;
private JLabel buyPriceLabel;
private JLabel sellPriceLabel;
private JLabel quantityLabel;
private JLabel pcBuyPriceLabel;
private JLabel pcSellPriceLabel;
private JButton okBtn;
private FlipFormListener flipFormListener;
public FlipFormPanel() {
Dimension dim = getPreferredSize();
dim.width = 250;
dim.height = 500;
setPreferredSize(dim);
okBtn = new JButton("OK");
setupLabels();
setupComboBox();
createFormat();
createFormattedTextFields();
setupOkButton();
layoutComponents();
}
public void setupOkButton() {
okBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String item = (String) itemCombo.getSelectedItem();
String buyPrice = buyPriceField.getText();
String sellPrice = sellPriceField.getText();
String quantity = quantityField.getText();
String pcBuyPrice = pcBuyPriceField.getText();
String pcSellPrice = pcSellPriceField.getText();
FlipFormEvent ev = new FlipFormEvent(this, item, buyPrice,
sellPrice, quantity, pcBuyPrice, pcSellPrice);
if (flipFormListener != null) {
flipFormListener.flipFormEventOccurred(ev);
}
}
});
}
public void setFlipFormListener(FlipFormListener listener) {
this.flipFormListener = listener;
}
public void setupLabels() {
itemLabel = new JLabel("Item: ");
buyPriceLabel = new JLabel("Buy Price: ");
sellPriceLabel = new JLabel("Sell Price: ");
quantityLabel = new JLabel("Quantity: ");
pcBuyPriceLabel = new JLabel("PC Buy Price: ");
pcSellPriceLabel = new JLabel("PC Sell Price: ");
}
public void setupComboBox() {
itemCombo = new JComboBox();
DefaultComboBoxModel itemModel = new DefaultComboBoxModel();
itemModel.addElement("Bandos Helmet");
itemModel.addElement("Bandos Chestplate");
itemModel.addElement("Bandos Tassets");
itemModel.addElement("Bandos Gloves");
itemModel.addElement("Bandos Boots");
itemModel.addElement("Bandos Warshield");
itemModel.addElement("Armadyl Helmet");
itemModel.addElement("Armadyl Chestplate");
itemModel.addElement("Armadyl Chainskirt");
itemModel.addElement("Armadyl Gloves");
itemModel.addElement("Armadyl Boots");
itemModel.addElement("Armadyl Crossbow");
itemModel.addElement("Armadyl Buckler");
itemModel.addElement("Hood of Subjugation");
itemModel.addElement("Garb of Subjugation");
itemModel.addElement("Gown of Subjugation");
itemModel.addElement("Gloves of Subjugation");
itemModel.addElement("Boots of Subjugation");
itemModel.addElement("Ward of Subjugation");
itemCombo.setModel(itemModel);
itemCombo.setSelectedIndex(-1);
itemCombo.setEditable(false);
}
private DecimalFormat priceFormat;
private DecimalFormat quantityFormat;
public void createFormat() {
priceFormat = new DecimalFormat("###,##0.###k");
quantityFormat = new DecimalFormat("###,###");
}
public void createFormattedTextFields() {
buyPriceField = new JFormattedTextField(priceFormat);
buyPriceField.setColumns(10);
sellPriceField = new JFormattedTextField(priceFormat);
sellPriceField.setColumns(10);
quantityField = new JFormattedTextField(quantityFormat);
quantityField.setColumns(10);
pcBuyPriceField = new JFormattedTextField(priceFormat);
pcBuyPriceField.setColumns(10);
pcSellPriceField = new JFormattedTextField(priceFormat);
pcSellPriceField.setColumns(10);
}
public void layoutComponents() {
setLayout(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
gc.fill = GridBagConstraints.NONE;
// First Row
gc.gridy = 0;
gc.weighty = 1;
gc.gridwidth = 1;
gc.weightx = 1;
gc.gridx = 0;
gc.anchor = GridBagConstraints.EAST;
gc.insets = new Insets(3, 3, 10, 3);
add(itemLabel, gc);
gc.weightx = 1;
gc.gridx = 1;
gc.anchor = GridBagConstraints.CENTER;
gc.insets = new Insets(3, 3, 10, 3);
add(itemCombo, gc);
// Second Row
gc.gridy = 1;
gc.weighty = 1;
gc.gridwidth = 1;
gc.weightx = 1;
gc.gridx = 0;
gc.anchor = GridBagConstraints.EAST;
gc.insets = new Insets(3, 3, 3, 3);
add(buyPriceLabel, gc);
gc.weightx = 1;
gc.gridx = 1;
gc.anchor = GridBagConstraints.CENTER;
gc.insets = new Insets(3, 3, 3, 3);
add(buyPriceField, gc);
// Third Row
gc.gridy = 2;
gc.weighty = 1;
gc.gridwidth = 1;
gc.weightx = 1;
gc.gridx = 0;
gc.anchor = GridBagConstraints.EAST;
gc.insets = new Insets(3, 3, 3, 3);
add(sellPriceLabel, gc);
gc.weightx = 1;
gc.gridx = 1;
gc.anchor = GridBagConstraints.CENTER;
gc.insets = new Insets(3, 3, 3, 3);
add(sellPriceField, gc);
// Fourth Row
gc.gridy = 3;
gc.weighty = 1;
gc.gridwidth = 1;
gc.weightx = 1;
gc.gridx = 0;
gc.anchor = GridBagConstraints.EAST;
gc.insets = new Insets(3, 3, 3, 3);
add(quantityLabel, gc);
gc.weightx = 1;
gc.gridx = 1;
gc.anchor = GridBagConstraints.CENTER;
gc.insets = new Insets(3, 3, 3, 3);
add(quantityField, gc);
// Fifth Row
gc.gridy = 4;
gc.weighty = 1;
gc.gridwidth = 1;
gc.weightx = 1;
gc.gridx = 0;
gc.anchor = GridBagConstraints.EAST;
gc.insets = new Insets(3, 3, 3, 3);
add(pcBuyPriceLabel, gc);
gc.weightx = 1;
gc.gridx = 1;
gc.anchor = GridBagConstraints.CENTER;
gc.insets = new Insets(3, 3, 3, 3);
add(pcBuyPriceField, gc);
// Sixth Row
gc.gridy = 5;
gc.weighty = 1;
gc.gridwidth = 1;
gc.weightx = 1;
gc.gridx = 0;
gc.anchor = GridBagConstraints.EAST;
gc.insets = new Insets(3, 3, 3, 3);
add(pcSellPriceLabel, gc);
gc.weightx = 1;
gc.gridx = 1;
gc.anchor = GridBagConstraints.CENTER;
gc.insets = new Insets(3, 3, 3, 3);
add(pcSellPriceField, gc);
// Seventh Row
gc.gridy = 6;
gc.weighty = 1;
gc.gridwidth = 2;
gc.weightx = 1;
gc.anchor = GridBagConstraints.CENTER;
gc.insets = new Insets(3, 3, 3, 3);
add(okBtn, gc);
}
}
这是FlipPanel:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class FlipPanel extends JPanel {
private JPanel mainPanel;
private JScrollPane scrollPane;
private ItemPanel itemPanel;
private JLabel lbl;
public FlipPanel() {
Dimension dim = getPreferredSize();
dim.width = 500;
dim.height = 500;
setPreferredSize(dim);
setLayout(new BorderLayout());
// lbl = new JLabel("Flip");
// add(lbl);
mainPanel = new JPanel();
JScrollPane scrollPane = new JScrollPane(mainPanel);
dim = scrollPane.getPreferredSize();
dim.width = 500;
dim.height = 500;
scrollPane.setPreferredSize(dim);
add(scrollPane, BorderLayout.CENTER);
mainPanel.setLayout(new GridLayout(0, 1));
ItemPanel firstItemPanel = new ItemPanel();
dim = firstItemPanel.getPreferredSize();
dim.width = 500;
dim.height = 100;
firstItemPanel.setPreferredSize(dim);
mainPanel.add(firstItemPanel);
}
public void addItemPanel(String item, String buyPrice, String sellPrice,
String quantity, String pcBuyPrice, String pcSellPrice) {
this.itemPanel = new ItemPanel(item, buyPrice, sellPrice, quantity,
pcBuyPrice, pcSellPrice);
mainPanel.add(itemPanel);
Dimension dim = this.itemPanel.getPreferredSize();
dim.width = 500;
dim.height = 100;
this.itemPanel.setPreferredSize(dim);
}
public void addItemPanel() {
this.itemPanel = new ItemPanel();
mainPanel.add(itemPanel);
}
}
这是ItemPanel:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class ItemPanel extends JPanel {
private JTextArea textArea;
private String item;
private String buyPrice;
private String sellPrice;
private String quantity;
private String pcBuyPrice;
private String pcSellPrice;
private JLabel itemLabel;
private JLabel buyPriceLabel;
private JLabel sellPriceLabel;
private JLabel quantityLabel;
private JLabel pcBuyPriceLabel;
private JLabel pcSellPriceLabel;
public ItemPanel() {
setLayout(new FlowLayout());
Dimension dim = getPreferredSize();
dim.height = 100;
dim.width = 500;
setPreferredSize(dim);
itemLabel = new JLabel("ITEM: ");
buyPriceLabel = new JLabel("BUY PRICE: ");
sellPriceLabel = new JLabel("SELL PRICE: ");
quantityLabel = new JLabel("QUANTITY: ");
pcBuyPriceLabel = new JLabel("PC BUY PRICE: ");
pcSellPriceLabel = new JLabel("PC SELL PRICE: ");
add(itemLabel);
add(buyPriceLabel);
add(sellPriceLabel);
add(quantityLabel);
add(pcBuyPriceLabel);
add(pcSellPriceLabel);
}
public ItemPanel(String item, String buyPrice,
String sellPrice, String quantity, String pcBuyPrice,
String pcSellPrice) {
this.item = item;
this.buyPrice = buyPrice;
this.sellPrice = sellPrice;
this.quantity = quantity;
this.pcBuyPrice = pcBuyPrice;
this.pcSellPrice = pcSellPrice;
setLayout(new BorderLayout());
Dimension dim = getPreferredSize();
dim.height = 100;
dim.width = 100;
setPreferredSize(dim);
itemLabel = new JLabel("ITEM: ");
buyPriceLabel = new JLabel("BUY PRICE: ");
sellPriceLabel = new JLabel("SELL PRICE: ");
quantityLabel = new JLabel("QUANTITY: ");
pcBuyPriceLabel = new JLabel("PC BUY PRICE: ");
pcSellPriceLabel = new JLabel("PC SELL PRICE: ");
add(itemLabel);
add(buyPriceLabel);
add(sellPriceLabel);
add(quantityLabel);
add(pcBuyPriceLabel);
add(pcSellPriceLabel);
}
}
非常感谢你。
答案 0 :(得分:1)
您需要使用大小提示和布局管理器的组合。
以下内容将每个面板设置为首选,最小尺寸设置为100
,但使用GridBagLayout
允许FlipPanel
扩展超出这些基本限制...
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Rectangle;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.Scrollable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JScrollPane(new TestPane()));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private GridBagConstraints gbc;
public TestPane() {
setLayout(new GridBagLayout());
gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.anchor = GridBagConstraints.WEST;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
add(createPanel("I be the flip panel"), gbc);
gbc.weighty = 0;
add(createPanel("Item Pane 01"), gbc, getComponentCount() - 1);
add(createPanel("Item Pane 02"), gbc, getComponentCount() - 1);
add(createPanel("Item Pane 03"), gbc, getComponentCount() - 1);
}
protected JPanel createPanel(String text) {
return new SubPanel(text);
}
public class SubPanel extends JPanel {
public SubPanel(String text) {
setLayout(new BorderLayout());
add(new JLabel(text));
setBorder(new LineBorder(Color.RED));
}
@Override
public Dimension getPreferredSize() {
Dimension size = super.getPreferredSize();
size.height = 100;
return size;
}
@Override
public Dimension getMinimumSize() {
return getPreferredSize();
}
}
}
}
有关详细信息,请参阅Laying Out Components Within a Container和How to Use GridBagLayout