import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics.*;
import javax.swing.*;
import java.util.*;
import java.awt.Color.*;
import javax.swing.text.*;
import java.text.*;
import java.awt.Font.*;
/**
* Write a description of class Ellipse here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class drawingFrame extends JFrame implements ActionListener {
Color bdcol, outcol;
public int x, y, w, h, x1, x2, x3, y1, y2, y3;
public int numEl = 0;
boolean sel;
Container myContent = getContentPane();
/** Panel for user to see textual out */
private JPanel interfacePanel;
/** Panel for user to see textual out */
private JPanel drawingBoard;
/** Label for SHapes */
public JLabel rec = new JLabel("", JLabel.CENTER);
/** Label for SHapes */
public JLabel ell = new JLabel("", JLabel.CENTER);
/** Label for SHapes */
public JLabel tri = new JLabel("", JLabel.CENTER);
/** Panel for menu buttons */
private JPanel menuPanel;
/** Panel for menu buttons */
private JMenuBar myMenu = new JMenuBar();
/** Menu header */
private JMenu drawShapes = new JMenu("drawShape");
/** Menu Item to draw Ellipse */
private JMenuItem drawEllipse = new JMenuItem("Ellipse");
/** Menu Item to draw Rectangle */
private JMenuItem drawRect = new JMenuItem("Rectangle");
/** Menu Item to draw Triangle*/
private JMenuItem drawTriangle = new JMenuItem("Triangle");
/** Menu header */
private JMenu editShape = new JMenu("editShape");
/** Menu Item to draw Rectangle */
private JMenuItem edit = new JMenuItem("Edit");
/** Menu Item to draw Rectangle */
private JMenuItem delete = new JMenuItem("Delete");
/** Menu header */
private JMenu aboutMenu = new JMenu("About");
/** Menu Item to draw Rectangle */
private JMenuItem aboutItem = new JMenuItem("About/Help");
/** Instance of Base Class */
private Rectangle rectangle;
/** Instance of Base Class */
private Ellipse ellipse;
/** Instance of Base Class */
private GeometShape baseShape;
/** Instance of Component Class */
private drawShapes myShape;
/** Instance of Component Class */
private ShapeArray list;
/**
* Constructor for the Frame Class
*
*/
public drawingFrame() {
list = new ShapeArray();
myShape = new drawShapes( list );
setTitle("Ma Freaking Board");
setDefaultCloseOperation(EXIT_ON_CLOSE);
drawEllipse.addActionListener(this);
drawRect.addActionListener(this);
drawTriangle.addActionListener(this);
edit.addActionListener(this);
delete.addActionListener(this);
aboutItem.addActionListener(this);
interfacePanel = new JPanel( );
interfacePanel.setLayout( new BoxLayout(interfacePanel, BoxLayout.PAGE_AXIS));
interfacePanel.setBorder(BorderFactory.createLineBorder (Color.black, 2));
interfacePanel.setBackground(Color.white);
interfacePanel.setPreferredSize( new Dimension( 150, 0));
interfacePanel.add(ell);
interfacePanel.add(rec);
interfacePanel.add(tri);
drawShapes.add(drawEllipse);
drawShapes.add(drawRect);
drawShapes.add(drawTriangle);
editShape.add(edit);
editShape.add(delete);
aboutMenu.add(aboutItem);
myMenu.add(drawShapes);
myMenu.add(editShape);
myMenu.add(aboutMenu);
this.setJMenuBar(myMenu);
Container myContent = getContentPane();
myContent.add(interfacePanel, BorderLayout.WEST);
myContent.add(myShape);
pack();
this.setSize(600, 600);
setResizable(false);
setVisible(true);
ell.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
setTitle("This action is suppose to select the particular ellipse that created this label, so i can delete or edit this particular ellipse");
}
});
}//End of public drawingFrame
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equalsIgnoreCase("Ellipse"))
{
//This is where I want to create a new JLabel. Meaning each created ellipse will create a new JLabel,
//BUt if i create the label here and add it to the interfacepanel, it is not displayed
//And each label needs a mouseListener.
showDialog();
list.myList.add(new Ellipse( x, y, w, h, bdcol, outcol, sel));
String s = String.valueOf( numEl + " : Ellipse: ");
ell.setText(s);
interfacePanel.add(ell);
repaint();
numEl++;
}
if(e.getActionCommand().equalsIgnoreCase("Rectangle"))
{
showDialog();
list.myList.add(new Rectangle( x, y, w, h, bdcol, outcol, sel));
String s = String.valueOf( numEl + " : Rectangle ");
rec.setText(s);
interfacePanel.add(rec);
repaint();
numEl++;
}
} //End of Action Performed for Shapes
public static void main(String[] args)
{
drawingFrame myFrame = new drawingFrame();
}
public void showDialog() {
class customDialg extends JDialog
{
JPanel textF = new JPanel(new GridLayout(0,1));
JPanel labelF = new JPanel(new GridLayout(0,1));
JLabel XL = new JLabel("Enter Xpos");
JLabel YL = new JLabel("Enter Ypos");
JLabel WL = new JLabel("Enter Width");
JLabel HL = new JLabel("Enter Height");
JLabel BDL = new JLabel("Ouline_Color");
JLabel FLL = new JLabel("Fill_Color");
JLabel SELL = new JLabel("Fill_Color");
JTextField XT = new JTextField("100", 6);
JTextField YT = new JTextField("200", 6);
JTextField WT = new JTextField("300", 6);
JTextField HT = new JTextField("200", 4);
JTextField BDT = new JTextField("black", 6);
JTextField FLT = new JTextField("magenta", 6);
JTextField SELT = new JTextField("true", 6);
public customDialg ()
{
setModal(true);
setLocation(400,300);
Container cont = getContentPane();
labelF.add(XL);
labelF.add(YL);
labelF.add(WL);
labelF.add(HL);
labelF.add(BDL);
labelF.add(FLL);
labelF.add(SELL);
textF.add(XT);
textF.add(YT);
textF.add(WT);
textF.add(HT);
textF.add(BDT);
textF.add(FLT);
textF.add(SELT);
cont.add(labelF, BorderLayout.CENTER);
cont.add(textF, BorderLayout.LINE_END);
JButton mybutt = new JButton("Enter Specs");
mybutt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
x = Integer.parseInt(XT.getText() );
y = Integer.parseInt(YT.getText() );
w = Integer.parseInt(WT.getText() );
h = Integer.parseInt(HT.getText() );
if(SELT.getText().equals("true")) { sel = true; }
else if(SELT.getText().equals("false")) { sel = false; }
if(BDT.getText().equals("white")) { bdcol = Color.white; }
else if(BDT.getText().equals("black")) { bdcol = Color.black; }
else if(BDT.getText().equals("red")) { bdcol = Color.red; }
else if(BDT.getText().equals("green")) { bdcol = Color.green; }
else if(BDT.getText().equals("blue")) { bdcol = Color.blue; }
else if(BDT.getText().equals("yellow")) { bdcol = Color.yellow; }
else if(BDT.getText().equals("cyan")) { bdcol = Color.cyan; }
else if(BDT.getText().equals("magenta")) { bdcol = Color.magenta; }
if(FLT.getText().equals("white")) { outcol = Color.white; }
else if(FLT.getText().equals("black")) { outcol = Color.black; }
else if(FLT.getText().equals("red")) { outcol = Color.red; }
else if(FLT.getText().equals("green")) { outcol = Color.green; }
else if(FLT.getText().equals("blue")) { outcol = Color.blue; }
else if(FLT.getText().equals("yellow")) { outcol = Color.yellow; }
else if(FLT.getText().equals("cyan")) { outcol = Color.cyan; }
else if(FLT.getText().equals("magenta")) { outcol = Color.magenta; }
dispose();}});
cont.add(mybutt, BorderLayout.SOUTH);
pack();
setVisible(true);
}
}
new customDialg ();
}
public void showAbout() {
class aboutFrame extends JFrame{
public AboutFrame() {
JFrame frame = new JFrame("Frame with components");
JTextPane textArea = new JTextPane();
textArea.setEditable(false);
textArea.setBackground(Color.white);
textArea.setText("This is a Java Swing tutorial. The Java Swing tutorial" +
"\nis for beginners and intermediate Swing developers. After reading this tutorial," +
"\nis for beginners and intermediate Swing developers. After reading this tutorial," +
"\nis for beginners and intermediate Swing developers. After reading this tutorial," +
"\nis for beginners and intermediate Swing developers. After reading this tutorial," +
"\nis for beginners and intermediate Swing developers. After reading this tutorial," +
"\nis for beginners and intermediate Swing developers. After reading this tutorial," +
"\nis for beginners and intermediate Swing developers. After reading this tutorial," +
"\nis for beginners and intermediate Swing developers. After reading this tutorial," +
"\nis for beginners and intermediate Swing developers. After reading this tutorial," +
"\nyou will be able to develop non-trivial Java Swing applications." );
frame.add(textArea, BorderLayout.CENTER);
frame.setSize(450, 450);
frame.setLayout(new FlowLayout());
frame.setVisible(true);
}
}
new AboutFrame();
}
} //End of Class drawingFrame
Adding JLabel to JFrameCreating JLabels with a MouseListener as you create a Shape.
Page 1 of 1
3 Replies - 1106 Views - Last Post: 10 March 2009 - 03:37 PM
#1
Adding JLabel to JFrame
Posted 03 March 2009 - 11:06 PM
This is s program that allows a user to draw shapes by inputting the specifications(size, location, color etc). The user can select a shape and edit or delete it after creating it. I created JLabels and added MouseListeners to them. These JLabels are created when the main program runs but they are not shown yet. When a user creates/draws a shape, a label's setText is set and the label is displayed(the arrayList is updated as well). The problem is that I dont know how many shapes the user is going to create, so I dont know how many labels I need. And all these labels need MouseListeners because clicking on a label means selecting the shape associated with. So you can click on a label and you will be able to edit or delete the shape associated that label. I tried creating the labels in the actionEvent method for the shapes but they cannot be displayed if i create them there.
Replies To: Adding JLabel to JFrame
#2
Re: Adding JLabel to JFrame
Posted 04 March 2009 - 06:11 PM
Ouf... that stuff may become very complicated
Adding the labels in your actionPerformed should work but what will happen if the user creates 100 shapes ?
Your BoxLayout will become unmanageable
Not a good idea at all to have a GUI where JComponent can be dynamically added.
This is how I would do it.
First split you screen in 2 with a GridLayout: the labels on the left the drawing on the right
Solution 1:
In the left panel have a ComboBox that contains the list of all the shapes then all the labels required for ONE shape
When the user changes the selected shape in the ComboBox update the labels accordingly
Solution 2:
Put a JTabbedPane in the left panel. One Tab for every shape. You can add and delete tab easily
or
Adding the labels in your actionPerformed should work but what will happen if the user creates 100 shapes ?
Your BoxLayout will become unmanageable
Not a good idea at all to have a GUI where JComponent can be dynamically added.
This is how I would do it.
First split you screen in 2 with a GridLayout: the labels on the left the drawing on the right
Solution 1:
In the left panel have a ComboBox that contains the list of all the shapes then all the labels required for ONE shape
When the user changes the selected shape in the ComboBox update the labels accordingly
Solution 2:
Put a JTabbedPane in the left panel. One Tab for every shape. You can add and delete tab easily
or
#3
Re: Adding JLabel to JFrame
Posted 09 March 2009 - 09:28 PM
Thanks for the response.
I have decided to use JList because it turned out be much easier. So i divided the content Pane of the JFrame into 2 panels, The JListPanel and the DrawingboardPanel. If i draw a shape, for example Rectangle, on the DrawingboardPanel, the JListPanel displays "0:Rectangle". When I draw a Triangle on DrawingboardPanel, the JListPanel displays "1:Triangle". I draw an ellipse, it displays "3:Ellipse" and so on. If I click on any of these list items, it is highlighted, this means I can click on a particular list item and edit or delete the Shape associated with it. I have not done all this yet but it is where I am moving towards.
Momodou.
I have decided to use JList because it turned out be much easier. So i divided the content Pane of the JFrame into 2 panels, The JListPanel and the DrawingboardPanel. If i draw a shape, for example Rectangle, on the DrawingboardPanel, the JListPanel displays "0:Rectangle". When I draw a Triangle on DrawingboardPanel, the JListPanel displays "1:Triangle". I draw an ellipse, it displays "3:Ellipse" and so on. If I click on any of these list items, it is highlighted, this means I can click on a particular list item and edit or delete the Shape associated with it. I have not done all this yet but it is where I am moving towards.
Momodou.
#4
Re: Adding JLabel to JFrame
Posted 10 March 2009 - 03:37 PM
mlfatty, on 9 Mar, 2009 - 08:28 PM, said:
Thanks for the response.
I have decided to use JList because it turned out be much easier. So i divided the content Pane of the JFrame into 2 panels, The JListPanel and the DrawingboardPanel. If i draw a shape, for example Rectangle, on the DrawingboardPanel, the JListPanel displays "0:Rectangle". When I draw a Triangle on DrawingboardPanel, the JListPanel displays "1:Triangle". I draw an ellipse, it displays "3:Ellipse" and so on. If I click on any of these list items, it is highlighted, this means I can click on a particular list item and edit or delete the Shape associated with it. I have not done all this yet but it is where I am moving towards.
Momodou.
I have decided to use JList because it turned out be much easier. So i divided the content Pane of the JFrame into 2 panels, The JListPanel and the DrawingboardPanel. If i draw a shape, for example Rectangle, on the DrawingboardPanel, the JListPanel displays "0:Rectangle". When I draw a Triangle on DrawingboardPanel, the JListPanel displays "1:Triangle". I draw an ellipse, it displays "3:Ellipse" and so on. If I click on any of these list items, it is highlighted, this means I can click on a particular list item and edit or delete the Shape associated with it. I have not done all this yet but it is where I am moving towards.
Momodou.
On this regard JList and JComboBox are almost the same
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote



|