java按钮和paint同时显示出了问题,请问下面的代码应该怎么改?

主方法中new MyFrame()

import java.awt.*;
import javax.swing.*;
public class MyFrame extends Frame{
MyFrame()
{
super("test.fuckyou");
setLayout(null);
setSize(1920, 1080);
Button[] button = new Button[3];
button[0] = new Button("1");
button[1] = new Button("1");
button[2] = new Button("1");
button[0].setBounds(960, 300, 50, 50);
button[1].setBounds(660, 700, 50, 50);
button[2].setBounds(1260, 700, 50, 50);
add(button[0]);
add(button[1]);
add(button[2]);
setVisible(true);
}
public void paint(Graphics g)
{
g.drawLine(985, 325, 685, 725);
}
}
这样可以实现
把Frame换为JFrame,Button换为JButton

import java.awt.*;
import javax.swing.*;
public class MyFrame extends JFrame{
MyFrame()
{
super("test.fuckyou");
setLayout(null);
setSize(1920, 1080);
JButton[] button = new JButton[3];
button[0] = new JButton("1");
button[1] = new JButton("1");
button[2] = new JButton("1");
button[0].setBounds(960, 300, 50, 50);
button[1].setBounds(660, 700, 50, 50);
button[2].setBounds(1260, 700, 50, 50);
add(button[0]);
add(button[1]);
add(button[2]);
setVisible(true);
}
public void paint(Graphics g)
{
g.drawLine(985, 325, 685, 725);
}
}
下半部分代码实现的是一条线和一个按钮,而且移动窗口按钮也不见了
上半部分代码可以实现3个按钮和一条线,下面的代码应该怎么改

慕神8447489
浏览 135回答 2
2回答

天涯尽头无女友

把paint()方法修改一下:public void paint(Graphics g){super.paint(g);g.drawLine(985, 325, 685, 725);}另外,有一个按钮快到窗体边缘了,可能看不清楚。

哆啦的时光机

public void paint(Graphics g){super.paint(g);g.drawLine(985, 325, 685, 725);g.dispose();}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java