3

I have simple GUI code as follows, in which I want to make the JButton one translucent, so that the image behind the JButton is visible!

package dealORnodeal;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class Deal extends JFrame implements ActionListener
{
private Container contentPane = getContentPane();
private JButton one = new JButton("1"),two = new JButton("2");
private JMenu menu1 = new JMenu("JumpTo");
private JMenuBar bar1 = new JMenuBar();
private ImagePanel bg = new ImagePanel(new ImageIcon("bg.jpg").getImage());
public Deal()
{

    super("Deal Or No Deal");
    setSize(800,850);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setResizable(false);
    setLayout(null);
    contentPane.add(bg);

    JMenuItem item1;

    item1 = new JMenuItem("Start Game");
    item1.addActionListener(this);
    menu1.add(item1);

    item1 = new JMenuItem("GoTo Rules");
    item1.addActionListener(this);
    menu1.add(item1);

    item1 = new JMenuItem("GoTo Credits");
    item1.addActionListener(this);
    menu1.add(item1);

    item1 = new JMenuItem("GoTo Menu");
    item1.addActionListener(this);

    menu1.add(item1);
    bar1.add(menu1);
    setJMenuBar(bar1);

    //GAME CODE
    one.setBounds(25,151,190,49);
    one.addActionListener(this);

    add(one);
    //GAME CODE END

setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) 
{}
}

Now how would the code be if I wanted to set the button to be translucent so that the background image would be visible through the button. BTW please don't confuse Translucent with transparent!

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Dharav Patel
  • 49
  • 2
  • 6

2 Answers2

1

I can't comment to your question, so I'll answer you here..

if you use this code:

myButton.setOpaque(false);

It would not paint the button - because now it's a trasnparent. to create the button translucent I think you should override the button paint method..

take a look at this thread

Community
  • 1
  • 1
Elior
  • 3,178
  • 6
  • 37
  • 67
1

setOpaque doesn't work for JButtons, the right property is:

button.setContentAreaFilled(false);

user1251007
  • 15,891
  • 14
  • 50
  • 76
Karlo
  • 11
  • 1