Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我对JLabel和JPanel的边界布局有意见_Java_Swing_Jpanel_Jlabel_Border Layout - Fatal编程技术网

Java 我对JLabel和JPanel的边界布局有意见

Java 我对JLabel和JPanel的边界布局有意见,java,swing,jpanel,jlabel,border-layout,Java,Swing,Jpanel,Jlabel,Border Layout,我在做这个绘图程序项目(更像是扩展它),我遇到了一个讨厌的bug。我在JPanel上添加了一个JLabel,位于BorderLayout的南部,但它不仅没有添加到JPanel的南部,而且在我在JPanel上绘制一些东西后,它也变得模糊。我不明白的是为什么会发生这两件事 课程编号: import java.awt.BorderLayout; import java.awt.Graphics; import java.awt.Point; import java.awt.event.MouseEve

我在做这个绘图程序项目(更像是扩展它),我遇到了一个讨厌的bug。我在JPanel上添加了一个JLabel,位于BorderLayout的南部,但它不仅没有添加到JPanel的南部,而且在我在JPanel上绘制一些东西后,它也变得模糊。我不明白的是为什么会发生这两件事

课程编号:

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Painter extends JPanel{

private int pointCount = 0; // count the number of points

// array of 10000 java.awt.Point references
private Point[] points = new Point[10000];
JLabel myCount = new JLabel();

// set up GUI and register mouse event handler
public Painter() {

    myCount.setText("Points so far: " + pointCount);
    add(myCount, BorderLayout.SOUTH);

    // handle frame mouse motion event
    addMouseMotionListener(
            new MouseMotionAdapter() { // anonymous inner class
                public void mouseDragged(MouseEvent event) {
                    if (pointCount < points.length) {
                        points[pointCount] = event.getPoint(); // find the point
                        ++pointCount; // increment number of points in the array
                        repaint();
                        myCount.setText("Points so far: " + pointCount);
                    } // end if     
            } // end of mouseDragged method
        } // end anonymous inner class
    ); // end of addMouseMotionListener
}// end Painter constructor

// draw ovals in a 4-by-4 bounding box at specified locations on window
public void paintComponent(Graphics g) {
    super.paintComponents(g); // clears drawing area

    // draw all points in the array
    for (int i = 0; i < pointCount; i++) {
        g.fillOval(points[i].x, points[i].y, 12, 12);
    } // end for loop
} // end method paintComponent
} // end of Painter class
导入java.awt.BorderLayout;
导入java.awt.Graphics;
导入java.awt.Point;
导入java.awt.event.MouseEvent;
导入java.awt.event.MouseMotionAdapter;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
公共类油漆工扩展JPanel{
private int pointCount=0;//计算点数
//10000 java.awt.Point引用数组
专用点[]点=新点[10000];
JLabel myCount=新的JLabel();
//设置GUI并注册鼠标事件处理程序
公共画家(){
setText(“到目前为止的点数:+pointCount”);
添加(myCount,BorderLayout.SOUTH);
//处理帧鼠标运动事件
addMouseMotionListener(
新的MouseMotionAdapter(){//匿名内部类
公共无效鼠标标记(鼠标事件){
if(点计数<点长度){
points[pointCount]=event.getPoint();//查找点
++pointCount;//增加数组中的点数
重新油漆();
setText(“到目前为止的点数:+pointCount”);
}//如果结束,则结束
}//mouseDragged方法的结尾
}//结束匿名内部类
);//addMouseMotionListener的结尾
}//端油漆工
//在窗上指定位置的4乘4边界框中绘制椭圆
公共组件(图形g){
super.paintComponents(g);//清除绘图区域
//绘制阵列中的所有点
对于(int i=0;i
第一个问题可以通过调用setLayout(新的BorderLayout)来解决;作为构造函数的第一行


第二个问题可以通过从paintComponents中删除“s”来解决,只需调用paintComponent即可“从paintComponents中删除“s”。非常感谢您,先生。还有一个问题。JPanel在默认情况下不应该有BorderLayout吗?@NlightNfotis是什么让你这么想的?文件中没有任何关于违约的内容layout@ControlAltDel这已经超过一个月了,但我要指出,“因为内容窗格默认使用
BorderLayout
类,所以代码不需要设置布局管理器。”