jframe

Why do people run Java GUI's on the Event Queue

流过昼夜 提交于 2019-11-27 06:00:47
问题 In Java, to create and show a new JFrame , I simply do this: public static void main(String[] args) { new MyCustomFrameClass().setVisible(true); } However, I have seen many people doing it like this: public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { new MyCustomFrameClass().setVisible(true); } }); } Why? Are there any advantages? 回答1: The rules governing what needs to be performed on the EDT (I see "EDT" more often used than "Event Queue")

How to disable main JFrame when open new JFrame

邮差的信 提交于 2019-11-27 05:52:11
问题 Example now I have a main frame contains jtable display all the customer information, and there was a create button to open up a new JFrame that allow user to create new customer. I don't want the user can open more than one create frame. Any swing component or API can do that? or how can disabled the main frame? Something like JDialog. 回答1: I would suggest that you make your new customer dialog a modal JDialog so that you do not allow input from other dialogs/frames in your app while it is

Java Swing on high-DPI screen

懵懂的女人 提交于 2019-11-27 05:49:32
问题 I have a Java Swing program that uses the System Look And Feel: UIManager.setLookAndFeel (UIManager.getSystemLookAndFeelClassName ()); The problem is that on a high-DPI system, the fonts in the frames are way too small. How can I make the text on my frames readable without having to change the fonts for ALL the frames? My program was written using Java 6 and has too many frames to modify. 回答1: You could physically modify the look and feel's font settings... import java.awt.EventQueue; import

Is there any way I can rotate this 90 degrees?

徘徊边缘 提交于 2019-11-27 05:41:08
This is my current code: package Calendar; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.SimpleDateFormat; import java.util.Date; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.Timer; public class Clock extends JLabel { private String pattern; private Timer timer; public Clock(String pattern){ this.pattern = pattern; createTimer(); timer.start(); } public Clock(){ pattern = "hh:mm:ss a"; createTimer(); timer.start(); } private void createTimer(){ timer = new Timer(0, new ActionListener() {

Java Container remove method not working correctly

可紊 提交于 2019-11-27 05:39:08
i hava added 1.TextArea 2.TextField then i start adding JButton successively on container...,now by using JRadioButton i want to remove JButton from container using this code i=0; k=0; while(!birdButton[i].isSelected()){ i++; } System.out.println(i); k=i+2; list.removeElementAt(i); listName.removeElementAt(i); System.out.println(k); c.getContentPane().remove(k); but when i select the 1st radiobutton 1st JButton should be deleted because of k=i+2; instead of deleting this one it deletes the TextArea(1st one). when i select the 3rd radiobutton then 1st JButton is deleted. can anyone let me know

How to add an image to a JFrame title bar?

寵の児 提交于 2019-11-27 05:38:35
I want to add an image (small icon) to the javax.swing.JFrame title bar. How can I do it? mre Since JPanel doesn't have a title bar, I'm assuming that you're referring to JFrame . That being said, use setIconImage(...) . Here is an example of using setIconImages() . import java.awt.Image; import javax.swing.*; import javax.imageio.ImageIO; import java.net.URL; import java.util.*; class FrameIcons { public static void main(String[] args) throws Exception { URL url16 = new URL("http://i.stack.imgur.com/m0KKu.png"); URL url32 = new URL("http://i.stack.imgur.com/LVVMb.png"); final List<Image>

Java Swing Blank JFrame coming up?

风格不统一 提交于 2019-11-27 05:38:29
I'm new to swing, and was wondering why sometimes my application comes up as blank, and sometimes it displays the components. It seems to be sporadic. There are no exceptions thrown or anything like that. It just frequently comes up as a blank JFrame. At times when I close the application and run it again, it shows the components correctly, but it mainly comes up as blank. Am I doing something wrong in the code? I'm using the Eclipse IDE, if it matters. Here's the code: import java.applet.Applet; import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.GridLayout; import java

How to use KeyListener with JFrame?

﹥>﹥吖頭↗ 提交于 2019-11-27 05:35:10
So, I was trying to make a rectangle move with a KeyEvent ( KeyListener ) and whenever I try to hit the key, the rectangle doesn't move. The rectangle is drawn, but whenever I hit the left and right keys, nothing happens. I have two classes, one is my main class with the keyEvents and the frame and the other, draws the rectangle and holds the function to move the rectangle. Here is my code: import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.JFrame; public class mainFrame extends JFrame implements KeyListener{ mainDraw Draw = new mainDraw(); public void

Clear components of JFrame and add new components

半世苍凉 提交于 2019-11-27 05:22:48
I have a JFrame , which have some options. When OK button is pressed i want the same JFrame to clear the contents and add new contents. I have tried it but the problem is new JFrame is popped out. What am I doing wrong? import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.IOException; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.*; public class GuiFrame extends JFrame { final JFrame f = new JFrame("Test"); public void Starter(){

How do I draw an image to a JPanel or JFrame?

老子叫甜甜 提交于 2019-11-27 05:02:54
How do I draw an Image to a JPanel or JFrame, I have already read oracle's tutorial on this but I can't seem to get it right. I need the image " BeachRoad.png " to be displayed on a specific set of coordinates. Here is what I have so far. public class Level1 extends JFrame implements ActionListener { static JLayeredPane EverythingButPlayer; static Level1 l1; public Level1() { EverythingButPlayer = new JLayeredPane(); BufferedImage img = null; try { img = ImageIO.read(new File("BeachRoad.png")); } catch (IOException e) { } Graphics g = img.getGraphics(); g.drawImage(img,0, 0,