我刚开始学习 Java 并且有一些菜鸟问题。下面几乎是我的程序的完整列表,因为现在我想听听我能听到的所有批评。
我要解决的“问题”是如何在它们JLabel
之间共享一个对象,JPanels
以便我可以处理类似的事件MousePressed
?我设法处理了来自 PlotSurface 的 MousePressed 事件:在我以某种方式移动平面上的光标后,JLabel
坐标会刷新。但是代码对我来说看起来很难看。任何建议将不胜感激!
目标是处理来自ControlsPanel
(X、Y、Z 和 R 可以更改)的事件。我想PlotSurface
在这四个更改后画一个圆圈。
class PlotSurface extends JPanel {
private ColoredCircle redCircle = new ColoredCircle();
private Point start = new Point(0,0);
private JLabel _label;
public PlotSurface(JLabel label)
{
_label = label;
setBackground(Color.WHITE);
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
String pos = redCircle.getX() + "," + redCircle.getY();
_label.setText(pos);
moveCircle(e.getX(),e.getY());
}
});
addMouseMotionListener(new MouseAdapter() {
public void mouseDragged(MouseEvent e) {
String pos = redCircle.getX() + "," + redCircle.getY();
_label.setText(pos);
moveCircle(e.getX(),e.getY());
}
});
}
private void moveCircle(int x, int y) {
final int CURR_X = redCircle.getX();
final int CURR_Y = redCircle.getY();
final int CURR_W = redCircle.getWidth();
final int CURR_H = redCircle.getHeight();
final int OFFSET = 1;
if ((CURR_X!=x) || (CURR_Y!=y)) {
// The circle is moving, repaint background over the old square location.
repaint(CURR_X,CURR_Y,CURR_W+OFFSET,CURR_H+OFFSET);
// Update coordinates.
redCircle.setX(x);
redCircle.setY(y);
// Repaint the circle at the new location.
repaint(redCircle.getX(), redCircle.getY(),
redCircle.getWidth()+OFFSET,
redCircle.getHeight()+OFFSET);
}
}
@Override
public Dimension getPreferredSize() {
return new Dimension(150,150);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
String pos = redCircle.getX() + "," + redCircle.getY();
Graphics2D g2 = (Graphics2D) g;
g2.drawString(pos,10,10);
g2.drawLine(0,getHeight()/2,getWidth(),getHeight()/2);
g2.drawLine(getWidth()/2,0,getWidth()/2,getHeight());
g2.fillOval(100,100,50,50);
redCircle.paintCircle(g2);
}
}
class ColoredCircle{ //
it know how to draw itself }
class ControlsHeader extends JPanel
{
public ControlsHeader() {
super();
JLabel lblR = new JLabel();
lblR.setText("R: ");
//...
JSpinner x = new JSpinner(model);
//...
JComboBox y = new JComboBox();
//..
ButtonGroup z = new ButtonGroup();
}
}
class ControlsFooter extends JPanel
{
public ControlsFooter()
{
super();
//a couple of elements
}
}
class ControlsFrame extends JFrame
{
public ControlsFrame(String title)
{
super(title);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setPreferredSize(new Dimension(1000, 1000));
JLabel coords = new JLabel();
coords.setText("COORDS");
coords.setForeground(Color.WHITE);
JPanel footer = new ControlsFooter();
footer.add(coords);
add(new PlotSurface(coords), BorderLayout.CENTER);
add(new ControlsHeader(), BorderLayout.NORTH);
add(footer, BorderLayout.SOUTH);
}
}
public class Lab4UI {
private static void startGUI() {
JFrame frame = new ControlsFrame("Lab 4. №36");
frame.pack();
frame.setVisible(true);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
startGUI();
}});
}
}