awt

mixing awt and swing in GUI programming using Java

こ雲淡風輕ζ 提交于 2019-11-30 16:20:48
I read on SO that mixing awt and swing is not really a good approach for GUI programming in Java. I cannot though, find any examples which does not use some awt components while using swing. For example, even when using swing, most of the example I have encountered will use awt for layout and event handling. That being said, what does it mean not to mix swing and awt in GUI programming using Java? Does it just mean not to borrow graphical widgets such as button, canvas from swing and awt at the same time or just completely use swing or awt alone? Or it is okay to use graphical units ( like

One action listener, two JButtons

微笑、不失礼 提交于 2019-11-30 15:09:03
I have two JButtons called "Left" and "Right". The "Left" button moves a rectangle object to the left and the "Right" button moves it to the right. I have one ActionListener in the class that acts as the listener for when either button is clicked. However I want different actions to happen when each are clicked. How can I distinguish, in the ActionListener , between which was clicked? Amarnath Set actionCommand to each of the button. // Set the action commands to both the buttons. btnOne.setActionCommand("1"); btnTwo.setActionCommand("2"); public void actionPerformed(ActionEvent e) { int

Difference between c# using and Java import

依然范特西╮ 提交于 2019-11-30 15:06:11
问题 I know that in java that we use *(asterisk) to import all the contents in a package like import java.lang.*; Then why don't we use same *(asterisk) in C# to import all the contents is there any method like in java to import all the contents. What is the difference between import java.awt.*; and using System.windows.forms; 回答1: What the Java import does what .NET is called a reference - adding a reference to an assembly in .NET allows you to use the (public) types defined in that assembly. The

Java - FontMetrics without Graphics

血红的双手。 提交于 2019-11-30 14:28:22
问题 How to get FontMetrics without use Graphics ? I want to get FontMetrics in constructor, now I do this way: BufferedImage bi = new BufferedImage(5, 5, BufferedImage.TYPE_INT_RGB); FontMetrics fm = bi.getGraphics().getFontMetrics(font); int width = fm.stringWidth(pattern); int height = fm.getHeight(); 回答1: Hmm... It is quite logical that you need graphics to get FontMetrics. Font height, width etc. can differ on various displays. If you have some Component, you can use it for getting

Difference between c# using and Java import

一个人想着一个人 提交于 2019-11-30 13:59:12
I know that in java that we use *(asterisk) to import all the contents in a package like import java.lang.*; Then why don't we use same *(asterisk) in C# to import all the contents is there any method like in java to import all the contents. What is the difference between import java.awt.*; and using System.windows.forms; What the Java import does what .NET is called a reference - adding a reference to an assembly in .NET allows you to use the (public) types defined in that assembly. The C# using directive is simply a way to access these types without typing out the whole namespace. You can

How to hide the arrow buttons in a JScrollBar

久未见 提交于 2019-11-30 13:44:03
I need to hide the arrow buttons of java.awt.Scrollbar(VERTICAL) in an AWT application. Does anyone know how this can be achieved? I saw an example here , but the code just hides the buttons. The vacant space for the buttons still remains; it is not occupied by the scroll bar. To be more exact, here is the screenshot of what I should achieve. I am not sure which direction to go about it. Update : I was looking for a solution in AWT. But now I am open to suggestions in Swing as well. Harry Lime Try this.. it replaces the regular buttons on the Vertical ScrollBar with buttons that are 0x0 in

Java - FontMetrics without Graphics

与世无争的帅哥 提交于 2019-11-30 11:05:18
How to get FontMetrics without use Graphics ? I want to get FontMetrics in constructor, now I do this way: BufferedImage bi = new BufferedImage(5, 5, BufferedImage.TYPE_INT_RGB); FontMetrics fm = bi.getGraphics().getFontMetrics(font); int width = fm.stringWidth(pattern); int height = fm.getHeight(); Hmm... It is quite logical that you need graphics to get FontMetrics. Font height, width etc. can differ on various displays. If you have some Component, you can use it for getting FontMetrics: component.getFontMetrics(font); No you do not necessarily need to get/use the graphics object: Font font

Handling the Event Dispatch Thread

♀尐吖头ヾ 提交于 2019-11-30 10:22:56
I have a question about the 'Event Dispatch Thread'. I have a Main class that is also a JFrame. It initialises the rest of the components in the code, some of them do not involve Swing and some of them do. Is it enough to simply initialise the Main class using the EDT like this?... public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { new Main(); } }); } This way everything would run on the Event Dispatcher thread. That is generally sufficient until you start making use of background threads for calculations, data acquisition, etc.

How to draw heart using java awt libaray?

蹲街弑〆低调 提交于 2019-11-30 09:58:40
问题 How to draw heart using java awt libaray? I am using Java AWT Libaray and I need to draw a heart for my game. How can I do this using AWT? Here what I was thinking: may be use g.fillArc() ? but then if I make two curves at top than how to make a triangle at button? Would that be g.fillPolygon() ? g.fillArc(x, y, 20,20, 60, 60); //so this will be left curve g.fillArc(x+20, y, 20,20, 60, 60); //so right curve? //button triangle? I was wondering if any one is experence in awt and could show me

How to convert Image to BufferedImage in Java?

我怕爱的太早我们不能终老 提交于 2019-11-30 09:48:56
问题 How to convert Image to BufferedImage in Java? Note, that existing answer is apparently not correct, because it uses methods getWidth(null) and getHeight(null), which can return -1 if image is not loaded yet ( Image is, by definition, an asynchronous object). Please, provide either more correct answer, or more proofs that existing answer is already correct. 回答1: If it's important to you, you can use a MediaTracker to "wait" for the image to be loaded, then you don't need to care about