Ce code, produit une fenetre dans lequel je pourrai du texte.
J'ai un petit problème, car mes menus ne veule pas s'installer en haut.
Comment faire?
Merci!
Code :
package core;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/* FrameDemo.java requires no other files. */
public class FrameDemo2 extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
public FrameDemo2(){
JEditorPane editorPane = createEditorPane();
JScrollPane editorScrollPane = new JScrollPane(editorPane);
editorScrollPane.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
editorScrollPane.setPreferredSize(new Dimension(250, 145));
editorScrollPane.setMinimumSize(new Dimension(10, 10));
// frame.getContentPane().add(menuBar(),BorderLayout.NORTH);
JPanel menu = new JPanel();
menu.add(createMenuBar(),BorderLayout.NORTH);
add(menu);
// add(createMenuBar(),BorderLayout.NORTH);
add(editorScrollPane,BorderLayout.WEST);
}
public JEditorPane createEditorPane(){
JEditorPane editorPane = new JEditorPane();
editorPane.setEditable(false);
editorPane.setText("TEST");
return editorPane;
}
public JMenuBar createMenuBar(){
JMenuBar menuBar;
JMenu menu;
JMenuItem menuItem,menuItem2, menuItem3;
//Create the menu bar.
menuBar = new JMenuBar();
//Build the first menu.
menu = new JMenu("File");
menu.setMnemonic(KeyEvent.VK_A);
menu.getAccessibleContext().setAccessibleDescription(
"The only menu in this program that has menu items");
menuBar.add(menu);
// Les JMenuItems
menuItem = new JMenuItem("Open a XML",
KeyEvent.VK_T);
menuItem2 = new JMenuItem("Quitter",
KeyEvent.VK_T);
menuItem3 = new JMenuItem("Faire la table des matieres",
KeyEvent.VK_T);
menuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_1, ActionEvent.ALT_MASK));
menuItem.getAccessibleContext().setAccessibleDescription(
"This doesn't really do anything");
menu.add(menuItem);
menu.add(menuItem2);
// Build second menu in the menu bar.
menu = new JMenu("XML Action");
menu.getAccessibleContext().setAccessibleDescription(
"This menu does nothing");
menuBar.add(menu);
menu.add(menuItem3);
return menuBar;
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("FrameDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel demo = new FrameDemo2();
//frame.setJMenuBar(demo);
// frame.setJMenuBar(demo.createMenuBar());
frame.getContentPane().add(demo,BorderLayout.PAGE_START);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}