Dodanie elementu do JFrame/Jpanel

0

Hejka, proszę o porady, otóż chce stworzyć obiekt w poniższym kodzie, ale jednocześnie chcę zachować podział borderlayout/gridlayout....

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

class MyPanel extends JPanel implements Runnable {
	/**
	 * 
	 */
	Graphics gr;
	Color c;
	private static final long serialVersionUID = 1L;
	
	void newColor() {
		c = new Color((float)Math.random(), (float)Math.random(), (float)Math.random());
	}
	 public void paint(Graphics g) {
		super.paint(g);
		System.out.println("paint()");
		g.setColor(c);
		g.fillOval(100, 100, 30, 30);
	}
	

	public void setBounds(int x, int y, int width, int height) {
		// TODO Automatycznie generowany szkielet metody
		//System.out.println("setBounds");
		super.setBounds(x, y, width, height);
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		
	}
}
public class LayApp extends JFrame{
	/**
	 * 
	 */
	

	private static final long serialVersionUID = 1L;
	
	public static void main(String args[]) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				new LayApp();
			}
		});
	}
	JButton newC, remove;
	MyPanel canvas;
	LayApp() {
		Container pane = getContentPane();
		canvas = new MyPanel();
		canvas.setPreferredSize(new Dimension(400,400));
		pane.add(canvas);
		
		JFrame ramka = new JFrame();
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(2,2));
		panel.setBackground(Color.GRAY);
		JLabel x1 = new JLabel("x1");
		panel.add(x1);
		JTextField a = new JTextField(5);
		panel.add(a);
		JLabel x2 = new JLabel("x2");
		panel.add(x2);
		JTextField b = new JTextField(5);
		panel.add(b);
		ramka.getContentPane().add(BorderLayout.EAST, panel);
		
		setDefaultCloseOperation(EXIT_ON_CLOSE);

		pack();
		setVisible(true);
	

        JMenuBar menuBar = new JMenuBar();
        

        setJMenuBar(menuBar);
        

        JMenu fileMenu = new JMenu("File");
        menuBar.add(fileMenu);
        
        JMenuItem Open = new JMenuItem("Open");
        fileMenu.add(Open);
        JMenuItem Exit = new JMenuItem("Exit");
        fileMenu.add(Exit);
    
        
        JMenu editMenu = new JMenu("Help");
        menuBar.add(editMenu);
        JMenuItem About = new JMenuItem("About");
        editMenu.add(About);
      
        
	}


}

 
1

Po tak precyzyjnym opisie problemu, mogę udzielić tylko takiej rady: dopisz gdzieś w kodzie

new Object();
0

Konkretnie chodzi mi o to, że po dodaniu tego, w podanym miejscu powininno wyświetlać się kółeczko w podanym miejscu
Wcześniej w troszeczke innym kodzie gdzie uzywałem containera kółeczko się pokzywało, ale nie było tam gridlayout`a

 void newColor() {
        c = new Color((float)Math.random(), (float)Math.random(), (float)Math.random());
    }
     public void paint(Graphics g) {
        super.paint(g);
        System.out.println("paint()");
        g.setColor(c);
        g.fillOval(100, 100, 30, 30);
    }
0

Niepotrzebnie tworzysz obiekt typu JFrame o nazwie ramka.

    LayApp() {
        //Container pane = getContentPane();
        canvas = new MyPanel();
        canvas.setPreferredSize(new Dimension(400,400));
        add(canvas);
 
        //JFrame ramka = new JFrame();
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(2,2));
        panel.setBackground(Color.GRAY);
        JLabel x1 = new JLabel("x1");
        panel.add(x1);
        JTextField a = new JTextField(5);
        panel.add(a);
        JLabel x2 = new JLabel("x2");
        panel.add(x2);
        JTextField b = new JTextField(5);
        panel.add(b);
        add(BorderLayout.EAST, panel);
        ...

1 użytkowników online, w tym zalogowanych: 0, gości: 1