JMenuBar项目被困在vlcj的Canvas下

时间:2014-04-21 16:39:31

标签: java user-interface vlcj jtoolbar

你好我正在尝试修复我的JMenuBar问题,因为它显示在问题中的帖子中我的JMenuBar项目没有以正确的方式打开,这是在Canvas上方而不是在Canvas下面 java application frame

应用程序的代码

    public class GuiInterface extends JFrame {
    Canvas c;
    private final JMenu jMenu1,jMenu2,jMenu3;
    private final JMenuBar jMenuBar1;
    private final JMenuItem itemNewSrt,itemOpenVideo,itemSavefile;
    JToolBar toolBar;    

        public GuiInterface(String title){

    //reader = new ReadFile();

    setSize(1024, 720);
    setVisible(true);
    setTitle("AnimeFactor");
    setDefaultCloseOperation(GuiInterface.EXIT_ON_CLOSE);
    //video setting 
    MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory();
    c = new Canvas();
    String[] petStrings = { "Translation Line", "Both Line" };
    laList = new JComboBox(petStrings);
    c.setBackground(Color.black);
    JPanel p = new JPanel();
    p.setLayout(new BorderLayout());
    p.add(c, BorderLayout.CENTER);
    add(p, BorderLayout.CENTER);
    EmbeddedMediaPlayer mediaPlayer = mediaPlayerFactory.newEmbeddedMediaPlayer();
    mediaPlayer.setVideoSurface(mediaPlayerFactory.newVideoSurface(c));
    mediaPlayer.playMedia("C:\\Users\\isslam\\Downloads\\gg.mp4");
    table = new JTable(new DefaultTableModel(columnNames, 0));
    table.setFillsViewportHeight(true);
    table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
    TableColumn columnA = table.getColumn("#");
    columnA.setMinWidth(10);
    columnA.setMaxWidth(40);
    TableColumn columnB= table.getColumn("Start");
    columnB.setMinWidth(80);
    columnB.setMaxWidth(90);
    TableColumn columnC= table.getColumn("End");
    columnC.setMinWidth(80);
    columnC.setMaxWidth(90);


    ImageIcon openIcon = new ImageIcon(
                GuiInterface.class.getResource("/resources/image/folder-icon.png"));
        ImageIcon saveIcon = new ImageIcon(
                GuiInterface.class.getResource("/resources/image/red-disk-icon.png"));
        ImageIcon newIcon = new ImageIcon(
                GuiInterface.class.getResource("/resources/image/Actionsnew-icon.png"));




        Action openAction = new AbstractAction("Open Subtitle", openIcon) {
            @Override
            public void actionPerformed(ActionEvent e) {
                ourFileSelector.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
                ourFileSelector.showSaveDialog(null);
                ourSrtFile =  ourFileSelector.getSelectedFile();
                srtPath = ourSrtFile.getAbsolutePath();
                Worker p = new Worker(srtPath, table);
                p.execute();
            }
        };

        Action saveAction = new AbstractAction("Save", saveIcon) {
            @Override
            public void actionPerformed(ActionEvent e) {

            }
        };
        Action newAction = new AbstractAction("New", newIcon) {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("New File");
            }
        };
    jMenu1 = new JMenu("File");
    jMenu2 = new JMenu("Video");
    jMenu3 = new JMenu("Subtitle");
    itemNewSrt = new JMenuItem(newAction);
    jMenu1.add(itemNewSrt);
    itemSavefile = new JMenuItem(saveAction);
    jMenu1.add(itemSavefile);
    jMenuBar1 = new JMenuBar();
    jMenuBar1.setBorder(etch);
    setJMenuBar(jMenuBar1);
    jMenuBar1.add(jMenu1);
    jMenuBar1.add(jMenu2);
    jMenuBar1.add(jMenu3);
    ourFileSelector = new JFileChooser();

    Obutton = new JButton[numberOfButton];
    etch = BorderFactory.createEtchedBorder();
    //starting and ending time
    enterText = new JTextField();
    enterText.setPreferredSize(new Dimension(0,100));
    startTime = new JTextField();
    startTime.setPreferredSize(new Dimension(120, 20));
    startTimeingLable = new JLabel("Starting Time");
    endTimeingLabel = new JLabel("Ending Time");
    endTime = new JTextField();
    endTime.setPreferredSize(new Dimension(120, 20));
    //end of start and ending time filed

        toolBar = new JToolBar();
        toolBar.setBorder(new LineBorder(Color.LIGHT_GRAY, 1));
        toolBar.add(newAction);
        toolBar.add(saveAction);


        JPanel toolBarPane = new JPanel(new BorderLayout());
        toolBarPane.add(toolBar,BorderLayout.NORTH);

        JPanel timing = new JPanel(new FlowLayout(FlowLayout.TRAILING));
        timing.add(startTimeingLable);
        timing.add(startTime);
        timing.add(endTimeingLabel);
        timing.add(endTime);
        timing.add(laList);

        empty = BorderFactory.createEmptyBorder(30, 5, 5, 5);
        Boveld = BorderFactory.createBevelBorder(BevelBorder.RAISED);
        Campound = BorderFactory.createCompoundBorder(empty,Boveld);


        JPanel textFiled = new JPanel(new BorderLayout());
        textFiled.add(timing);
        textFiled.add(enterText,BorderLayout.SOUTH);
        textFiled.setBorder(Campound);
        JScrollPane scrollPane = new JScrollPane(table);
        add(scrollPane, BorderLayout.SOUTH);
        add(textFiled, BorderLayout.EAST);
        add(toolBarPane,BorderLayout.PAGE_START);





       toolBar.add(openAction);
       itemOpenVideo = new JMenuItem(openAction);
       jMenu1.add(itemOpenVideo);
       //itemOpenVideo.addActionListener(new MenuBarMethod());

    }


     class Worker extends SwingWorker<DefaultTableModel, Void> {
       private final String srtPath;
       private final JTable table;
       DefaultTableModel model;
        public Worker(String srtPath, JTable table) {
                this.srtPath = srtPath;
                this.table = table;
             }


     @Override
      protected DefaultTableModel doInBackground() {
     model = new DefaultTableModel(columnNames, 0);
    ArrayList<String> ends = ReadFile.getFileEndingTime(srtPath);
    ArrayList<String> starts = ReadFile.getFileStartingTime(srtPath);
    ArrayList<String> subs = ReadFile.readSubtitles(srtPath);
    ArrayList<String> lins = ReadFile.ArraylineLengths(srtPath);
    for (int i = 0; i < ReadFile.maxLine(srtPath); i++) {
        model.addRow(new Object[] {lins.get(i), starts.get(i), ends.get(i), subs.get(i)});
    }
    return model;
}
    @Override
     protected void done() {
    table.setModel(model);
    table.setFillsViewportHeight(true);
    table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
    TableColumn columnA = table.getColumn("#");
    columnA.setMinWidth(10);
    columnA.setMaxWidth(40);
    TableColumn columnB= table.getColumn("Start");
    columnB.setMinWidth(80);
    columnB.setMaxWidth(90);
    TableColumn columnC= table.getColumn("End");
    columnC.setMinWidth(80);
    columnC.setMaxWidth(90);
}
}
        }

1 个答案:

答案 0 :(得分:0)

您的问题来自混合重型和轻型部件,而无需特别注意让它们一起工作。最简单,最好的解决方案:不要将重量轻,重量轻的组件混合在一起。因此,请避免使用Canvas,而是使用Swing等效项,即JPanel。


修改
如果你绝对需要混合重量轻和重量轻的组件,那么你需要解决让弹出菜单出现在重量级Canvas组件上的问题。考虑:

JPopupMenu.setDefaultLightWeightPopupEnabled(false); 
ToolTipManager.sharedInstance().setLightWeightPopupEnabled(false);