Java 将mouseListener、JRadioButton和paintComponent集成到JPanel中选择并创建形状中
不熟悉MouseListener和paintComponent工具,但我必须使用它们来创建GUI,该GUI能够在每个鼠标单击位置创建形状(首先选择一个),而无需使用小程序。第一次在这里,不知道如何正确地张贴语法以及。对不起Java 将mouseListener、JRadioButton和paintComponent集成到JPanel中选择并创建形状中,java,user-interface,shapes,Java,User Interface,Shapes,不熟悉MouseListener和paintComponent工具,但我必须使用它们来创建GUI,该GUI能够在每个鼠标单击位置创建形状(首先选择一个),而无需使用小程序。第一次在这里,不知道如何正确地张贴语法以及。对不起 Create.java public class Create extends JFrame implements ActionListener{ static int x, y; static int sSelection; JPanel blank
public class Create extends JFrame implements ActionListener{
static int x, y;
static int sSelection;
JPanel blankSlate, shapeChoice, topMenu;
JMenuBar scBar, utilityBar;
JMenu fileMenu, shapeMenu;
JMenuItem menuiSave, menuiLoad, menuiNew;
JRadioButtonMenuItem triA, rectA, circle;
public Create(){
//container block
Container container=getContentPane();
container.setLayout(new BorderLayout());
//creation block
topMenu=new JPanel();
topMenu.setLayout(new GridLayout(1,3));
blankSlate=new JPanel();
blankSlate.setLayout(new BorderLayout());
shapeChoice=new JPanel();
shapeChoice.setLayout(new GridLayout(1,3));
//utilities block
utilityBar=new JMenuBar();
setJMenuBar(utilityBar);
fileMenu=new JMenu("File");
menuiSave=new JMenuItem("Save");
menuiLoad=new JMenuItem("Load");
menuiNew=new JMenuItem("New");
fileMenu.add(menuiNew);
fileMenu.add(menuiSave);
fileMenu.add(menuiLoad);
utilityBar.add(fileMenu);
//adding bars to sections block
topMenu.add(utilityBar);
//Blank Slate block
//need to add mouselistener and paintComponent according to selection of shapes
AL a1=new AL();
//shape choice block
scBar=new JMenuBar();
setJMenuBar(scBar);
shapeMenu=new JMenu("Shapes");
triA=new JRadioButtonMenuItem("Triangle");
circle=new JRadioButtonMenuItem("Circle");
rectA=new JRadioButtonMenuItem("Rectangle");
shapeMenu.add(triA);
shapeMenu.add(circle);
shapeMenu.add(rectA);
ButtonGroup gr= new ButtonGroup();
gr.add(triA);
gr.add(circle);
gr.add(rectA);
triA.addActionListener(this);
circle.addActionListener(this);
rectA.addActionListener(this);
scBar.add(shapeMenu);
//adding bars to sections block
shapeChoice.add(scBar);
//adding sections to container
container.add(BorderLayout.NORTH, topMenu);
container.add(BorderLayout.CENTER,blankSlate);
container.add(BorderLayout.SOUTH,shapeChoice);
}
public void formula()
{
if(sSelection==1){
ShapeRect r1=new ShapeRect();}
else if(sSelection ==2){}
else if(sSelection==3){}
}
class AL extends MouseAdapter{
public void mouseClicked(MouseEvent e){
x=e.getX();
y=e.getY();
drawing(x, y);
}
}
}
public class Test extends JFrame{
public static void main(String[]args){
Create fr = new Create ();
fr.setSize(500, 500);
fr.setDefaultCloseOperation(EXIT_ON_CLOSE);
fr.setLocationRelativeTo(null);
fr.setTitle("Create");
fr.setVisible(true);
}
}
public class ShapeRect extends Shape{
static int x, y;
public void paintComponent(Graphics g) {
paintComponent(g);
g.setColor(Color.BLACK);
g.fillRect(x, y, 20, 20);
}
}
public class ShapeRect extends Shape{
static int x, y;
public void paintComponent(Graphics g) {
paintComponent(g);
g.setColor(Color.BLACK);
g.fillRect(x, y, 20, 20);
}
}