【发布时间】:2014-11-30 22:53:56
【问题描述】:
我正在创建一个程序,它将有一个 MainFrame,它包含一个顶部的面板,其中包含并排的两个按钮(有点像一个非常独特的工具栏),然后下面将是左侧的主面板(flipPanel)区域右边的另一个面板将是一个表单(flipFormPanel)。 FlipPanel 位于名为 scrollPane 的滚动窗格内,而 scrollPane 位于 MainFrame 内的左侧区域。表单中的项目将被传递到一个名为 itemPanel 的新面板中,然后每次按下 OK 按钮时都会将其放置在 FlipPanel 中。我希望 itemPanel 跨越 FlipPanel 的宽度,但高度仅为 100 像素左右。
我无法让 scrollPane 填充 MainFrame 的左侧区域。在scrollPane 填满这个区域后,我需要flipPanel 来填满scrollPane,然后让itemPanels 添加scrollPane 的全宽但只有100 像素高。
flipFormPanel 表单的大小也不是我想要的。它必须是 250 像素宽,并且从工具栏底部到 MainFrame 底部的高度。
我不确定为什么 scrollPane 和 flipFormPanel 在 MainFrame 中没有正确调整大小,我想知道你们是否可以帮助我。我是 Swing 新手,还在学习中,所以如果您有任何建议,请不要阻止。
这是我希望最终定位的图像,以防我解释得不够清楚:https://www.dropbox.com/s/nrqy7bsaabyqip5/FlipProgramLayout.png?dl=0
这是主框架:
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);
}
}
这里是翻转面板:
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);
}
}
这里是项目面板:
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);
}
}
非常感谢。
【问题讨论】:
-
如何覆盖 getPreferredSize()?
-
对于example。
标签: java swing size jscrollpane jlabel