JAVA创建一个窗体添加两个按钮,为这两个按钮添加事件处理使窗体的下部分分面板背景颜色在黑白间转换
1个回答
展开全部
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class TestWin extends JFrame implements ActionListener {
private JButton blackBtn=new JButton("Black");
private JButton whiteBtn=new JButton("White");
private JPanel pane=new JPanel();
public TestWin() {
this.blackBtn.addActionListener(this);
this.whiteBtn.addActionListener(this);
JPanel btnPane=new JPanel();
btnPane.add(this.blackBtn);
btnPane.add(this.whiteBtn);
this.add(btnPane,"North");
this.add(pane,"Center");
this.setSize(800, 600);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
Object source=e.getSource();
if(source==this.blackBtn) {
this.pane.setBackground(Color.BLACK);
}else if(source==this.whiteBtn) {
this.pane.setBackground(Color.WHITE);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new TestWin());
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |