jlabel

How to prevent JLabel positions from resetting?

▼魔方 西西 提交于 2019-11-28 07:07:11
问题 I have a JPanel that contains 11 JLabels, each of them registered with a MouseMotionListener like so (generated by Netbeans): label1.addMouseMotionListener(new MouseMotionAdapter() { public void mouseDragged(MouseMotionEvent evt){ label1MouseDragged(evt); } and the individual labelXMouseDragged methods contain (for example): label1.setLocation(label1.getParent().getMousePosition()); This Panel lives inside another Panel alongside various other controls. I find that I can drag my labels just

Using a custom font for a JLabel

不打扰是莪最后的温柔 提交于 2019-11-28 06:11:02
问题 I'm trying to use a special font in my JFrame, but I'm running into problems. I have a JLabel defined like this: private JLabel lab = new JLabel("Text"); and I have a file called CUSTOMFONT-MEDIUM.TTF (TrueType font) but after writing the following: try { lab.setFont(Font.createFont(Font.TRUETYPE_FONT, getClass().getResource("/CUSTOMFONT-MEDIUM.TTF").openStream())); } catch(IOException ex){ //exception handled here I suppose } catch(FontFormatException ex2) { //same here } the code compiles

Changing ImageIcon to another picture with button click

☆樱花仙子☆ 提交于 2019-11-28 04:38:24
问题 So I want to replace an ImageIcon in a JLabel every time a button is pressed. I made it so the image, label, and GridBagConstraints are public. When I try to change it though nothing happens. Am I going about this the wrong way or? Thanks! package hi.low; import java.awt.*; import java.awt.event.*; import java.util.ArrayList; import javax.swing.*; import java.util.Random; import java.util.ArrayList; public class Card_panel extends JPanel implements ActionListener { private final int WIDTH =

How should HTML for Java components reference resources?

心不动则不痛 提交于 2019-11-28 04:05:09
问题 Resources for HTML (style sheets, images etc.) might not be loaded correctly when: A Swing app. is distributed, or when it is put into a Jar file. The HTML is generated at run-time. How can such resources be accessed reliably in the HTML? 回答1: HTML from a Jar file that links resources (e.g. CSS or images) by relative references will work just fine. E.G. This example loads HTML (that has a relative reference to an image) from a Jar. import javax.swing.*; import java.net.URL; class ShowHtml {

.setBounds not working for JLabel and JButton

对着背影说爱祢 提交于 2019-11-28 02:29:40
I am trying to change the positioning of a JLabel and a JButton on my GUI. Even though I try to use .setBounds to change their locations; they both just appear in the top centre of the screen. import java.awt.color.*; import java.awt.font.*; import java.awt.Graphics; import java.awt.Image; import javax.swing.*; import javax.swing.*; import java.awt.event.*; import java.awt.*; import javax.swing.UIManager.*; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; public class yo implements MouseListener { Image image; JButton button = new JButton("Wassup"); JFrame frame = new

How to format JLabel/JTextField so text wraps [duplicate]

烂漫一生 提交于 2019-11-28 02:29:15
This question already has an answer here: Is there a “word wrap” property for JLabel? 6 answers Lets say I have a string and JLabel, the JLabel displayed on a JFrame: public String content="Hello fellow stack-overflowers, I want to format this output"; public JLabel jL = new JLabel(); Once do the following: jL.setText(content); I get this output: Hello fellow stack-overflowers, I want to for.. But what I really want is the text not to keep going right past the length of the label or textfield, but to make a newline every like 4 words or so, something like: Hello fellow stack-overflowers, I

How to dynamically add JLabels to JPanel?

跟風遠走 提交于 2019-11-28 01:56:32
I'm having a problem with this. I have a JPanel and normally I would create a JLabel like this: JLabel lblNewLabel = new JLabel("New label"); lblNewLabel.setBounds(0, 0, 135, 14); panel.add(lblNewLabel); but I want each time I click a button, in that panel to be created a new JLabel with the same size, but with a different height possition. I tried: panel.add(new JLabel(stringName)); but this way I don't get to set it's bounds. stringName I get from a JTextField. First off, use a layout . Done correctly the layout will place the components like you want. Secondly, when dynamically adding a

JLabel displaying countdown, java

落花浮王杯 提交于 2019-11-28 01:36:01
I've got a "status" JLabel in one class (named Welcome) and the timer in another one (named Timer). Right now, the first one displays the word "status" and the second one should be doing the countdown. The way I would like it to be, but don't know how to - display 10, 9, 8, 7 ... 0 (and go to the next window then). My attempts so far: // class Welcome setLayout(new BorderLayout()); JPanel area = new JPanel(); JLabel status = new JLabel("status"); area.setBackground(Color.darkGray); Font font2 = new Font("SansSerif", Font.BOLD, 25); status.setFont(font2); status.setForeground(Color.green); area

Split image into clickable regions

江枫思渺然 提交于 2019-11-28 01:23:19
问题 Is there any way to split an image to region (right now it's JLabel but I can change it if necessary)? I use swing in my program and I have an image (square for this example) with some triangles, stars and trapezoids inside it (it can be JPG, PNG, etc). The idea is that the user will click inside one of those shapes and then I will put another small icon on top of the area the user clicked. The user can click on multiple areas but at the end of the day, I need to know which shapes were

Make JLabel background transparent again

一世执手 提交于 2019-11-28 01:14:30
I have a JLabel that changes its background color when the mouse enters it. The problem I have is that I want the JLabel to become transparent after the mouse exits. Is there a statement I can use to accomplish this? It's a lazy holiday here in Germany, so combining the two answers: final JLabel label = new JLabel("some label with a nice text"); label.setBackground(Color.YELLOW); MouseAdapter adapter = new MouseAdapter() { /** * @inherited <p> */ @Override public void mouseEntered(MouseEvent e) { label.setOpaque(true); label.repaint(); } /** * @inherited <p> */ @Override public void