简体   繁体   English

JFrame和JPanel问题

[英]JFrame and JPanel problems

I am attempting to make a program for a local bagel shop that sells pastries, bagels, and coffee. 我正在尝试为一家当地的百吉饼商店制作程序,该商店出售糕点,百吉饼和咖啡。 I am having trouble implementing the panels that I have made into the JFrame 我在实现我在JFrame中制作的面板时遇到了麻烦

any help understanding how to make my JFrame look like this would be helpful http://kepler.covenant.edu/COS150/Bagel_files/image002.jpg 任何有助于理解如何使我的JFrame看起来像这样的帮助都会有所帮助http://kepler.covenant.edu/COS150/Bagel_files/image002.jpg

Once I have this working I hope to have the middle set of panels change depending on which product is selected. 完成这项工作后,我希望中间的面板组根据选择的产品而有所不同。 I am not entirely sure how to make the buttons and panels talk to one another. 我不太确定如何使按钮和面板彼此对话。

Thanks for the Help! 谢谢您的帮助!

here is my code for the JFrame so far. 这是到目前为止我的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();
}



} 

I have no errors but I cannot see anything other than the label at the top of the page. 我没有任何错误,但是除了页面顶部的标签外,看不到其他任何内容。

here is my code for the other classes 这是我其他班级的代码

ButtonPanel 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 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);   
}
}

Size Panel 尺寸面板

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 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 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);
}
}

Some good practices to get into when writing Swing GUI's: 编写Swing GUI时需要注意的一些好的做法:

1) Your Panel classes are built wrong. 1)您的Panel类构建错误。 You need to re-write buttonPanel, and all of your others, so that they read like this: 您需要重新编写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);

    }
}

The way yours is currently written, you have a ButtonPanel class (always capitalise classes) which posseses a JPanel with the buttons on it, but it never displays this JPanel that it owns. 当前编写方式,您有一个ButtonPanel类(始终为大写类),该类拥有一个带有按钮的JPanel,但它永远不会显示其拥有的JPanel。 You need ButtonPanel to BE a JPanel, not just own one. 您需要ButtonPanel成为JPanel,而不仅仅是拥有一个。 Thats the major issue. 多数民众赞成在主要问题。 Minor points you could improve on: 您可以改善的次要点:

2) The JFrame has a JPanel called the contentPane. 2)JFrame有一个名为contentPane的JPanel。 As said by jzd, when using JFrame.add you are really calling JFrame.getContentPane.add(Component c). 正如jzd所说,使用JFrame.add时,您实际上是在调用JFrame.getContentPane.add(Component c)。 This is a convenience method, but it hides what you are really doing. 这是一种方便的方法,但是它隐藏了您的实际操作。 Especially when you're just learning, shortcuts are bad. 特别是在您刚刚学习时,捷径是不好的。

I personally go a step further, starting my JFrames off with 我个人更进一步,从开始我的JFrames

JPanel content = new JPanel(new BorderLayout());
content.add(...);
....
this.setContentPane(content);

3) It is good practice when extending a class (like JFrame) to begin with a call to super(). 3)当扩展类(如JFrame)以对super()的调用开始时,这是一个好习惯。 Again, you can leave it out as shorthand, but its good to be pedantic when beginning. 同样,您可以将其省略为速记方式,但是在开始时花一点时间学习它是有益的。 This calls the superclass's constructor (with whatever arguments you need), which is basically saying "Lets create the basic JFrame, and then apply my extensions to it". 这将调用超类的构造函数(带有所需的任何参数),基本上是说“让我们创建基本的JFrame,然后对其应用扩展”。

4) Its also good practice to capitalise your classes, and begin all functions with lowercase verbs. 4)同样好的做法是大写您的类,并以小写动词开头所有功能。 ie ButtonPanel or build() 即ButtonPanel或build()

5) It seems like you're only just starting out with GUI, but might I recommend the external library "MigLayout" (google it). 5)似乎您只是刚刚开始使用GUI,但我可能会建议使用外部库“ MigLayout”(使用Google)。 Its amazing in the extra power and ease that it gives you over struggling against all of Java's inbuilt, horrible, layout types. 令人惊讶的是,它的强大功能和便捷性使您不必为所有Java内置的,可怕的布局类型而苦恼。

它们使用ActionListener接口, ActionPerformed(ActionEvent e)方法相互交谈,并且为了触发这些方法,您需要<the component to be listened to>.addActionListener(theListener);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM