jlabel

how to center JLabel in Jframe Swing?

烈酒焚心 提交于 2019-12-30 06:00:02
问题 I have this working code for a stop watch in Swing. I want to have the label Time Remaining 300 seconds centered on the second line`. here is my code. import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.Timer; public class TimerGUI extends JPanel { JLabel

How do I present text vertically in a JLabel ? (Java 1.6)

让人想犯罪 __ 提交于 2019-12-29 06:46:14
问题 I'm looking to have text display vertically, first letter at the bottom, last letter at the top, within a JLabel. Is this possible? 回答1: I found this page: http://www.java2s.com/Tutorial/Java/0240__Swing/VerticalLabelUI.htm when I needed to do that. I don't know if you want the letters 'standing' on each other or all rotated on their side. /* * The contents of this file are subject to the Sapient Public License * Version 1.0 (the "License"); you may not use this file except in compliance *

Java: vertical alignment within JPanel

烂漫一生 提交于 2019-12-29 05:06:28
问题 I am trying to vertically align (center) both JLabels inside one JPanel. JPanel panel = new JPanel(); panel.setPreferredSize(size); JLabel label1 = new JLabel(icon); JLabel label2 = new JLabel("text"); panel.add(label1); panel.add(label2); I have tried using setAligmentY() with no success. Both labels always appear on the top of JPanel. UPD: Labels should be located next to each other like using FlowLayout, but in the middle of the JPanel. 回答1: Use a GridBagLayout with the default constraints

Underlined JLabel

时光怂恿深爱的人放手 提交于 2019-12-28 14:08:28
问题 I am trying to make a JLabel underlined. I searched everywhere, but I got nothing. Even in the properties, there is no option for underlining the JLabel. What can I do? 回答1: JLabel label = new JLabel("<HTML><U>YOUR TEXT HERE</U></HTML>"); label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); OR JLabel label = new JLabel("Underlined Label"); Font font = label.getFont(); Map attributes = font.getAttributes(); attributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON); label

Resizing image to fit in JLabel

前提是你 提交于 2019-12-28 07:05:16
问题 I am trying to load an image from directory and place it as an icon in JLabel . But when the image size is big it doesn't fit completely in the label. I tried resizing the image to fit in the label but its not working. May I know where I am going wrong? JFileChooser choose=new JFileChooser(); choose.showOpenDialog(null); File f=choose.getSelectedFile(); String filename=f.getAbsolutePath(); path.setText(filename); ImageIcon icon=new ImageIcon(filename); Image img=icon.getImage(); BufferedImage

dragging a jlabel around the screen

折月煮酒 提交于 2019-12-27 12:07:01
问题 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.*;

Centering a JLabel on a JPanel

…衆ロ難τιáo~ 提交于 2019-12-27 08:59:10
问题 I'm using the NetBeans GUI builder to handle my layout (I'm terrible with LayoutManagers) and am trying to place a simple JLabel so that it is always centered (horizontally) inside its parent JPanel. Ideally, this would maintain true even if the JPanel was resized, but if that's a crazy amount of coding than it is sufficient to just be centered when the JPanel is first created. I'm bad enough trying to handle layouts myself, but since the NetBeans GUI Builder autogenerates immutable code, it

Centering a JLabel on a JPanel

人盡茶涼 提交于 2019-12-27 08:58:07
问题 I'm using the NetBeans GUI builder to handle my layout (I'm terrible with LayoutManagers) and am trying to place a simple JLabel so that it is always centered (horizontally) inside its parent JPanel. Ideally, this would maintain true even if the JPanel was resized, but if that's a crazy amount of coding than it is sufficient to just be centered when the JPanel is first created. I'm bad enough trying to handle layouts myself, but since the NetBeans GUI Builder autogenerates immutable code, it

Java: Load image from file, edit and add to JPanel

允我心安 提交于 2019-12-25 07:52:02
问题 I want to load an image from my computer into 2D Graphics so that I can edit it afterwards and then I want to add it to JPanel . If you need to see my project I can send it to you. void loadImage() { FileDialog fd = new FileDialog(new Frame(), "Please choose a file:", FileDialog.LOAD); fd.show(); if (fd.getFile() != null) { File fil = new File(fd.getDirectory(), fd.getFile()); strDirectory = fd.getDirectory(); strFileType = fd.getFile(); mainImage.setIcon(new ImageIcon(fil.toString()));

Format Jlabel - JTextBox

佐手、 提交于 2019-12-25 07:45:11
问题 I have a simple binary tree printer: public String displayTree(){ if(root != null){ return this.toString(new StringBuilder(), true, new StringBuilder(), root).toString(); }else{ return "Empty tree"; } } private StringBuilder toString(StringBuilder prefix, boolean isLeft, StringBuilder sb, BinaryNode<T> node) { if(node.getRight() !=null) { toString(new StringBuilder().append(prefix).append(esIzquierdo ? "│ " : " "), false, sb, node.getRight()); } sb.append(prefix).append(isLeft? "└── " : "┌──