java小程序设计,设计一个窗口程序
编写程序设计一个窗口程序,窗口下方有yellow,blue和green三个颜色按钮,当按钮按下时,窗口背景颜色会变成相关颜色,窗口可以正常关闭。需要带有程序的!谢谢!本人...
编写程序
设计一个窗口程序,窗口下方有yellow,blue和green三个颜色按钮,当按钮按下时,窗口背景颜色会变成相关颜色,窗口可以正常关闭。
需要带有程序的!谢谢!本人是java初学者! 展开
设计一个窗口程序,窗口下方有yellow,blue和green三个颜色按钮,当按钮按下时,窗口背景颜色会变成相关颜色,窗口可以正常关闭。
需要带有程序的!谢谢!本人是java初学者! 展开
3个回答
展开全部
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FrameTest {
private JFrame jf;
private JButton yellow,blue,green;
private JPanel panel1;
public FrameTest(){
jf = new JFrame("Frame");
yellow = new JButton("yellow");
blue = new JButton("blue");
green = new JButton("green");
panel1 = new JPanel();
panel1.setLayout(new FlowLayout());
jf.add(panel1,BorderLayout.CENTER);
jf.setVisible(true);
jf.setSize(500,600);
jf.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
);
blue.setForeground(Color.blue);
panel1.add(blue);
blue.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
panel1.setBackground(Color.blue);
}
});
yellow.setForeground(Color.yellow);
panel1.add(yellow);
yellow.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
panel1.setBackground(Color.yellow);
}
});
green.setForeground(Color.green);
panel1.add(green);
green.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
panel1.setBackground(Color.green);
}
});
}
public static void main(String args[]){
FrameTest ft = new FrameTest();
}
}
import java.awt.event.*;
import javax.swing.*;
public class FrameTest {
private JFrame jf;
private JButton yellow,blue,green;
private JPanel panel1;
public FrameTest(){
jf = new JFrame("Frame");
yellow = new JButton("yellow");
blue = new JButton("blue");
green = new JButton("green");
panel1 = new JPanel();
panel1.setLayout(new FlowLayout());
jf.add(panel1,BorderLayout.CENTER);
jf.setVisible(true);
jf.setSize(500,600);
jf.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
);
blue.setForeground(Color.blue);
panel1.add(blue);
blue.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
panel1.setBackground(Color.blue);
}
});
yellow.setForeground(Color.yellow);
panel1.add(yellow);
yellow.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
panel1.setBackground(Color.yellow);
}
});
green.setForeground(Color.green);
panel1.add(green);
green.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
panel1.setBackground(Color.green);
}
});
}
public static void main(String args[]){
FrameTest ft = new FrameTest();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询