160 likes | 275 Views
This tutorial focuses on creating a graphical user interface (GUI) in Java, specifically utilizing components like JFrame, JPanel, JLabel, JMenu, JTextField, and JButton. It demonstrates how to set up a basic conference publication insertion form. You'll learn how to implement layouts using GridLayout and FlowLayout, manage menu actions with JMenu and AbstractAction, and respond to user input through buttons and text fields. This step-by-step guide also includes example code snippets to help you understand the implementation clearly.
E N D
Tutorial for Project (Part 2)(GUI in Java) TA : Seunghan Chang (aq9320@wayne.edu)
4. JLabel 1. JFame 2. JPanel 3. JMenu 5. JTextField 6. JButton Example GUI in Java(2) Please click me to see how this GUI works
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"); }
Example GUI in Java 4. JLabel 1. JFame 2. JPanel 3. JMenu 5. JTextField 6. JButton
JPanel : Conference JPanel conferencePanel = new JPanel(); conferencePanel.setLayout(new GridLayout(2,4)); conferencePanel.setBorder(BorderFactory.createLineBorder(Color.blue, 2)); conferencePanel.setBorder(BorderFactory.createTitledBorder("Conference")); JPanel publicationPanel_2 = new JPanel(); publicationPanel_2.setLayout(new FlowLayout(FlowLayout.LEFT)); publicationPanel.add(publicationPanel_2); 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); Layout Manager
Example GUI in Java 4. JLabel 1. JFame 2. JPanel 3. JMenu 5. JTextField 6. JButton
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);
Example GUI in Java 4. JLabel 1. JFame 2. JPanel 3. JMenu 5. JTextField 6. JButton
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);
Example GUI in Java 4. JLabel 1. JFame 2. JPanel 3. JMenu 5. JTextField 6. JButton
JTextField private static JTextField sampleJTextField_12 = null; sampleJTextField_12 = new JTextField(70); publicationPanel_2.add(sampleJTextField_12);
Example GUI in Java 4. JLabel 1. JFame 2. JPanel 3. JMenu 5. JTextField 6. JButton
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);}});
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