Cyp*_*her 0 java swing jpanel jframe jbutton
我的问题只是改变颜色的一小部分.当我点击那些按钮时,我想改变整个背景.我已经在谷歌搜索没有任何反应.
我使用面板,但它似乎不仅仅是一个小部分,它可以改变我想要一个完整的背景.
import java.awt.event.MouseListener;
import javax.swing.JOptionPane;
import java.awt.event.*;
/**
*
*
* @author Christopher Porras
* @Version 0.1
* @Doing GUI
*/
public class Button extends JFrame {
private JButton bred;
private JButton bblue;
private JButton bgreen;
private JPanel mousepanel;
public Button()
{
super("ChangeColor");
setLayout(new FlowLayout());
setSize(200,200);
mousepanel = new JPanel();
mousepanel.setBackground(Color.white);
add(mousepanel);
bred = new JButton("REd");
add(bred);
bblue = new JButton("Blue");
add(bblue);
bgreen = new JButton("Green");
add(bgreen);
thehandler handler = new thehandler();
bred.addMouseListener(handler);
bblue.addMouseListener(handler);
bgreen.addMouseListener(handler);
}
private class thehandler implements MouseListener
{
public void mouseClicked(MouseEvent e)
{
if(e.getSource()==bred)
{
mousepanel.setBackground(Color.red);
}
else if(e.getSource()==bblue)
{
mousepanel.setBackground(Color.blue);
}
else if(e.getSource()==bgreen)
{
mousepanel.setBackground(Color.green);
}
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
//To change body of generated methods, choose Tools | Templates.
}
}
public static void main(String[]args)
{
Button button = new Button();
button.setVisible(true);
button.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
Run Code Online (Sandbox Code Playgroud)
您处于正确的轨道上,但您需要将事件绑定到按钮组件而不是鼠标的中断信号.
而不是MouseListener,改为theHandler:
public class TheActionHandler implements ActionListener {
@Override
public void actionPerformed(final ActionEvent e) {
if(e.getSource()==bred)
{
mousepanel.setBackground(Color.red);
}
else if(e.getSource()==bblue)
{
mousepanel.setBackground(Color.blue);
}
else if(e.getSource()==bgreen)
{
mousepanel.setBackground(Color.green);
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8258 次 |
| 最近记录: |