在我的源代码中,
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Object6 extends JFrame {
JButton p = new JButton("Y");
JButton n = new JButton("N");
public Object6(){
setSize(1280,800);
setVisible(true);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void paint(Graphics g){
p.setLocation(590,500);
n.setLocation(590,550);
add(p);
add(n);
p.setSize(100,50);
n.setSize(100,50);
g.drawString("Does statement 6 apply?", 100, 100);
}
public static void main(String[]args){
new Object6();
}
}
按钮“p”,出现字符串;但是,只有当我点击它应该是的空间时才会出现按钮“n”。当我删除g.drawString("Does statement 6 apply?", 100, 100);
时,两个按钮同时出现。
当两个按钮同时出现时,如何添加g.drawString("Does statement 6 apply?", 100, 100);
?
答案 0 :(得分:3)
paint
方法中修改UI,这可能会导致paint
方法一次又一次地被调用......直到它耗尽CPU周期super.paint
paint
顶级容器,一般应避免覆盖paint
。而是使用JPanel
之类的内容并覆盖其paintComponent
方法看看
答案 1 :(得分:0)
你的绘画方法应该是这样的......
public void paint(Graphics g){
super.paint(g);
g.drawString("Does statement 6 apply?", 100, 100);
}
我认为它解决了你的问题
答案 2 :(得分:-1)
感谢所有回答的人。这是具有相同问题的任何人的最终(现在正在工作)代码:
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Object6 extends JFrame {
JPanel a = new JPanel();
JButton p = new JButton("Y");
JButton n = new JButton("N");
Color c = new Color(0x4BBCF8);
JLabel b = new JLabel("Does statement 6 apply?");
public Object6(){
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
setSize(1280,800);
setBackground(c);
panel();
}
public void panel(){
a.setLocation(540,600);
a.setSize(200,50);
add(a);
a.setLayout(null);
p.setLocation(0,0);
n.setLocation(100,0);
p.setSize(100,50);
n.setSize(100,50);
a.add(p);
a.add(n);
int e = b.getText().length();
b.setLocation((1280-e*8)/2,100);
b.setSize(8*e,16);
add(b);
}
public static void main(String[]args){
new Object6();
}
}