2011-02-18 380 views
2

我正在嘗試製作一個銷售糕點,百吉餅和咖啡的當地百吉餅店的計劃。我無法實現我所做成的JFrameJFrame和JPanel問題

任何瞭解如何使我的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); 
} 
} 

回答

4

一些好的做法,以進入方法Swing GUI:

1)您的面板類構建錯誤。 你需要重新寫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類(總是大寫類),這posseses一個JPanel與它的按鈕,但它永遠不會顯示它擁有的這個JPanel。你需要ButtonPanel是一個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中時,它是很好的做法),開始與超()的調用。再說一遍,你可以把它簡寫成簡寫,但是開始的時候它很好學。這會調用超類的構造函數(無論您需要什麼參數),它基本上都是這樣說的:「讓我們創建基本的JFrame,然後再應用我的擴展」。

4)它也是一個很好的做法,以大寫你的類,並開始所有功能與小寫動詞。即ButtonPanel或構建()

5)看起來你只是剛剛開始與GUI,但我可以推薦外部庫「MigLayout」(谷歌它)。它的超強力和易用性令人驚歎,它讓您可以反抗所有Java內置,可怕的佈局類型。

0

他們跟海誓山盟與ActionListener接口,ActionPerformed(ActionEvent e)方法,並以解僱那些你寫作時需要<the component to be listened to>.addActionListener(theListener);