所以这是我当前项目的注释中给出的例子。通常我运行示例代码并使用它来查看一切是如何工作的。但是,我不确定如何正确调用示例代码中的函数。
这是给我的:
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Draw extends JPanel implements ActionListener
{
JTextField tfInfo;
JLabel lblColor, lblShapes;
JCheckBox cbRed, cbBlue;
ButtonGroup shapes;
JRadioButton rbCircle, rbSquare;
JButton btnSubmit;
public Draw()
{
setLayout(new FlowLayout(FlowLayout.CENTER,5,5));
tfInfo = new JTextField("Color & Shapes", 15);
lblColor = new JLabel("Colors:");
cbRed = new JCheckBox("Red");
cbBlue = new JCheckBox("Blue");
lblShapes = new JLabel("Shapes:");
shapes = new ButtonGroup();
rbCircle = new JRadioButton("Circle");
rbSquare = new JRadioButton("Square");
btnSubmit = new JButton("Submit");
btnSubmit.addActionListener(this);
add(tfInfo);
add(lblColor);
add(cbRed);
add(cbBlue);
add(lblShapes);
add(rbCircle);
add(rbSquare);
add(btnSubmit);
shapes.add(rbCircle);
shapes.add(rbSquare);
}
public static void main(String [] args)
{
Draw n = new Draw();
n.setVisible(true);
}
public void actionPerformed(ActionEvent a)
{
if(a.getSource() == btnSubmit)
{
if(cbRed.isSelected()&&cbBlue.isSelected())
{
if(rbCircle.isSelected())
{
tfInfo.setText("urple Circle");
}
else if(rbSquare.isSelected())
{
tfInfo.setText("Purple Square");
}
}
else if(cbRed.isSelected())
{
if(rbCircle.isSelected())
{
tfInfo.setText("Red Circle");
}
else if(rbSquare.isSelected())
{
tfInfo.setText("Red Square");
}
}
else if(cbBlue.isSelected())
{
if(rbCircle.isSelected())
{
tfInfo.setText("Blue Circle");
}
}
else if(rbSquare.isSelected())
{
tfInfo.setText("Blue Square");
}
}
}
public class MApp extends JPanel implements MouseListener
{
private boolean clicked;
private Rectangle r;
public MApp()
{
clicked = false;
r = new Rectangle(10, 10, 50, 50);
addMouseListener(this);
}
public void paintComponent(Graphics g)
{
if(clicked)
{
g.setColor(Color.BLUE);
}
else
{
g.setColor(Color.RED);
}
g.fillRect((int)r.getX(), (int)r.getY(),
(int)r.getWidth(), (int)r.getHeight());
}
public void mouseClicked (MouseEvent e)
{
Point p = new Point(e.getX(),e.getY());
if(r.contains(p))
{
clicked = !clicked;
}
repaint();
}
public void mousePressed (MouseEvent evnt) {}
public void mouseReleased (MouseEvent evnt) {}
public void mouseEntered (MouseEvent evnt) {}
public void mouseExited (MouseEvent evnt) {}
}
}
答案 0 :(得分:1)
我想我会把它分解为这些部分:
1)将MApp放入自己的java文件中。另外,我将mouseClicked中的repaint()更改为:paintComponent(getGraphics());您的新java文件将如下所示:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JPanel;
public class MApp extends JPanel implements MouseListener
{
private boolean clicked;
private Rectangle r;
public MApp()
{
clicked = false;
r = new Rectangle(10, 10, 50, 50);
addMouseListener(this);
}
public void paintComponent(Graphics g)
{
if(clicked)
{
g.setColor(Color.BLUE);
}
else
{
g.setColor(Color.RED);
}
g.fillRect((int)r.getX(), (int)r.getY(),
(int)r.getWidth(), (int)r.getHeight());
}
public void mouseClicked (MouseEvent e)
{
Point p = new Point(e.getX(),e.getY());
if(r.contains(p))
{
clicked = !clicked;
}
paintComponent(getGraphics());
}
public void mousePressed (MouseEvent evnt) {}
public void mouseReleased (MouseEvent evnt) {}
public void mouseEntered (MouseEvent evnt) {}
public void mouseExited (MouseEvent evnt) {}
}
下一步:像这样创建一个主文件:
import javax.swing.JFrame;
public class Tester {
public static JFrame window = new JFrame("Graphics");
public static void main(String[] args) {
window.setBounds(100, 100,800, 800);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(null);
MApp m = new MApp();
m.setBounds(100,100,50,50);
window.add(m);
Draw d = new Draw();
d.setBounds(0, 0, window.getWidth(), 80);
window.add(d);
window.setVisible(true);
}
}
(不要忘记从Draw类中删除MApp类)
您将能够运行您的代码并查看正在发生的事情......这只是一个快速示例,只有一些快捷方式,但您会明白这一点