awt

Put JLabel on Component in JPanel

回眸只為那壹抹淺笑 提交于 2019-12-06 15:55:37
I have JPanel which contain a component imageWindow(object of this ) now i want to put a label tag in the center of this imagewindow but i am not able to do so? JPanel imagePanel = new JPanel(); imagePanel.setLayout(new GridBagLayout()); imagePanel.add(ic); //ic is imageWindow obj JLabel l1=new JLabel("First Label."); JPanel example =new JPanel(); example.add(l1); imagePanel.add(example); what i am doing wrong here is screenshow i need to put the label here. i want to put the label at the center but it always coming in the right side what i am doing wrong? You could display the image in a

How to make java.awt.Label background transparent?

百般思念 提交于 2019-12-06 15:53:51
I used to do the transparent background with javax.swing.JLabel this way: lbl.setBackground(new Color(0, 0, 0, 0)); . But it doesn't work with java.awt.Label. Is there any simple way to make the label transparent? Update: public class SplashBackground extends Panel { private static final long serialVersionUID = 1L; private Image image = null; /** * This is the default constructor */ public SplashBackground() { super(); initialize(); } /** * This method initializes this * */ private void initialize() { image = Toolkit.getDefaultToolkit().createImage(getClass().getResource("/splash/splash.jpg"))

Why doesn't Graphics2D.setStoke() work for Graphics2D.drawString?

耗尽温柔 提交于 2019-12-06 15:51:49
I want the string to have different width so that I set the stroke of Graphics2D and the code is here: import java.awt.BasicStroke; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import javax.swing.JFrame; import javax.swing.JPanel; public class StrokeTest { public static void main(String[] args) { StrokeTest test = new StrokeTest(); test.createUI(); } public void createUI(){ JFrame frame = new JFrame(); frame.add(new MainPanel()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } @SuppressWarnings("serial")

Using Mouse Click to Generate Balls

久未见 提交于 2019-12-06 15:46:53
I have below code, I need to alter it so that the balls are generated with a mouse click rather than all of them generating at once. I know I need to use a mouse listener but I do not know how I can integrate that into what I have without "breaking" the app. No changes needed import javax.swing.JFrame; import java.awt.Canvas; import java.awt.Dimension; import java.awt.Graphics2D; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.image.BufferStrategy; public class Display { public final int width; public final int height; private JFrame frame; private

SystemTray based application without window on Mac OS X

不问归期 提交于 2019-12-06 15:13:41
问题 How do I do an application that runs only as a SystemTray TrayIcon on mac os x, (without an awt window and dock icon)? The code I'm using is this: public class App { public static void main( String[] args ) { final TrayIcon trayIcon; if (SystemTray.isSupported()) { SystemTray tray = SystemTray.getSystemTray(); Image image = Toolkit.getDefaultToolkit().getImage("tray.gif"); trayIcon = new TrayIcon(image, "Tray Demo"); trayIcon.setImageAutoSize(true); try { tray.add(trayIcon); } catch

java UnsatisfiedLinkError awt.image

你说的曾经没有我的故事 提交于 2019-12-06 14:28:05
I have a program that makes use of the following method to get a scaled instance of an image icon: public ImageIcon createScaledImageIcon(String filename) { ImageIcon icon = new ImageIcon(filename); Image image = icon.getImage().getScaledInstance(cardWidth, cardHeight, Image.SCALE_SMOOTH); icon.setImage(image); return icon; } I don't know if it's the source of the problem or not. But i get the following error messages: Exception in thread "Image Fetcher 0" java.lang.UnsatisfiedLinkError: sun.awt.image.ImageRepresentation.setBytePixels(IIII[BIILsun/awt/image/ByteComponentRaster;I)V at sun.awt

FontMetrics setFont?

你。 提交于 2019-12-06 13:53:00
I am making a program in which I need to set the Font inside a FontMetrics object. I have already created the fontmetrics before, but now I don't have access to the Graphics object anymore. Yet, I want to change the font inside the FontMetrics, that is, create a new FontMetrics, with a new Font, but using the same Graphic context. I could make the Graphics avaliable, but it would take a lot of time and it wouldn't make sense at all to do so. Would anyone know an easier way to it? It seens so obvius it should be a way, maybe I am just missing something really stupid (I looked at the

Programmatically swap two JPanels in JFrame

落花浮王杯 提交于 2019-12-06 13:36:10
I am trying to accomplish the above functionality, and am having little success. I am using GridLayout with 2 columns and 2 rows to show the user a puzzle-like game, where there are 4 (200x200 pixel) JPanels (3 colored, 1 default bgColor) which fill the whole contentPane. Clicking on a colored panel resolves in an assessment if the panel is next to the gray panel. If so, they should swap. I have accomplished every step to the last one, where they swap. Here's the code: public class MainClass extends JFrame { //Generated private static final long serialVersionUID = 4710628273679698058L; private

GridBagLayout weighty alignment

╄→尐↘猪︶ㄣ 提交于 2019-12-06 13:30:21
I am dinamically generating a layout which requires to use relative sizes, the only way I found to do it without using an external Java layout library is GridBagLayout's weightx and weighty. At this point it has been working exactly as I need with one small exception. When I have one column containing two JPanels with a space distribution of 66.6% and 33.3% respectively and then another column with 3 JPanels using 33.3% of the space each one of them, the 33.3% of the first column is not the same as the 33.3% of the second. I need them to be perfectly aligned. Unfortunately this is my first

Can I use multiple ActionListeners in a single class?

六月ゝ 毕业季﹏ 提交于 2019-12-06 13:29:06
Consider the overly simplified example below; some of it written with pseudocode for brevity. When I try running this as is, I get a compiler error stating the actionPerformed is already found in my main method. However, if I rename it, say to actionPerformed2 it's no longer recognized by ActionListener . Do I need to combine the listeners for both the foo and foo2 methods into a single ActionListener method?? How do I properly differentiate listeners from each other when using multiple listeners in a single class with multiple button object? I'm just beginning to play with swing components,