2015-12-22 71 views
2

菜單你好eveybody我創造了這個代碼:打開從按鈕

package project1; 

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.FlowLayout; 
import java.awt.Font; 
import java.awt.Graphics; 
import java.awt.GridBagLayout; 
import java.awt.Image; 
import java.awt.event.ActionListener; 
import java.awt.image.BufferedImage; 
import java.awt.image.ImageProducer; 
import java.io.File; 
import java.io.IOException; 

import javax.imageio.ImageIO; 
import javax.swing.BoxLayout; 
import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JComponent; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JSeparator; 

public class ccc { 
public static void main(String[] args){ 
JFrame frame = new JFrame("Food delivery"); // Create a frame 
frame.setSize(1600, 1400); // Set the frame size 
frame.setLocationRelativeTo(null); // New since JDK 1.4 
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
frame.setVisible(true); // Display the frame 

JPanel panel = new JPanel(new FlowLayout()); 
JPanel panel1 = new JPanel(new FlowLayout(FlowLayout.LEFT)); 
ImageIcon icon = new ImageIcon("hamburger.jpg"); 
frame.setIconImage(icon.getImage()); 
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
frame.setLayout(new GridBagLayout()); 

try { 
frame.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new  File("fd12.jpg"))))); 
} catch (IOException e) { 
     e.printStackTrace(); 
} 
frame.setLayout(new FlowLayout(FlowLayout.LEFT)); 

JLabel label = new JLabel("Welcome to Food Delivery"); 

label.setFont(new Font("Courier New", Font.BOLD, 38)); 
label.setForeground(Color.black); 

System.out.println(""); 

JButton btn1= new JButton("Breakfast"); 
btn1.setFont(new Font("Courier New", Font.BOLD, 16)); 
btn1.setForeground(Color.BLACK); 

JButton btn2= new JButton("Starters"); 
btn2.setFont(new Font("Courier New", Font.BOLD, 16)); 
btn2.setForeground(Color.BLACK); 

JButton btn3= new JButton("Main Dishes"); 
btn3.setFont(new Font("Courier New", Font.BOLD, 16)); 
btn3.setForeground(Color.BLACK); 

JButton btn4= new JButton("Deserts"); 
btn4.setFont(new Font("Courier New", Font.BOLD, 16)); 
btn4.setForeground(Color.BLACK); 

JButton btn5= new JButton("Drinks"); 
btn5.setFont(new Font("Courier New", Font.BOLD, 16)); 
btn5.setForeground(Color.BLACK); 

frame.add(label); 
frame.add(new JSeparator()); 
frame.add(btn1); 
frame.add(btn2); 
frame.add(btn3); 
frame.add(btn4); 
frame.add(btn5); 
frame.pack(); 
frame.setVisible(true); 

}} 

屏幕截圖: enter image description here

enter image description here

我很想得到得到打開的頁面中的新頁面對於每個按鈕和裏面我會寫的食物菜單選擇和附近它有價格寫?請幫助我,我不能這樣做的一切,就可以知道如何...

感謝

+1

像[Card Layout](https://docs.oracle.com/javase/tutorial/uiswing/layout/card.html)這樣的風格是需要檢查的東西嗎? – Emz

回答

0

可以使用的JPanel繪製其他容器裏面的東西。 (JPanel和JFrame是容器)

但你說你只是想寫文本。所以你可以使用JTextArea。 Javadoc將幫助你。

+0

你好我的朋友我是一個初學者我現在不真的如何寫它..我想當我點擊菜單之一來打開一個新的頁面內與下一個和上一頁,並在該頁寫我想要的所有價格和選擇框 –

+0

擁有next和previous會很難。但是你可以使用JTabbedPane來打開新頁面。 Javadoc現在。祝你好運:) – MeGoodGuy

0

正如Emz在評論中指出的那樣,CardLayout是此場景中最好的拳頭。爲每個菜單創建一個JPanel,並使用CardLayout將它們添加到JPanel。然後使用按鈕在面板之間切換。

下面是一個例子:

public class Example extends JPanel { 

    Example() { 

     JPanel starters = new JPanel(new FlowLayout()); 
     starters.add(new JLabel("starters...")); 
     JPanel drinks = new JPanel(new FlowLayout()); 
     drinks.add(new JLabel("drinks...")); 

     JPanel main = new JPanel(new CardLayout()); 
     main.add(starters, "starters"); 
     main.add(drinks, "drinks"); 

     JPanel buttons = new JPanel(); 
     JButton startersButton = new JButton("Starters"); 
     startersButton.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 

       CardLayout cl = (CardLayout)(main.getLayout()); 
       cl.show(main, "starters"); 
      } 
     }); 

     JButton drinksButton = new JButton("Starters"); 
     drinksButton.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 

       CardLayout cl = (CardLayout)(main.getLayout()); 
       cl.show(main, "drinks"); 
      } 
     }); 

     buttons.add(startersButton); 
     buttons.add(drinksButton); 

     setLayout(new BorderLayout()); 
     add(main); 
     add(buttons, BorderLayout.PAGE_START); 
    } 
} 

添加到一個幀並運行。

+0

我試圖添加代碼到一個框架,但我有一個錯誤: 這裏是鏈接中的代碼:http://textuploader.com/5f4zf –

+0

@HamzaMeddah而錯誤是?在哪一行? – user1803551