【问题标题】:JFrame and JPanel problemsJFrame 和 JPanel 问题
【发布时间】:2011-06-29 12:40:57
【问题描述】:

我正在尝试为当地一家出售糕点、百吉饼和咖啡的百吉饼店制作程序。我在实现我在 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();
}



} 

我没有错误,但除了页面顶部的标签外,我什么也看不到。

这是我其他类的代码

按钮面板

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

}
}

产品面板

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

类型面板

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

附加面板

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

【问题讨论】:

    标签: java swing jframe jpanel


    【解决方案1】:

    在编写 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,而不仅仅是拥有一个 JPanel。 那是主要问题。您可以改进的小点:

    2) JFrame 有一个名为 contentPane 的 JPanel。正如 jzd 所说,使用 JFrame.add 时,您实际上是在调用 JFrame.getContentPane.add(Component c)。这是一种方便的方法,但它隐藏了你真正在做什么。尤其是刚开始学习的时候,走捷径很糟糕。

    我个人更进一步,开始我的 JFrames

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

    3) 扩展类(如 JFrame)以调用 super() 开始是一种很好的做法。同样,您可以将其作为简写省略,但在开始时保持迂腐是很好的。这会调用超类的构造函数(使用您需要的任何参数),这基本上是在说“让我们创建基本的 JFrame,然后对其应用我的扩展”。

    4) 将您的类大写并以小写动词开头所有函数也是一个好习惯。即 ButtonPanel 或 build()

    5) 似乎您才刚刚开始使用 GUI,但我可能会推荐外部库“MigLayout”(google it)。它的额外功能和易用性令人惊叹,它使您能够克服所有 Java 内置的、可怕的布局类型。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2014-06-04
        • 1970-01-01
        • 2014-10-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-28
        • 2013-09-12
        相关资源
        最近更新 更多