java程序运行后 只能显示一个button 其他的不能显示 只有鼠标在上面的时间可以显示 是怎么回事?
importjava.awt.Color;importjava.awt.Dimension;importjava.awt.Graphics;importjava.awt....
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Timekeeper extends JFrame implements ActionListener{
public Timekeeper()
{
super("MyClock");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300,300);
setBackground(Color.YELLOW);
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((int)dimension.getWidth()/2-200,(int)dimension.getHeight()/2-200);
JButton buttonStart = new JButton("启动");
JButton buttonPause = new JButton("暂停");
JButton buttonReset = new JButton("复位");
buttonStart.setMargin(new Insets(0,0,0,0));
buttonPause.setMargin(new Insets(0,0,0,0));
buttonReset.setMargin(new Insets(0,0,0,0));
JPanel jp = new JPanel();
jp.setLayout(null);
buttonStart.setBounds(20, 20,50,30);
buttonPause.setBounds(80, 20,50,30);
buttonReset.setBounds(50, 50,50,30);
jp.add(buttonStart);
jp.add(buttonPause);
jp.add(buttonReset);
this.setLayout(null);
jp.setVisible(true);
jp.setBounds(100, 100, 300, 300);
this.add(jp);
setResizable(false);
setVisible(true);
int delay = 1000;
ActionListener ac = new ActionListener(){
public void actionPerformed(ActionEvent evt){
repaint();
}
};
new Timer(delay,ac).start();
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
public void paint(Graphics g)
{
g.setColor(Color.YELLOW);
g.fillRect(0, 0, 300, 50);
g.setColor(Color.RED);
g.drawString(new SimpleDateFormat("hh:mm:ss").format(new Date()), 130, 50);
}
public static void main(String []args)
{
new Timekeeper();
}
} 展开
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Timekeeper extends JFrame implements ActionListener{
public Timekeeper()
{
super("MyClock");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300,300);
setBackground(Color.YELLOW);
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((int)dimension.getWidth()/2-200,(int)dimension.getHeight()/2-200);
JButton buttonStart = new JButton("启动");
JButton buttonPause = new JButton("暂停");
JButton buttonReset = new JButton("复位");
buttonStart.setMargin(new Insets(0,0,0,0));
buttonPause.setMargin(new Insets(0,0,0,0));
buttonReset.setMargin(new Insets(0,0,0,0));
JPanel jp = new JPanel();
jp.setLayout(null);
buttonStart.setBounds(20, 20,50,30);
buttonPause.setBounds(80, 20,50,30);
buttonReset.setBounds(50, 50,50,30);
jp.add(buttonStart);
jp.add(buttonPause);
jp.add(buttonReset);
this.setLayout(null);
jp.setVisible(true);
jp.setBounds(100, 100, 300, 300);
this.add(jp);
setResizable(false);
setVisible(true);
int delay = 1000;
ActionListener ac = new ActionListener(){
public void actionPerformed(ActionEvent evt){
repaint();
}
};
new Timer(delay,ac).start();
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
public void paint(Graphics g)
{
g.setColor(Color.YELLOW);
g.fillRect(0, 0, 300, 50);
g.setColor(Color.RED);
g.drawString(new SimpleDateFormat("hh:mm:ss").format(new Date()), 130, 50);
}
public static void main(String []args)
{
new Timekeeper();
}
} 展开
3个回答
展开全部
最好不要在窗体设置背景,颜色什么的,不然就会出现这种情况,特别是Jbutton最突出,最好使用button控件,要不然你在窗体上放个JPanel,在JPanel里放控件iu不会出现鼠标划过才显示按钮的问题了,这个问题我以前纠结过,现在一切OK
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询