Tutorial for Project (Part 2) (GUI in Java)
-
Upload
jillian-erickson -
Category
Documents
-
view
19 -
download
2
description
Transcript of Tutorial for Project (Part 2) (GUI in Java)
23年 4月 19日 2/16
Example GUI in Java(1)
23年 4月 19日 3/16
Example GUI in Java(2) 1. JFame
2. JPanel
5. JTextField
4. JLabel
6. JButton
3. JMenu
Please click me to see how this GUI works
23年 4月 19日 4/16
Programming Environments
23年 4月 19日 5/16
JFrame
sampleFrame = new MainFrame();sampleFrame.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } } ); . . .sampleFrame.show();
class MainFrame extends JFrame { public MainFrame() { Toolkit kit = Toolkit.getDefaultToolkit(); Dimension screenSize = kit.getScreenSize(); int h = screenSize.height; int w = screenSize.width;
setSize(w-100,h-100); setLocation(10, 10);
setTitle("Sample Frame"); }
23年 4月 19日 6/16
Example GUI in Java 1. JFame
2. JPanel
5. JTextField
4. JLabel
6. JButton
3. JMenu
23年 4月 19日 7/16
JPanel : Conference
JPanel conferencePanel = new JPanel();
conferencePanel.setLayout(new GridLayout(2,4));
conferencePanel.setBorder(BorderFactory.createLineBorder(Color.blue, 2));
conferencePanel.setBorder(BorderFactory.createTitledBorder("Conference"));
Container contentPane = sampleFrame.getContentPane(); contentPane.setLayout(new GridLayout(6,1));
contentPane.add(titlePanel);
contentPane.add(conferencePanel);
contentPane.add(proceedingsPanel);
contentPane.add(publicationPanel);
contentPane.add(emptyPanel);
contentPane.add(buttonPanel);
JPanel publicationPanel_2 = new JPanel();
publicationPanel_2.setLayout(new FlowLayout(FlowLayout.LEFT));
publicationPanel.add(publicationPanel_2);
Layout Manager
23年 4月 19日 8/16
Example GUI in Java 1. JFame
2. JPanel
5. JTextField
4. JLabel
6. JButton
3. JMenu
23年 4月 19日 9/16
JMenu
JMenuBar menuBar = new JMenuBar();
sampleFrame.setJMenuBar(menuBar);
JMenu samplemenu1 = new JMenu("Menu1");
samplemenu1.setMnemonic('1');
. . .
menuBar.add(samplemenu1);
. . .
Action menu1Action = new AbstractAction("Menu1")
{ public void actionPerformed(ActionEvent e)
{ JOptionPane.showMessageDialog(sampleFrame, "You selected Menu1", "Menu1",
JOptionPane.INFORMATION_MESSAGE); } };
menu1Action.putValue(Action.MNEMONIC_KEY, new Integer('1'));
samplemenu1.add(menu1Action);
23年 4月 19日 10/16
Example GUI in Java 1. JFame
2. JPanel
5. JTextField
4. JLabel
6. JButton
3. JMenu
23年 4月 19日 11/16
JLabel
JLabel titleLabel = new JLabel();
titleLabel.setHorizontalTextPosition(JLabel. CENTER);
titleLabel.setText("Conference Publication Insertion");
Font f = new Font("Arial", Font.BOLD, 30);
titleLabel.setForeground(Color.blue);
titleLabel.setFont(f);
titlePanel.add(titleLabel);
23年 4月 19日 12/16
Example GUI in Java 1. JFame
2. JPanel
5. JTextField
4. JLabel
6. JButton
3. JMenu
23年 4月 19日 13/16
JTextField
private static JTextField sampleJTextField_12 = null;
sampleJTextField_12 = new JTextField(70);
publicationPanel_2.add(sampleJTextField_12);
23年 4月 19日 14/16
Example GUI in Java 1. JFame
2. JPanel
5. JTextField
4. JLabel
6. JButton
3. JMenu
23年 4月 19日 15/16
JButton
private static JButton sampleJButton1 = null;
JButton sampleJButton1 = new JButton("Insert");
buttonPanel.add(sampleJButton1);
sampleJButton1.addActionListener( new ActionListener()
{ public void actionPerformed(ActionEvent e)
{ JOptionPane.showMessageDialog(sampleFrame, "You selected Insert Button", "Insert",
JOptionPane.INFORMATION_MESSAGE); } } );
23年 4月 19日 16/16
For More Useful Information
http://java.sun.com/docs/books/tutorial/ui/features/components.html
http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JComponent.html
Thanks and Good Luck