Скачиваний:
63
Добавлен:
15.06.2014
Размер:
9.45 Кб
Скачать
package by;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;


public class MagazineHelper extends JFrame {

private final static int WIDTH = 410;
private final static int HEIGHT = 220;

public MagazineHelper(boolean orientation, boolean useOrientation) {
super("Magazine Helper");
JPanel content = new JPanel(new BorderLayout(5, 5));
content.add(createLabel("Top"), (useOrientation) ? BorderLayout.PAGE_START : BorderLayout.NORTH);
//content.add(createLabel("Bottom"), (useOrientation) ? BorderLayout.PAGE_END : BorderLayout.SOUTH);
content.add(createLabel("Left"), (useOrientation) ? BorderLayout.LINE_START : BorderLayout.WEST);
//content.add(createLabel("Right"), (useOrientation) ? BorderLayout.LINE_END : BorderLayout.EAST);
content.add(createMainTable(), BorderLayout.CENTER);
if (orientation) {
content.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
}
setContentPane(content);
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}

private JLabel createLabel(String caption) {
JLabel lbl = new JLabel(caption);
lbl.setPreferredSize(new Dimension(100, 50));
lbl.setHorizontalAlignment(JLabel.CENTER);
lbl.setBorder(BorderFactory.createLineBorder(new Color(0xff8000), 3));
return lbl;
}

//method that add table into panel and frame
private JTable createMainTable()
{
String[] columnNames = {"First Name",
"Last Name",
"Sport",
"# of Years",
"Vegetarian"};
Object[][] data = {
{"Kathy", "Smith",
"Snowboarding", new Integer(5), new Boolean(false)},
{"John", "Doe",
"Rowing", new Integer(3), new Boolean(true)},
{"Sue", "Black",
"Knitting", new Integer(2), new Boolean(false)},
{"Jane", "White",
"Speed reading", new Integer(20), new Boolean(true)},
{"Joe", "Brown",
"Pool", new Integer(10), new Boolean(false)},
{"Kathy", "Smith",
"Snowboarding", new Integer(5), new Boolean(false)},
{"John", "Doe",
"Rowing", new Integer(3), new Boolean(true)},
{"Sue", "Black",
"Knitting", new Integer(2), new Boolean(false)},
{"Jane", "White",
"Speed reading", new Integer(20), new Boolean(true)},
{"Joe", "Brown",
"Pool", new Integer(10), new Boolean(false)},
{"Kathy", "Smith",
"Snowboarding", new Integer(5), new Boolean(false)},
{"John", "Doe",
"Rowing", new Integer(3), new Boolean(true)},
{"Sue", "Black",
"Knitting", new Integer(2), new Boolean(false)},
{"Jane", "White",
"Speed reading", new Integer(20), new Boolean(true)},
{"Joe", "Brown",
"Pool", new Integer(10), new Boolean(false)},
{"Kathy", "Smith",
"Snowboarding", new Integer(5), new Boolean(false)},
{"John", "Doe",
"Rowing", new Integer(3), new Boolean(true)},
{"Sue", "Black",
"Knitting", new Integer(2), new Boolean(false)},
{"Jane", "White",
"Speed reading", new Integer(20), new Boolean(true)},
{"Joe", "Brown",
"Pool", new Integer(10), new Boolean(false)},
{"Kathy", "Smith",
"Snowboarding", new Integer(5), new Boolean(false)},
{"John", "Doe",
"Rowing", new Integer(3), new Boolean(true)},
{"Sue", "Black",
"Knitting", new Integer(2), new Boolean(false)},
{"Jane", "White",
"Speed reading", new Integer(20), new Boolean(true)},
{"Joe", "Brown",
"Pool", new Integer(10), new Boolean(false)},
{"Kathy", "Smith",
"Snowboarding", new Integer(5), new Boolean(false)},
{"John", "Doe",
"Rowing", new Integer(3), new Boolean(true)},
{"Sue", "Black",
"Knitting", new Integer(2), new Boolean(false)},
{"Jane", "White",
"Speed reading", new Integer(20), new Boolean(true)},
{"Joe", "Brown",
"Pool", new Integer(10), new Boolean(false)},
{"Kathy", "Smith",
"Snowboarding", new Integer(5), new Boolean(false)},
{"John", "Doe",
"Rowing", new Integer(3), new Boolean(true)},
{"Sue", "Black",
"Knitting", new Integer(2), new Boolean(false)},
{"Jane", "White",
"Speed reading", new Integer(20), new Boolean(true)},
{"Joe", "Brown",
"Pool", new Integer(10), new Boolean(false)},
{"Kathy", "Smith",
"Snowboarding", new Integer(5), new Boolean(false)},
{"John", "Doe",
"Rowing", new Integer(3), new Boolean(true)},
{"Sue", "Black",
"Knitting", new Integer(2), new Boolean(false)},
{"Jane", "White",
"Speed reading", new Integer(20), new Boolean(true)},
{"Joe", "Brown",
"Pool", new Integer(10), new Boolean(false)},
{"Kathy", "Smith",
"Snowboarding", new Integer(5), new Boolean(false)},
{"John", "Doe",
"Rowing", new Integer(3), new Boolean(true)},
{"Sue", "Black",
"Knitting", new Integer(2), new Boolean(false)},
{"Jane", "White",
"Speed reading", new Integer(20), new Boolean(true)},
{"Joe", "Brown",
"Pool", new Integer(10), new Boolean(false)},
{"Kathy", "Smith",
"Snowboarding", new Integer(5), new Boolean(false)},
{"John", "Doe",
"Rowing", new Integer(3), new Boolean(true)},
{"Sue", "Black",
"Knitting", new Integer(2), new Boolean(false)},
{"Jane", "White",
"Speed reading", new Integer(20), new Boolean(true)},
{"Joe", "Brown",
"Pool", new Integer(10), new Boolean(false)}
};
JTable maintable = new JTable(data,columnNames);
//JTable maintable = new JTable(new TableModel());
maintable.setPreferredSize(new Dimension(100,50));


JScrollPane scrollPane = new JScrollPane(maintable);
maintable.setFillsViewportHeight(true);


maintable.setPreferredSize(new Dimension(100, 50));
//maintable.setHorizontalAlignment(JLabel.CENTER);
maintable.setBorder(BorderFactory.createLineBorder(new Color(0xff8000), 3));

return maintable;
}

public static void main(String[] args)
{
//set default size of screen application
MagazineHelper myapplication = new MagazineHelper(false, false);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
myapplication.setSize((int)screenSize.getWidth(),(int)(screenSize.getHeight() - 40));

//set a minimum size for application
Dimension minimumSize = new Dimension(800,600);
myapplication.setMinimumSize(minimumSize);

//add JMenuBar to the frame
JMenuBar menuBar = new JMenuBar();
myapplication.setJMenuBar(menuBar);

//define and add two drop down menu
JMenu fileMenu = new JMenu("File");
JMenu editMenu = new JMenu("Edit");
JMenu viewMenu = new JMenu("View");
JMenu toolsMenu = new JMenu("Tools");
JMenu windowMenu = new JMenu("Window");
JMenu helpMenu = new JMenu("Help");
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(viewMenu);
menuBar.add(toolsMenu);
menuBar.add(windowMenu);
menuBar.add(helpMenu);

//create and add simple menu item to one of the drop down menu
JMenuItem newItem = new JMenuItem("New");
JMenuItem openItem = new JMenuItem("Open");
JMenuItem recentItem = new JMenuItem("Recent");
JMenuItem exitItem = new JMenuItem("Exit");
//
JMenuItem cutItem = new JMenuItem("Cut");
JMenuItem copyItem = new JMenuItem("Copy");
JMenuItem pasteItem = new JMenuItem("Paste");
//
JMenuItem informationItem = new JMenuItem("Information");
JMenuItem aboutItem = new JMenuItem("About");

//add items into file categorie
fileMenu.add(newItem);
fileMenu.add(openItem);
fileMenu.add(recentItem);
fileMenu.add(exitItem);
//add items into edit categorie
editMenu.add(cutItem);
editMenu.add(copyItem);
editMenu.add(pasteItem);
//add items into help categorie
helpMenu.add(informationItem);
helpMenu.add(aboutItem);

myapplication.setVisible(true);
}
}
Соседние файлы в папке by