我正在尝试为出售糕点,百吉饼和咖啡的当地百吉饼商店制作一个节目。我无法实现我在JFrame中制作的面板
了解如何使我的JFrame看起来像这样的任何帮助都会有所帮助http://kepler.covenant.edu/COS150/Bagel_files/image002.jpg
一旦我完成这项工作,我希望根据选择的产品改变中间面板组。我不完全确定如何使按钮和面板相互通信。
感谢您的帮助!
这是我到目前为止的JFrame代码。
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class BagelOrder extends JFrame
{
JLabel topLabel;
sizePanel sp = new sizePanel();
typePanel tp = new typePanel();
productsPanel pp = new productsPanel();
buttonPanel bp = new buttonPanel();
extrasPanel ep = new extrasPanel();
public BagelOrder()
{
setTitle("Order Entry Screen");
setSize(800, 800);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
topLabel = new JLabel("Order Entry Screen");
add(pp, BorderLayout.WEST);
add(topLabel, BorderLayout.NORTH);
add(bp, BorderLayout.SOUTH);
add(middleCoffeePanelSetBuild(), BorderLayout.CENTER);
add(east, BorderLayout.EAST);
setVisible(true);
}
private JPanel middleCoffeePanelSetBuild()
{
JPanel middlePanel = new JPanel(new GridLayout(3,1));
add(sp);
add(tp);
add(ep);
}
public static void main(String args[])
{
BagelOrder bo = new BagelOrder();
}
}
我没有错误,但除了页面顶部的标签外,我看不到任何其他内容。
这是我的其他类的代码
ButtonPanel
import java.awt.GridLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JPanel;
public class buttonPanel extends JPanel
{
JButton enterItemButton;
JPanel buttonPanel;
ButtonGroup bg;
JButton totalButton;
JButton newOrderButton;
public buttonPanel()
{
buttonPanel = new JPanel(new GridLayout(1,3));
bg = new ButtonGroup();
enterItemButton = new JButton("Enter Item");
totalButton = new JButton("Total");
newOrderButton = new JButton("New Order");
buttonPanel.setSize(150, 780);
buttonPanel.add(enterItemButton);
bg.add(enterItemButton);
buttonPanel.add(totalButton);
bg.add(totalButton);
buttonPanel.add(newOrderButton);
bg.add(newOrderButton);
setVisible(true);
}
}
ProductPanel
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.Border;
public class productsPanel extends JPanel
{
public productsPanel()
{
JPanel productPanel = new JPanel(new GridLayout(3,1));
ButtonGroup bg = new ButtonGroup();
JRadioButton coffeeButton = new JRadioButton("Coffee");
JRadioButton bagelButton = new JRadioButton("Bagel");
JRadioButton pastryButton = new JRadioButton("Pastry");
productPanel.setSize(150, 780);
productPanel.add(coffeeButton);
bg.add(coffeeButton);
productPanel.add(bagelButton);
bg.add(bagelButton);
productPanel.add(pastryButton);
bg.add(pastryButton);
Border etched = BorderFactory.createEtchedBorder();
Border titled = BorderFactory.createTitledBorder(etched, "Products");
productPanel.setBorder(titled);
setVisible(true);
}
}
尺寸面板
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.Border;
public class sizePanel extends JPanel{
public sizePanel()
{
JPanel sp = new JPanel(new GridLayout(3,1));
ButtonGroup bg = new ButtonGroup();
JRadioButton smallButton = new JRadioButton("Small");
JRadioButton mediumButton = new JRadioButton("Medium");
JRadioButton largeButton = new JRadioButton("Large");
sp.setSize(200, 200);
sp.add(smallButton);
bg.add(smallButton);
sp.add(mediumButton);
bg.add(mediumButton);
sp.add(largeButton);
bg.add(largeButton);
Border etched = BorderFactory.createEtchedBorder();
Border titled = BorderFactory.createTitledBorder(etched, "Size");
sp.setBorder(titled);
setVisible(true);
}
}
TypePanel
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.Border;
public class typePanel extends JPanel
{
public typePanel()
{
JPanel typePanel = new JPanel(new GridLayout(3,1));
ButtonGroup bg = new ButtonGroup();
JRadioButton regularButton = new JRadioButton("Regular");
JRadioButton decafButton = new JRadioButton("Decaf");
JRadioButton frenchRoastButton = new JRadioButton("French Roast");
typePanel.setSize(200, 200);
typePanel.add(regularButton);
bg.add(regularButton);
typePanel.add(decafButton);
bg.add(decafButton);
typePanel.add(frenchRoastButton);
bg.add(frenchRoastButton);
Border etched = BorderFactory.createEtchedBorder();
Border titled = BorderFactory.createTitledBorder(etched, "Type");
typePanel.setBorder(titled);
setVisible(true);
}
}
ExtrasPanel
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.border.Border;
public class extrasPanel extends JPanel
{
public extrasPanel()
{
JPanel extrasPanel = new JPanel(new GridLayout(2,1));
JCheckBox creamCheckBox = new JCheckBox("Cream");
JCheckBox sugarCheckBox = new JCheckBox("Sugar");
extrasPanel.setSize(200, 200);
extrasPanel.add(creamCheckBox);
extrasPanel.add(sugarCheckBox);
Border etched = BorderFactory.createEtchedBorder();
Border titled = BorderFactory.createTitledBorder(etched, "Extras");
extrasPanel.setBorder(titled);
setVisible(true);
}
}
答案 0 :(得分:4)
编写Swing GUI的一些好习惯:
1)您的Panel类构建错误。 您需要重新编写buttonPanel和所有其他人,以便他们这样读:
public buttonPanel() {
super(new GridLayout(1,3));
bg = new ButtonGroup();
enterItemButton = new JButton("Enter Item");
totalButton = new JButton("Total");
newOrderButton = new JButton("New Order");
this.setSize(150, 780);
this.add(enterItemButton);
bg.add(enterItemButton);
this.add(totalButton);
bg.add(totalButton);
this.add(newOrderButton);
bg.add(newOrderButton);
this.setVisible(true);
}
}
你当前编写的方式,你有一个ButtonPanel类(总是大写类),它有一个带有按钮的JPanel,但它从不显示它拥有的这个JPanel。你需要ButtonPanel成为JPanel,而不仅仅是拥有它。 这是主要问题。您可以改进的小问题:
2)JFrame有一个名为contentPane的JPanel。正如jzd所说,当使用JFrame.add时,你实际上是在调用JFrame.getContentPane.add(Component c)。这是一种方便的方法,但它隐藏了你真正在做的事情。特别是当你刚学习时,快捷方式很糟糕。
我个人更进一步,用
启动我的JFramesJPanel content = new JPanel(new BorderLayout());
content.add(...);
....
this.setContentPane(content);
3)扩展一个类(如JFrame)以调用super()开始是一种很好的做法。再一次,你可以把它作为速记,但开始时擅长迂腐是件好事。这会调用超类的构造函数(使用你需要的任何参数),这基本上是说“让我们创建基本的JFrame,然后将我的扩展应用到它”。
4)使用小写动词开始所有函数也是一种很好的做法。即ButtonPanel或build()
5)看起来你刚开始使用GUI,但我可能会推荐外部库“MigLayout”(google it)。它在额外的功能和易用性方面令人惊叹,它可以让你克服所有Java内置的,可怕的布局类型。
答案 1 :(得分:0)
他们使用ActionListener
界面,ActionPerformed(ActionEvent e)
方法相互交谈,并为了解雇您需要的方法<the component to be listened to>.addActionListener(theListener);