关于JAVA的JFrame和JPanel的问题,程序运行后,JPanel 无法显示. 代码如下:
publicclassSimpleGui{JFrameframe;JButtonbutton1;JButtonbutton2;JLabellabel;publicstat...
public class SimpleGui {
JFrame frame ;
JButton button1;
JButton button2;
JLabel label;
public static void main (String [] args){
SimpleGui gui = new SimpleGui ();
gui.go();
}
public void go (){
frame = new JFrame ();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button1 = new JButton ("egg");
button1.addActionListener(new Button1Listener ());
button2 = new JButton ("wawa");
button2.addActionListener(new Button2Listener ());
label = new JLabel ("i am wawa ");
MyDrawPanel drawpanel = new MyDrawPanel ();
frame.getContentPane().add(BorderLayout.SOUTH,button1);
frame.getContentPane().add(BorderLayout.EAST,button2);
frame.getContentPane().add(BorderLayout.CENTER,drawpanel);
frame.getContentPane().add(BorderLayout.WEST,label);
frame.setSize(300,300);
frame.setVisible(true);
}
class Button1Listener implements ActionListener {
@Override
public void actionPerformed(ActionEvent event) {
frame.repaint();
}
}
class Button2Listener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
label.setText("HOHO");
}
}
}
类MyDrawPanel的代码如下:
class MyDrawPanel extends JPanel{
public void painComponent(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
GradientPaint gradient = new GradientPaint(70,70,Color.blue,150,150,Color.orange);
g2d.setPaint(gradient);
g2d.fillOval(70, 70, 100, 100);
}
} 展开
JFrame frame ;
JButton button1;
JButton button2;
JLabel label;
public static void main (String [] args){
SimpleGui gui = new SimpleGui ();
gui.go();
}
public void go (){
frame = new JFrame ();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button1 = new JButton ("egg");
button1.addActionListener(new Button1Listener ());
button2 = new JButton ("wawa");
button2.addActionListener(new Button2Listener ());
label = new JLabel ("i am wawa ");
MyDrawPanel drawpanel = new MyDrawPanel ();
frame.getContentPane().add(BorderLayout.SOUTH,button1);
frame.getContentPane().add(BorderLayout.EAST,button2);
frame.getContentPane().add(BorderLayout.CENTER,drawpanel);
frame.getContentPane().add(BorderLayout.WEST,label);
frame.setSize(300,300);
frame.setVisible(true);
}
class Button1Listener implements ActionListener {
@Override
public void actionPerformed(ActionEvent event) {
frame.repaint();
}
}
class Button2Listener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
label.setText("HOHO");
}
}
}
类MyDrawPanel的代码如下:
class MyDrawPanel extends JPanel{
public void painComponent(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
GradientPaint gradient = new GradientPaint(70,70,Color.blue,150,150,Color.orange);
g2d.setPaint(gradient);
g2d.fillOval(70, 70, 100, 100);
}
} 展开
3个回答
展开全部
明显的问题,这种布局需要东西南北中,你没有北?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
呵呵,这个也不难的,我说个方法吧,
把圆 矩形等抽象出一个接口或类,
画图时,把图放到一个List里(LinkedList最快),
重写JFrame的 reprint()方法,在reprint()的时候把list里的图形添上去;
把圆 矩形等抽象出一个接口或类,
画图时,把图放到一个List里(LinkedList最快),
重写JFrame的 reprint()方法,在reprint()的时候把list里的图形添上去;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询