jlabel

How to right-justify icon in a JLabel?

假如想象 提交于 2019-11-27 09:29:01
For a JLabel with icon, if you setHorizontalTextPosition(SwingConstants.LEADING) , the icon is painted right after text, no matter how wide the label is. This is particularly bad for a list, as the icons would be all over the place depending on how long the text is for each item. I traced the code and it seems to be that in SwingUtilities#layoutCompoundLabelImpl , text width is simply set to SwingUtilities2.stringWidth(c, fm, text) , and icon x is set to follow text without considering label width. Here is the simplest case: import java.awt.*; import javax.swing.*; public class TestJLabelIcon

Java: how to add image to Jlabel?

醉酒当歌 提交于 2019-11-27 08:51:36
Image image = GenerateImage.toImage(true); //this generates an image file JLabel thumb = new JLabel(); thumb.setIcon(image) You have to supply to the JLabel an Icon implementation (i.e ImageIcon ). You can do it trough the setIcon method, as in your question, or through the JLabel constructor: Image image=GenerateImage.toImage(true); //this generates an image file ImageIcon icon = new ImageIcon(image); JLabel thumb = new JLabel(); thumb.setIcon(icon); I recommend you to read the Javadoc for JLabel , Icon , and ImageIcon . Also, you can check the How to Use Labels Tutorial , for more

JLabel won't show with JPanel.setLayout(null). Why?

房东的猫 提交于 2019-11-27 08:33:52
问题 I want to show many different labels over a map, so I'm using null layout in my panel, and calling setLocation for each label. For some reason, though, the labels don't show. If I remove the pan.setLayout(null), then the label appears in the top-center of the panel. Why isn't null layout working with setPosition? package mapa; import java.awt.*; import javax.swing.*; public class Mapa extends JFrame { private static JPanel pan; private static JLabel lab; public Mapa() { } private static void

Draw in an image inside panel

[亡魂溺海] 提交于 2019-11-27 08:28:48
问题 I have a panel with two buttons. I'm trying to insert an image inside the panel and I want to draw lines inside the image after clicking on a button. I have used the below code but this doesn't seem to work. public class Try_Panel extends JFrame { // start attributes private JPanel jPanel1 = new JPanel(null, true); private JButton jButton1 = new JButton(); private JButton jButton2 = new JButton(); // end attributes public Try_Panel(String title) { // Frame-Init super(title);

putting marks on ImageIcon in JLabel

僤鯓⒐⒋嵵緔 提交于 2019-11-27 08:18:47
问题 So I'm trying to find a way to modify an image in Java. In other words, if user clicks on the image, a mark will be put at the point where the user just clicked. I have an ImageIcon which I put in a JLabel. So far, the approach I took was to use JLayeredPanel to put another JPanel on top of the JLabel and draw on this JPanel: //... ImageIcon icon = new ImageIcon("foo.jpg"); JLabel lb = new JLabel(icon); JPanel glass = new JPanel(); lb.setBounds(0, 0, 100, 100); glass.setBounds(0, 0, 100, 100)

Changing a JLabel's Value from a JSlider's Value

会有一股神秘感。 提交于 2019-11-27 08:04:16
问题 I have a single JPanel that contains a JSlider and a JLabel. I want to configure it so that when the JSlider's value is being changed by the user, that new value is reflected by the JLabel. I understand that I can fire ChangeEvents with the Slider, but I don't know how to add a ChangeListener to the JLabel. Here's a snippet of my code. scaleSlider.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent event) { int currentTime = ((JSlider)event.getSource()).getValue();

setLocation of Label

夙愿已清 提交于 2019-11-27 07:27:01
问题 I have all of the labels working correctly but the userLabel[3] is not positioning properly No matter what I do, the label "Color:" always shows up on the frame with a x-coordinate of 0 and a y-coordinate that is half way down the frame. JLabel[] userLabel = new JLabel[4]; for(int p = 0; p < userLabel.length; p++){ userLabel[p] = new JLabel(); userLabel[p].setSize(100,50); frameSetUp.add(userLabel[p]); } userLabel[0].setText("Width of Frame:"); userLabel[1].setText("Height of Frame:");

How to add fade/fade out effects to a JLabel

£可爱£侵袭症+ 提交于 2019-11-27 07:26:49
问题 Hello I am trying to create a Java game and need to add some effects to my label. I have the following questions How to add fade in/ fade out effects to my label . I have a JLabel but I need a shape for it, possibly a rectangle or a cloud shape . How can I get that done ? 回答1: You can use a AlphaComposite to change the opactiy level of a component... Now, with a little clever use of a timer, you can make the label fade in and out or simply control the opacity as you please... public class

How to make draggable components with ImageIcon

那年仲夏 提交于 2019-11-27 07:22:15
问题 I'm trying to build a user interface for a chess game. I've used a GridBagLayout filled with JLabels and the chess pieces are ImageIcons of the JLabels . Now I would like to move the pieces by dragging it on the board. Is there a way to do this with ImageIcons ? Or is there a better way to solve the problem? EDIT: here is a sample code. you can notice that you can move the iconImage, but it doesn't "drag" with the mouse. import java.awt.BorderLayout; import java.awt.Color; import java.awt

Append text in JLabel

和自甴很熟 提交于 2019-11-27 07:11:44
问题 How would I go about achieving the effect of JTextArea with JLabel? I want the output to be displayed every time the button is clicked on the next line down instead of replacing what text is already there, i.e. like an append method for JLabel? I just want it to follow the same behavior as JTextArea.append. Also I want to add hyperlink to each line. 回答1: Use HTML formatting in the label by starting the text with prefix <html><body> (possibly add some in-line styles in the body opening element