将Swing按钮添加到使用AWT绘制的程序

lib*_*997 3 java swing awt

我有一个绘制随机三角形的程序.我想添加一个按钮,点击后会擦除并重新生成另一个随机三角形.我测试看是否可以在另一个程序中显示一个按钮SwingButtonTest.java,(这是我滚动的方式,我正在学习)并且它成功了.

然后,我基本上复制了我认为必要的所有代码,使按钮显示从该程序到我的三角程序.不幸的是程序没有显示按钮......

再一次,感谢任何帮助,谢谢你!

import javax.swing.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


@SuppressWarnings({ "serial" })
public class TriangleApplet extends JApplet implements ActionListener {

   int width, height;
   int[] coords = new int[6];
   JButton refresh;

   public void init() {
      //This stuff was added from SwingButtonTest.java
      Container container = getContentPane();
      container.setLayout(null);

        container.setLayout(null);

        refresh= new JButton("I like trains");
        refresh.setBackground(Color.yellow);
        refresh.setSize(140, 20);
        refresh.addActionListener(this);

        container.add(refresh);
      //End of SwingButtonTest.java stuff

      coords = randomTri();
   }


    public static int[] randomTri() {
        int[] pointArray = new int[6];
        int x;
        for (int i = 0; i < 6; i++) {
            x = ((int)(Math.random()*40)*10);
            pointArray[i] = x;
        }
        return pointArray;
    }


   public void paint( Graphics g ) {
        g.setColor(Color.BLUE);

        g.drawLine(coords[0], coords[1], coords[2], coords[3]);
        g.drawString("A: " + coords[0]/10 + ", " + coords[1]/10, coords[0], coords[1]);
        g.drawLine(coords[2], coords[3], coords[4], coords[5]);
        g.drawString("B: " + coords[2]/10 + ", " + coords[3]/10, coords[2], coords[3]);
        g.drawLine(coords[4], coords[5], coords[0], coords[1]);
        g.drawString("C: " + coords[4]/10 + ", " + coords[5]/10, coords[4], coords[5]);
        //Math for AB
        int abXDif = Math.abs(coords[0]/10 - coords[2]/10);
        int abYDif = Math.abs(coords[1]/10 - coords[3]/10);
        int abLength = (abXDif*abXDif) + (abYDif*abYDif);
        g.drawString("AB: Sqrt of "+ abLength, 0, 10);
        //Math for BC
        int bcXDif = Math.abs(coords[2]/10 - coords[4]/10);
        int bcYDif = Math.abs(coords[3]/10 - coords[5]/10);
        int bcLength = (bcXDif*bcXDif) + (bcYDif*bcYDif);
        g.drawString("BC: Sqrt of "+ bcLength, 0, 20);
        //Math for AC
        int acXDif = Math.abs(coords[4]/10 - coords[0]/10);
        int acYDif = Math.abs(coords[5]/10 - coords[1]/10);
        int acLength = (acXDif*acXDif) + (acYDif*acYDif);
        g.drawString("AC: Sqrt of "+ acLength, 0, 30);


   }

@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub

}
}
Run Code Online (Sandbox Code Playgroud)

Rei*_*eus 6

按钮未显示的原因是您不打电话:

super.paint(g);
Run Code Online (Sandbox Code Playgroud)

paint()方法中.此调用将导致按钮和其他添加的组件被绘制.

在Swing中自定义绘制的更好方法是将自定义绘制放置在扩展JComponent和覆盖的组件中,paintComponent以利用Swing优化的绘制模型.

还可以考虑使用布局管理器来调整和放置组件.避免使用绝对定位(null布局).从没有布局管理器做:

虽然可以不使用布局管理器,但是如果可能的话,应该使用布局管理器.布局管理器可以更轻松地调整依赖于外观的组件外观,不同的字体大小,容器的大小变化以及不同的区域设置.