Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to draw shape by right clicking a panel in Swing?
    primarykey
    data
    text
    <p>I am trying to create a swing program. In my program I want to achieve something like this: right click on a panel and select the menu "Draw rectangle" and program should draw a very simple rectangle on the panel. Here is my code:</p> <pre><code>import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.BevelBorder; public class MainWindow extends JFrame { JFrame frame = null; AboutDialog aboutDialog = null; JLabel statusLabel = null; //label on statusPanel public MainWindow() { frame = new JFrame("Project"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //MENUS JMenuBar menuBar = new JMenuBar(); //menubar JMenu menuDosya = new JMenu("Dosya"); //menus on menubar JMenu menuYardim = new JMenu("Yardım"); //menus in menus menuBar.add(menuDosya); menuBar.add(menuYardim); JMenuItem menuItemCikis = new JMenuItem("Çıkış", KeyEvent.VK_Q); //dosya menus menuItemCikis.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { System.exit(0); } }); menuDosya.add(menuItemCikis); JMenuItem menuItemYardim = new JMenuItem("Hakkında", KeyEvent.VK_H); //hakkinda menus menuItemYardim.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { JDialog f = new AboutDialog(new JFrame()); f.show(); } }); menuYardim.add(menuItemYardim); frame.setJMenuBar(menuBar); //TOOLBAR JToolBar toolbar = new JToolBar(); JButton exitButton = new JButton("Kapat"); toolbar.add(exitButton); //STATUSBAR JPanel statusPanel = new JPanel(); statusPanel.setBorder(new BevelBorder(BevelBorder.LOWERED)); frame.add(statusPanel, BorderLayout.SOUTH); statusPanel.setPreferredSize(new Dimension(frame.getWidth(), 20)); statusPanel.setLayout(new BoxLayout(statusPanel, BoxLayout.X_AXIS)); statusLabel = new JLabel("Ready."); statusLabel.setHorizontalAlignment(SwingConstants.LEFT); statusPanel.add(statusLabel); //MAIN CONTENT OF THE PROGRAM final JPanel mainContentPanel = new JPanel(); //RIGHT CLICK MENU final JPopupMenu menuSag = new JPopupMenu("RightClickMenu"); JMenuItem menuRightClickRectangle = new JMenuItem("draw rectangle"); menuRightClickRectangle.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { //CircleShape cs=new CircleShape(); mainContentPanel.add(new CircleShape()); //trying to draw. mainContentPanel.repaint(); //mainContentPanel.repaint(); boyle olacak. } }); JMenuItem menuRightClickCircle = new JMenuItem("Daire çiz"); menuSag.add(menuRightClickRectangle); menuSag.add(menuRightClickCircle); mainContentPanel.addMouseListener(new MouseAdapter() { public void mouseReleased(MouseEvent e) { if (e.getButton() == MouseEvent.BUTTON3) { menuSag.show(e.getComponent(), e.getX(), e.getY()); statusLabel.setText("X=" + e.getX() + " " + "Y=" + e.getY()); } } }); JButton west = new JButton("West"); JButton center = new JButton("Center"); JPanel content = new JPanel(); //framein icindeki genel panel. en genel panel bu. content.setLayout(new BorderLayout()); content.add(toolbar, BorderLayout.NORTH); content.add(statusPanel, BorderLayout.SOUTH); content.add(west, BorderLayout.WEST); content.add(mainContentPanel, BorderLayout.CENTER); frame.setContentPane(content); frame.setPreferredSize(new Dimension(400, 300)); frame.setLocationRelativeTo(null); frame.pack(); frame.setVisible(true); } } </code></pre> <p>The problem is nothing is drawn on the panel. I guess there is an event loss in the program but i don't know how to solve this issue.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload