當前位置:編程學習大全網 - 編程語言 - java編寫壹個圖形界面程序

java編寫壹個圖形界面程序

import?java.awt.*;

import?java.awt.event.*;

import?java.util.*;

import?javax.swing.*;

import?javax.swing.border.Border;

class?MainFrame?extends?JFrame?{

private?static?final?long?serialVersionUID?=?1L;

private?Map<String,?Integer>?sizes?=?new?HashMap<String,?Integer>();

private?Map<String,?Integer>?styles?=?new?HashMap<String,?Integer>();

private?Map<String,?Integer>?toppings?=?new?HashMap<String,?Integer>();

public?MainFrame()?{

sizes.put("Extra?Large",?10);

sizes.put("Large",?8);

sizes.put("Medium",?5);

sizes.put("Small",?3);

styles.put("Deep?Dish",?20);

styles.put("Regular",?10);

styles.put("Thin?Crust",?5);

styles.put("Chicago",?3);

toppings.put("Cheese",?8);

toppings.put("Tomato",?7);

toppings.put("Peppers",?6);

toppings.put("Peperoni",?5);

this.setTitle("布局及事件處理");

this.setSize(450,?350);

this.setLayout(new?BorderLayout());

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JLabel?lblTitle?=?new?JLabel();

lblTitle.setText("Pizzeria?Juno");

lblTitle.setFont(new?Font("宋體",?Font.BOLD,?36));

lblTitle.setHorizontalAlignment(SwingConstants.CENTER);

this.add("North",?lblTitle);

JPanel?bodyPanel?=?new?JPanel();

bodyPanel.setLayout(new?GridLayout(2,?1));

this.add("Center",?bodyPanel);

JPanel?listPanel?=?new?JPanel();

listPanel.setLayout(new?GridLayout(1,?3));

listPanel.setSize(200,?200);

bodyPanel.add(listPanel);

Border?lineBorder?=?BorderFactory.createLineBorder(Color.BLACK);

JPanel?sizePanel?=?new?JPanel();

sizePanel.setLayout(new?BorderLayout());

listPanel.add(sizePanel);

JLabel?sizeTitle?=?new?JLabel();

sizeTitle.setText("Sizes");

sizePanel.add("North",?sizeTitle);

JList?sizeList?=?new?JList(sizes.keySet().toArray());

sizeList.setSize(100,?100);

sizeList.setBorder(lineBorder);

sizeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

sizePanel.add(sizeList);

JPanel?stylePanel?=?new?JPanel();

stylePanel.setLayout(new?BorderLayout());

listPanel.add(stylePanel);

JLabel?styleTitle?=?new?JLabel();

styleTitle.setText("Styles");

stylePanel.add("North",?styleTitle);

JList?styleList?=?new?JList(styles.keySet().toArray());

styleList.setSize(100,?100);

styleList.setBorder(lineBorder);

styleList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

stylePanel.add(styleList);

JPanel?toppingPanel?=?new?JPanel();

toppingPanel.setLayout(new?BorderLayout());

listPanel.add(toppingPanel);

JLabel?toppingTitle?=?new?JLabel();

toppingTitle.setText("Toppings");

toppingPanel.add("North",?toppingTitle);

JList?toppingList?=?new?JList(toppings.keySet().toArray());

toppingList.setSize(100,?100);

toppingList.setBorder(lineBorder);

toppingPanel.add(toppingList);

JTextArea?txtResult?=?new?JTextArea();

txtResult.setEditable(false);

bodyPanel.add(txtResult);

JPanel?bottomPanel?=?new?JPanel();

bottomPanel.setLayout(new?GridLayout(1,?3));

this.add("South",?bottomPanel);

JLabel?label1?=?new?JLabel("Click?to?complete?order");

bottomPanel.add(label1);

JButton?btnRingUp?=?new?JButton("Ring?up");

btnRingUp.addActionListener(new?ActionListener()?{

@Override

public?void?actionPerformed(ActionEvent?e)?{

if(sizeList.getSelectedValue()?==?null)?{

JOptionPane.showMessageDialog(MainFrame.this,?"Please?select?size.");

return;

}

if(styleList.getSelectedValue()?==?null)?{

JOptionPane.showMessageDialog(MainFrame.this,?"Please?select?style.");

return;

}

if(toppingList.getSelectedValue()?==?null)?{

JOptionPane.showMessageDialog(MainFrame.this,?"Please?select?topping.");

return;

}

float?total?=?0;

String?size?=?sizeList.getSelectedValue().toString();

total?+=?sizes.get(size);

String?style?=?styleList.getSelectedValue().toString();

total?+=?styles.get(style);

String?result?=?size?+?"?Pizza,?"?+?style?+?"?Style";

Object[]?toppings?=?toppingList.getSelectedValues();

for(Object?topping?:?toppings)?{

result?+=?"\n?+"?+?topping.toString();

total?+=?MainFrame.this.toppings.get(topping.toString());

}

result?+=?"\n?Total:?"?+?total;

txtResult.setText(result);

}

});

bottomPanel.add(btnRingUp);

JButton?btnQuit?=?new?JButton("Quit");

btnQuit.addActionListener(new?ActionListener()?{

@Override

public?void?actionPerformed(ActionEvent?e)?{

MainFrame.this.dispose();

}

});

bottomPanel.add(btnQuit);

}

}

public?class?App?{

public?static?void?main(String[]?args)?{

MainFrame?mainFrame?=?new?MainFrame();

mainFrame.setVisible(true);

}

}

  • 上一篇:漢語有可能取代英語成為世界語言嗎?
  • 下一篇:我最敬佩的壹個人初壹作文
  • copyright 2024編程學習大全網