jlabel

Java: Linebreaks in JLabels?

北慕城南 提交于 2019-11-26 18:28:08
问题 I'm trying to make a Swing JLabel with multiple lines of text. It's added just fine, but the line breaks don't come through. How do I do this? Alternatively, can I just specify a maximum width for a JLabel and know that the text would wrap, like in a div? private void addLegend() { JPanel comparisonPanel = getComparisonPanel(); //this all displays on one line JLabel legend = new JLabel("MMM FFF MMM FFFO O OOM M MMMM.\nMMM FFF MMM FFFO O OOM M MMMM.\nMMM FFF MMM FFFO O OOM M MMMM.\n");

Why is my JLabel not showing up

ぐ巨炮叔叔 提交于 2019-11-26 18:02:06
I am calling this method called check in one of my abstract classes but for some reason the JLabel (problem) I am adding to the JPanel (panel) is not showing up. Why is this occurring? Any explanations, I am using both the repaint, and validate methods but still nothing shows up. The problem you're having is you're blocking the Event Dispatching Thread, prevent the UI from been updated or any new events from been processed... It starts here... for(int i = 0; i < 15; i++) { //... //Check to see if user has enetered anything // And is compounded here while(!answered) { Thread.sleep(duration); //

dragging a jlabel around the screen

最后都变了- 提交于 2019-11-26 17:53:30
So I am trying to click and drag a JLabel around a JFrame. The following code allows a JLabel to be moved around the screen when the mouse is pressed / dragged at any point on the screen, but I am not sure how to add a second ActionListener to check if the mouse is clicking on the label, assuming that is the solution. I would like to have multiple JLabels on the screen so that the only label being moved is the one that the mouse has clicked and is now dragging. Thanks. import java.awt.*; import java.awt.event.*; import javax.swing.*; @SuppressWarnings("serial") public class test extends JFrame

How do I put html in a JLabel in java?

蓝咒 提交于 2019-11-26 17:13:07
问题 How do I use html tags in a JLabel in java? 回答1: To put html in a JLabel , you would make it look something like this JLabel label = new JLabel("<html><yourTagHere><yourOtherTagHere>this is your text</yourOtherTagHere></yourTagHere></html>"); 回答2: This will do the trick: String labelText ="<html><FONT COLOR=RED>Red</FONT> and <FONT COLOR=BLUE>Blue</FONT> Text</html>"; JLabel coloredLabel =new JLabel(labelText); 回答3: There are following ways Using SetText method of JLabel Object JLabel

Customize JOptionPane Dialog

非 Y 不嫁゛ 提交于 2019-11-26 17:06:01
问题 I am learning java swing. The code below is a catch block which handles an IOException and shows a error message. catch(IOException e) { System.out.println("IOException"); JOptionPane.showMessageDialog(null,"File not found",null, JOptionPane.ERROR_MESSAGE); } I was thinking of declaring and customizing a JOptionPane of my own inside the catch block like the code below: JOptionPane jop=new JOptionPane(); jop.setLayout(new BorderLayout()); JLabel im=new JLabel("Java Technology Dive Log", new

find out if text of JLabel exceeds label size

落爺英雄遲暮 提交于 2019-11-26 16:43:12
问题 In Java when the text of the JLabel could not be displayed due to lack of space the text is truncated and "..." is added in the end. How can I easily find out if currently JLabel displays full text or the truncated? EDIT: I see that there is a way to find out the size of the text by using FontMetrics . However this solution doesn't fully answers the question. In the case the text of JLabel contains HTML decorations the metrics.stringWidth() would also calculate width of HTML tags. So it could

How to change the size of the font of a JLabel to take the maximum size

大憨熊 提交于 2019-11-26 16:14:31
I have a JLabel in a Container. The defaut size of the font is very small. I would like that the text of the JLabel to take the maximum size. How can I do that? coobird Not the most pretty code, but the following will pick an appropriate font size for a JLabel called label such that the text inside will fit the interior as much as possible without overflowing the label: Font labelFont = label.getFont(); String labelText = label.getText(); int stringWidth = label.getFontMetrics(labelFont).stringWidth(labelText); int componentWidth = label.getWidth(); // Find out how much the font can grow in

How do I set a JLabel's background color?

六眼飞鱼酱① 提交于 2019-11-26 15:57:47
In my JPanel , I set the background of a JLabel to a different color. I can see the word "Test" and it's blue, but the background doesn't change at all. How can I get it to show? this.setBackground(Color.white); JLabel label = new JLabel("Test"); label.setForeground(Color.blue); label.setBackground(Color.lightGray); this.add(label); Use label.setOpaque(true); Otherwise the background is not painted, since the default of opaque is false for JLabel . From the JavaDocs : If true the component paints every pixel within its bounds. Otherwise, the component may not paint some or all of its pixels,

Resize a picture to fit a JLabel

做~自己de王妃 提交于 2019-11-26 15:35:43
问题 I'm trying to make a picture fit a JLabel. I wish to reduce the picture dimensions to something more appropriate for my Swing JPanel. I tried with setPreferredSize but it doesn't work. I'm wondering if there is a simple way to do it? Should I scale the image for this purpose? 回答1: Outline Here are the steps to follow. Read the picture as a BufferedImage. Resize the BufferedImage to another BufferedImage that's the size of the JLabel. Create an ImageIcon from the resized BufferedImage. You do

Print jLabel&#39;s icon in a printer using a button [closed]

时间秒杀一切 提交于 2019-11-26 14:57:35
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 years ago . I have a jLabel with an Icon that I want to print in a printer (canon,hp,epson anything) using a button. How can I do that? Any useful code? Code snippet? links? All I can see is like this: How to print content of a label in java? But It's not what I want. I'm using netbeans Thanks in advance. 回答1: Basically,