I'm new to java.
I want to make my image to act as the button in my project. Like the windows phone tile. My frame background is black. And I arranged 6 buttons. I also added an image but it comes with some white spaces around the image as borders. Here is the code i used:
ImageIcon image = new ImageIcon(getClass().getResource("yellowimage.png");
JButton button = new JButton(image);
I dont want those empty spaces. I just want the button to be exactly the image and wanted to type "click me" over the button. How can I do this ?
Try this:
JButton button = new JButton(image);
button.setContentAreaFilled(false);
button.setBorder(null);
You can also change the button image when it is pressed with:
button.setPressedIcon(pressedIcon);
You can try this to make JButton with an image.
Icon yourIcon = new ImageIcon("yourFile.gif");
JButton button2 = new JButton(yourIcon);
You can minimize the border around the image with setMargin();
ImageIcon image = new ImageIcon(getClass().getResource("yellowimage.png");
JButton button = new JButton(image);
button.setMargin(new Insets(1, 1, 1, 1));
Try this..
JButton button = new JButton();
ImageIcon icon = new ImageIcon(
ClassName.class.getResource("image.png"));
button.setIcon(icon);
button.setBackground(new Color(0,0,0,0));
button.setBorderPainted(false);
Related
I have a JButton that displays random text when clicked on but my problem is that if I click on it twice it will display random text again but to the side of the first one when I'd like it to displays at the exact same place instead of the first one (hope I'm being clear in what i'm saying haha)
I've already tried googling the problem and searching on reddit and stackoverflow but couldn't find what I'm looking for
Code of the button :
butt = new JButton("Generate");
butt.setFont(new Font("Tahoma", Font.BOLD, 12));
butt.setBackground(Color.WHITE);
butt.addActionListener(new ActionListener() {
public void actionPerformed (ActionEvent e) {
RandomString yep = new RandomString();
String c = yep.nextString();
text = new JTextArea(" "+c,0,0);
text.setLineWrap(true);
text.setBounds(10,10,100,60);
text.setFont(new Font("Courier", Font.BOLD, 14));
text.setEditable(false);
p3.add(text,BorderLayout.CENTER);
f.remove(p4);
f.add(p3,BorderLayout.CENTER);
}
});
If this was perfect it would display the text in a first click and in the second click it would display the text again but this time exactly where the first text was as if it was erasing it and displaying it again
I really hope this is understandable ! Thanks for any help that's like the final touches to this program :)
You are creating a JTextArea every time you perform action! Move the JTextArea out of the actionPerformed and set your text.
JTextArea text = new JTextArea();
JButton button = new JButton("Generate");
....
button.addActionListener(new ActionListener() {
....
String c = yep.nextString();
text.setText(c);
....
}
I am trying to give my interface a new function, but I have encountered some obstacles. I want to enlarge image on JLabel when mouseEnters.
Here is how my JLabels looks:
int sacle = 50 //Size of my JLabel Icon
int zoom = 10 // How much the icon should enlarge
imageIcon = new ImageIcon(new ImageIcon(myClass.class.getResource(Picture))
.getImage().getScaledInstance(scale, scale, Image.SCALE_SMOOTH));
JLabel stackIsGreat = new JLabel();
stackIsGreat.setIcon(imageIcon);
//and I add multiple of such JLabels`
And the code goes on and on. I wanted to creat a function and add it to mouseListener, so all will behave the same. I wanted to achive that with:
//inside external method
activeLabel = (javax.swing.JLabel)(e.getSource());
ImageIcon temp = (ImageIcon) activeLabel.getIcon();
But there is no way I know I could get use of this, because java says I need Image to create my enlarged ImageIcon
ImageIcon enlarged = new ImageIcon((Image).getScaledInstance(scale + zoom, scale + zoom, Image.SCALE_SMOOTH))
How can I retrive the image used to crate the JLabel from code.
Any help would be appreciated.
I want to enlarge image on JLabel when mouseEnters.
Instead of creating your own MouseListener you could use a JButton to give you the rollover effect:
Something like:
JButton button = new JButton(...);
button.setBorderPainted( false );
ImageIcon icon = (ImageIcon)button.getIcon();
Image image = icon.getImage();
Image scaled = image.getScaledImage(...);
button.setRolloverIcon( new ImageIcon( scaled ) );
This question already has answers here:
Resizing icon to fit on JButton in Java?
(4 answers)
Closed 6 years ago.
Hi how can I fit the size of a ImageIcon to a JButton? I want to adjust the size of the ImageIcon to the size of the Button
JFrame frame2 = new JFrame("Tauler Joc");
JPanel panell = new JPanel();
ImageIcon icon = new ImageIcon("king.jpg");
JButton jb= new JButton(icon);
jb.setBounds(200,200,700,700);
panell.add(jb);
frame2.add(panell);
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
You can do it this way by simply adding a little method to your project:
private static Icon resizeIcon(ImageIcon icon, int resizedWidth, int resizedHeight) {
Image img = icon.getImage();
Image resizedImage = img.getScaledInstance(resizedWidth, resizedHeight, java.awt.Image.SCALE_SMOOTH);
return new ImageIcon(resizedImage);
}
Now, to use this method in your example code:
JFrame frame2 = new JFrame("Tauler Joc");
JPanel panell = new JPanel();
ImageIcon icon = new ImageIcon("king.jpg");
JButton jb= new JButton();
jb.setBounds(200,200,700,700);
panell.add(jb);
// Set image to size of JButton...
int offset = jb.getInsets().left;
jb.setIcon(resizeIcon(icon, jb.getWidth() - offset, jb.getHeight() - offset));
frame2.add(panell);
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
If you just want the image and no border just set the offset variable to 0 or get rid of the offset variable altogether.
I want to set an icon for a jlabel, can somebody give me example syntax
I have tried this :
JLabel icon = new JLabel();
ImageIcon chromo = createImageIcon("res/icon.png");
panel.add(icon);
icon.setIcon(chromo);
After I tried this the label didn't show up on the panel at all.
Try this:
JLabel icon = new JLabel();
ImageIcon chromo = createImageIcon("res/icon.png");
icon.setIcon(chromo);
panel.add(icon);
And don't forget to check your layout, if it is absolute then you need to add bounds to it.
icon.setBounds(startX, startY, width, height);
I am trying to do a fairly basic swing GUI where a user can enter a url, choose a local file location etc. I am using multiple layout managers including boxlayout, borderlayout and flowlayout. The code is below.
My problem is that some of the components are moving around when the user puts text into the optionsTxt jtextarea. Anyone know where I should start to stop this happening?
Setup menu bar
JButton menu_File = new JButton("File");
JButton menu_Edit = new JButton("Edit");
JToolBar toolBar = new JToolBar();
toolBar.add(menu_File);
toolBar.add(menu_Edit);
//Setup options area
JPanel options = new JPanel();
options.setBorder(BorderFactory.createTitledBorder("Options"));
BoxLayout layout_Options = new BoxLayout(options, BoxLayout.Y_AXIS);
options.setLayout(layout_Options);
JLabel optionsLblURL = new JLabel("Enter URL:");
optionsTxtUrl = new JTextArea(1,15);
JLabel chooseDestLbl = new JLabel("Choose save location:");
chooseDest = new JButton("Browse");
chooseDest.addActionListener(this);
options.add(optionsLblURL);
options.add(optionsTxtUrl);
options.add(chooseDestLbl);
options.add(chooseDest);
//Setup launch area
JPanel launch = new JPanel();
launch.setBorder(BorderFactory.createTitledBorder("Launch"));
launchBtnStart = new JButton("Start Download");
launchBtnStart.setVerticalAlignment(SwingConstants.CENTER);
launchBtnStart.setHorizontalAlignment(SwingConstants.CENTER);
launchBtnStart.addActionListener(this);
launch.add(launchBtnStart);
//Setup reporting area
JPanel logging = new JPanel();
logging.setBorder(BorderFactory.createTitledBorder("Log"));
BoxLayout layout_Logging = new BoxLayout(logging, BoxLayout.Y_AXIS);
logging.setLayout(layout_Logging);
JTextArea loggingTxt = new JTextArea(3,10);
loggingTxt.setEditable(false);
logging.add(pb);
logging.add(loggingTxt);
//Add components to window
BorderLayout borderLayout = new BorderLayout();
setLayout(borderLayout);
add("North", toolBar);
add("West", options);
add("East", launch);
add("South", logging);
setVisible(true);
"My problem is that some of the components are moving around when the user puts text into the optionsTxt jtextarea. Anyone know where I should start to stop this happening?"
Start by putting your JTextArea in a ScrollPane and setLineWrap(true) and setWrapStyleWord(true). You may want to consider doing this with both JTextAreas you have
JTextArea optionsTxtUrl = new JTextArea(1,15);
optionsTxtUrl.setLineWrap(true);
optionsTxtUrl.setWrapStyleWord(true);
JScrollPane scroll = new JScrollPane(optionsTxtUrl);
options.add(scroll); // take out options.add(optionsTxtUrl);
This will make your lines wrap when they reach the right edge of the text area
public void setWrapStyleWord(boolean word) - Sets the style of wrapping used if the text area is wrapping lines. If set to true the lines will be wrapped at word boundaries (whitespace) if they are too long to fit within the allocated width. If set to false, the lines will be wrapped at character boundaries. By default this property is false.
public void setLineWrap(boolean wrap) - Sets the line-wrapping policy of the text area. If set to true the lines will be wrapped if they are too long to fit within the allocated width. If set to false, the lines will always be unwrapped. A PropertyChange event ("lineWrap") is fired when the policy is changed. By default this property is false.
If this doesn't solve your problem, you should edit your post with a Minimal, Complete, Tested and Readable example so we can test out your problem.